0
$\begingroup$

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;

}
  • 0
    *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?2011-09-18
  • 0
    I thought it was intuitive. I'll edit my question2011-09-18
  • 2
    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.2011-09-18
  • 0
    Not sure about "intuitive". I'd guess from what you wish to do that $(x_0,y_0)$ and $(x_1,y_1)$ define the line of reflection, and `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...2011-09-18
  • 0
    I apologize, I'm the one in seek for help; I understand. I edited my question and I hope I improved it a bit. Author of the textbook is my Uni professor, and he didn't left comments nor any hints for this particular implementation.2011-09-18
  • 0
    There is in any event a vectorial derivation [here](http://mathworld.wolfram.com/Reflection.html).2011-09-18
  • 1
    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](http://mathworld.wolfram.com/Point-LineDistance2-Dimensional.html) as `p` (why?)...2011-09-18
  • 1
    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...2011-09-18
  • 0
    See [here](https://math.stackexchange.com/q/13261/11619) for older discussion (and better quality images).2017-06-12

2 Answers 2

5

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

  • 0
    Sorry, I missed that @J.M. had already provided a link in a comment that was "out of sight". Oh, well.2011-09-18
  • 0
    What software did you used to draw the above image ?2011-09-18
  • 0
    @Aljosha: An ancient version of Mathematica. I guess that my EPS to PNG conversion must also take some of the blame :-)2011-09-18
  • 0
    I must be missing something, where do $a$ and $b$ come from after "plugging in the numbers"?2017-02-05
  • 0
    @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)$.2017-02-05
  • 0
    @JyrkiLahtonen Ah, thanks, I was just confused about the fact they didn't appear anywhere else, but I see you're just factoring out the common expressions.2017-02-05
-1

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;
  }