# Point reflection over a line

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;

}

• Which textbook? Did the textbook at least say what the inputs of that function were supposed to be, and what the output is supposed to mean? – J. M. is a poor mathematician Sep 18 '11 at 15:07
• I thought it was intuitive. I'll edit my question – nullpotent Sep 18 '11 at 15:08
• Um, not to be a grinch here, but ... less groveling, please? It is kinda tedious to read through the I'm-not-worthy boilerplate in order to locate your actual question. – Henning Makholm Sep 18 '11 at 15:11
• Anyway, another way to think of it: your $(x_0,y_0)$ and $(x_1,y_1)$ form a line segment. One could consider a perpendicular line segment passing through p, and it should also pass though the "reflected" point (why?) and the reflected point ought to have the same point-line distance as p (why?)... – J. M. is a poor mathematician Sep 18 '11 at 15:20
• It might help to note that $a + ib = (dx + i\cdot dy)^2 / |dx + i\cdot dy|^2$, and indeed that the author of this code is making quite heavy use of complex numbers. Draw yourself a diagram and have a look at what x2 and y2 do. Sorry I don't have time to elaborate more... – Billy Sep 18 '11 at 15:22

Here's an explanation. It is easier to follow, if you draw a picture. Let $\vec{u}=(dx,dy)$ be the vector from the point $P_0=(x_0,y_0)$ to the point $P_1=(x_1,y_1)$, i.e. a vector pointing in the direction of the mirror line. Then $\vec{n}=(-dy,dx)$ is perpendicular to it. Let's name the vector from $P_0$ to $P$ $\vec{v}=(p.x-x_0,p.y-y_0)$. The projection of the vector $\vec{v}$ along the normal $\vec{n}$ is then $$\vec{p}=\frac{\vec{n}\cdot\vec{v}}{\vec{n}\cdot\vec{n}}\vec{n}=\frac{-(p.x-x_0)dy+(p.y-y_0)dx}{dx^2+dy^2}\vec{n}.$$

We compute the mirror image point $P'=(x_2,y_2)$ by comparing the representations of the vectors $\vec{P_0P}=\vec{v}$ and $\vec{P_0P'}=\vec{v'}$ in the (orthogonal) basis $\{\vec{u},\vec{n}\}$. The reflection maps $\vec{u}$ to itself, but the vector perpendicular to mirror is mapped to its negative: $\vec{n}\mapsto -\vec{n}$. Therefore the reflection maps $$\vec{v}\mapsto\vec{v}'=\vec{v}-2\vec{p}.$$ The rest amounts to just plugging in the numbers. Write $N=dx^2+dy^2$ for short. We get \begin{align} \vec{v}'&=\frac1N\left(N(p.x-x_0)-2dy^2(p.x-x_0)+2dy\,dx(p.y-y_0)\right)\vec{i}\\ &+\frac1N\left(N(p.y-y_0)+2dy\,dx(p.x-x_0)-2dx^2(p.y-y_0)\right)\vec{j}\\ &=\left(a(p.x-x_0)+b(p.y-y_0),b(p.x-x_0)-a(p.y-y_0)\right). \end{align} Your formula comes from the fact that the above vector $\vec{v'}$ is the separation vector from $P_0$ to $P'$. Therefore we need to add the coordinates of $P_0$ to the result.

Edit: A crude image here. The points $P,P_0,P_1,P'$ are marked with the dots, and the vectors are $u,n,v,p,v',-2p$. Sorry about the missing arrows on top of those letters - can't do any better :-(

• Sorry, I missed that @J.M. had already provided a link in a comment that was "out of sight". Oh, well. – Jyrki Lahtonen Sep 18 '11 at 18:43
• What software did you used to draw the above image ? – nullpotent Sep 18 '11 at 18:57
• @Aljosha: An ancient version of Mathematica. I guess that my EPS to PNG conversion must also take some of the blame :-) – Jyrki Lahtonen Sep 18 '11 at 19:00
• I must be missing something, where do $a$ and $b$ come from after "plugging in the numbers"? – Vegard Feb 5 '17 at 13:24
• @Vegard: Just combine the terms. For example in the $\vec{i}$-component you get $(N-2dy^2)/N=a$ for the coefficient of $(p.x-x_0)$. – Jyrki Lahtonen Feb 5 '17 at 13:30

It worked for me in C++. Where p is the point to reflect, and P0(x0, y0) P1(x1, y1) is the mirror line, and p1 is the point reflected. See the graph again


struct Point{
int x;
int y;
};

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  = round(a * (p.x - x0) + b*(p.y - y0) + x0);
y2  = round(b * (p.x - x0) - a*(p.y - y0) + y0);

p1.x = (int)x2;
p1.y = (int)y2;

return p1;
}