Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
October 22nd, 2013, 01:51 PM
 DelleVelleD
Registered User

Join Date: Oct 2013
Posts: 7
Time spent in forums: 1 h 51 m 12 sec
Reputation Power: 0
Homework - 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
October 22nd, 2013, 02:02 PM
 NormR
Contributing User

Join Date: Aug 2010
Location: SW Missouri
Posts: 3,583
Time spent in forums: 2 Weeks 3 Days 3 h 2 m 40 sec
Reputation Power: 347
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
October 22nd, 2013, 06:53 PM
 djslavens
Registered User

Join Date: Sep 2013
Posts: 8
Time spent in forums: 2 h 6 m 44 sec
Reputation Power: 0
What happens when you run your code?

#4
October 23rd, 2013, 08:31 AM
 DelleVelleD
Registered User

Join Date: Oct 2013
Posts: 7
Time spent in forums: 1 h 51 m 12 sec
Reputation Power: 0
I need help on how to write the code.

#5
October 23rd, 2013, 08:36 AM
 NormR
Contributing User

Join Date: Aug 2010
Location: SW Missouri
Posts: 3,583
Time spent in forums: 2 Weeks 3 Days 3 h 2 m 40 sec
Reputation Power: 347
Quote:
 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
October 23rd, 2013, 12:41 PM
 DelleVelleD
Registered User

Join Date: Oct 2013
Posts: 7
Time spent in forums: 1 h 51 m 12 sec
Reputation 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
October 23rd, 2013, 12:44 PM
 NormR
Contributing User

Join Date: Aug 2010
Location: SW Missouri
Posts: 3,583
Time spent in forums: 2 Weeks 3 Days 3 h 2 m 40 sec
Reputation Power: 347
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
October 23rd, 2013, 12:55 PM
 DelleVelleD
Registered User

Join Date: Oct 2013
Posts: 7
Time spent in forums: 1 h 51 m 12 sec
Reputation Power: 0
There is no problem, I just don't know where to start on the method to make it copy diagonally.

#9
October 23rd, 2013, 12:58 PM
 NormR
Contributing User

Join Date: Aug 2010
Location: SW Missouri
Posts: 3,583
Time spent in forums: 2 Weeks 3 Days 3 h 2 m 40 sec
Reputation Power: 347
Quote:
 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
October 23rd, 2013, 01:14 PM
 DelleVelleD
Registered User

Join Date: Oct 2013
Posts: 7
Time spent in forums: 1 h 51 m 12 sec
Reputation Power: 0
How do I upload a picture?

#11
October 23rd, 2013, 01:17 PM
 NormR
Contributing User

Join Date: Aug 2010
Location: SW Missouri
Posts: 3,583
Time spent in forums: 2 Weeks 3 Days 3 h 2 m 40 sec
Reputation Power: 347
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
October 23rd, 2013, 01:34 PM
 DelleVelleD
Registered User

Join Date: Oct 2013
Posts: 7
Time spent in forums: 1 h 51 m 12 sec
Reputation Power: 0
Before:

1 2 3
4 5 6
7 8 9

After:

1 2 3
4 5 2
7 4 1

#13
October 23rd, 2013, 01:53 PM
 NormR
Contributing User

Join Date: Aug 2010
Location: SW Missouri
Posts: 3,583
Time spent in forums: 2 Weeks 3 Days 3 h 2 m 40 sec
Reputation Power: 347
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
October 23rd, 2013, 01:59 PM
 DelleVelleD
Registered User

Join Date: Oct 2013
Posts: 7
Time spent in forums: 1 h 51 m 12 sec
Reputation Power: 0
Yes, that is what I want

#15
October 23rd, 2013, 02:09 PM
 NormR
Contributing User

Join Date: Aug 2010
Location: SW Missouri
Posts: 3,583
Time spent in forums: 2 Weeks 3 Days 3 h 2 m 40 sec
Reputation Power: 347
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.

 Viewing: Dev Shed Forums > Programming Languages > Java Help > Homework - Need help on how to copy a picture diagonally!