Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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;

share|cite|improve this question
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. Sep 18 '11 at 15:07
I thought it was intuitive. I'll edit my question – iccthedral 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. 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
up vote 3 down vote accepted

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 :-(

enter image description here

share|cite|improve this answer
Thank you very much for your time and effort. – iccthedral Sep 18 '11 at 17:20
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 ? – iccthedral 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

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.