Thread: Need help on how to copy a picture diagonally!

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Oct 2013
Posts
7
Rep Power
0

Need help on how to copy a picture diagonally!

I need help on how to copy a picture from the bottom right side of the picture to the top left side of the picture. This is my copy from top to bottom method:

public void mirrorTopToBottom() {
for (int y = 0; y < getHeight() / 2; y++) {
for (int x = 0; x < getWidth(); x++) {
Pixel topPixel = getPixel(x, getHeight() - 1 - y);
Pixel bottomPixel = getPixel(x, y);
topPixel.setColor (bottomPixel.getColor());
}
}
this.repaint();
}
2. Have you designed how the pixels should be moved?
For example: Take a simple 3x3 grid, number the 9 squares and then design how the numbers should be moved to get the desired results.

Then expand that logic to the size of the image.
3. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Sep 2013
Posts
8
Rep Power
0
What happens when you run your code?
4. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Oct 2013
Posts
7
Rep Power
0
I need help on how to write the code.
5. need help on how to write the code.
If you will post your design for how the code should work, we can help you write code that follows that design.

Did you draw the numbered grid and work out how to move the numbers to get the desired results? The numbers represent the pixels.
Can you post a copy of a numbered grid for before and for after the numbers are moved?
6. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Oct 2013
Posts
7
Rep Power
0
I did do the diagram but here is how I think the code should go:

Code:
```public void mirrorDiagonal() {
int width = getWidth();
int height = getHeight();
int max;
if(width > height) {
max = height;
}
else {
max = width;
}
for (int y = 0; y < height ; y++) {
for (int x = 0; x < width; x++) {
Pixel bottomPixel = getPixel(x, y);
Pixel topPixel = getPixel(x, getHeight() - 1 - y);
Pixel leftPixel = getPixel(x, y);
Pixel rightPixel = getPixel(getWidth() - 1 - x, y);
Pixel botRightPixel = getPixel(x, y);
Pixel topLeftPixel = getPixel(x, y);
}
}
this.repaint();
}```
I just don't know what to do after this point because my teacher did not explain it clearly
7. Can you explain what the problem is with the code? What does it do now and what do you want it to do differently?

I still haven't seen your design for what you want the code to do. Post a grid with numbers for before and after the code executes to show what you want to do.
Last edited by NormR; October 23rd, 2013 at 12:57 PM.
8. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Oct 2013
Posts
7
Rep Power
0
There is no problem, I just don't know where to start on the method to make it copy diagonally.
9. where to start on the method to make it copy diagonally.
I still haven't seen your design for what you want the code to do. Post a grid with numbers for before and after the code executes to show what you want to do.
10. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Oct 2013
Posts
7
Rep Power
0
How do I upload a picture?
11. An image is too complicated and not clear enough. Just do something simple with a grid:
11121314
21222324
31323334
41424344
A 4x4 grid of 2 digit numbers: 11 to 44. What would it look like after the contents are moved diagonally?
12. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Oct 2013
Posts
7
Rep Power
0
Before:

1 2 3
4 5 6
7 8 9

After:

1 2 3
4 5 2
7 4 1
13. That example has 1, 2 and 4 in 2 places and 6, 8 and 9 are gone. Is that what you want?

Would a 5x5 grid would show it better?
Last edited by NormR; October 23rd, 2013 at 01:56 PM.
14. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Oct 2013
Posts
7
Rep Power
0
Yes, that is what I want
15. The code copies pixels, not swaps them.

Now look at the pattern for doing the copies. The diagonal is not used.
For example:
0,0 goes to 2,2
etc

A 5x5 grid might make it clearer

Once you have a design for copying the pixels:
For testing the logic, I'd make a small 2 dim int array, fill it with numbers and print it before the copy code using: Arrays.deepToString()
Execute the loops to make the copies
and print the results to see if the copies were done correctly.

When that works, copy the logic to the code that uses pixels.