I'm having trouble understanding the solution presented below (It's from a textbook). I tried to get something similar, but to no avail. Help me find the way he derived those a,b,x2 and y2 expressions.
Point p is a point which we want to reflect (It's a simple structure with two integers x and y representing coordinates on a 2d plane); (x0,y0) and (x1,y1) are coordinates of endpoints of a line. Expected result is a new point with reflected coordinates.
Point mirror(Point p, int x0, int y0, int x1, int y1)
{
double dx,dy,a,b;
long x2,y2;
Point p1; //reflected point to be returned
dx = (double) (x1 - x0);
dy = (double (y1 - y0);
a = (dx * dx - dy * dy) / (dx * dx + dy*dy);
b = 2 * dx * dy / (dx*dx + dy*dy);
x2 = Math.round(a * (p.x - x0) + b*(p.y - y0) + x0);
y2 = Math.round(b * (p.x - x0) - a*(p.y - y0) + y0);
p1 = Point((int)x2,(int)y2);
return p1;
}
p1
is the reflected point, but comments in the code would seriously have made things clearer. You still haven't answered my question of where you got this code... – J. M. ain't a mathematician Sep 18 '11 at 15:12p
, and it should also pass though the "reflected" point (why?) and the reflected point ought to have the same point-line distance asp
(why?)... – J. M. ain't a mathematician Sep 18 '11 at 15:20