I'm quite new to Java and I'm using the acm graphics package to learn. I have several .drawLine(double x, double y)
methods and I use them to draw a mushroom. The mushroom is the identical on the left and right side but inverted. I wanted to know how can I (also if it would be correct) reflect my coords over the y-axis. Mathematically we can do this by multipling all x values by -1. This way I don't have to re-write the entire code for the right side. I've been reasearching but I haven't found anything that has worked. I've tried java reflection and have got nowhere. Also am I approching this the wrong way? I'm open to all suggestions that help me understand this concept.
Here's an example of my code:
GPen black = new GPen(); //Make new pen
add(black, 270, 30); //Add it at these coords
black.drawLine(0, 30); //Draw a box
black.drawLine(-40, 0);
black.drawLine(0, -30);
black.drawLine(40, 0);
Could I iterate throught these, multiply the x values by -1, and have it run the method again?
"Mathematically we can do this by multipling all x values by -1" - here lies your problem. This gives you a reflection in the y-axis, but not about a particular vertical line. To reflect about a particular vertical line, say the line x=270 as in your example:
To reflect the point (p, q) where p<270
Distance from line of reflection = 270-p
So new coordinate are: (270 + d, q)
Which is equal to: (540 - p, q)
So in general the formula to reflect the point (p,q) in the line x = A is (2A - p, q)
Edit: An alternative approach you could try is this:
GPen black = new GPen(); //Make new pen
int[] x_coords = new int[4] {0,-40,0,40};
int[] y_coords = new int[4] {30,0,-30,0};
add(black, 270, 30); //Add it at these coords
for (int i=0; i<x_coords.length; i++){
black.drawLine( -1*x_coords[i] , y_coords[i]);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.