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;

}
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}'&amp;=\frac1N\left(N(p.x-x_0)-2dy^2(p.x-x_0)+2dy\,dx(p.y-y_0)\right)\vec{i}\\
&amp;+\frac1N\left(N(p.y-y_0)+2dy\,dx(p.x-x_0)-2dx^2(p.y-y_0)\right)\vec{j}\\
&amp;=\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 :-(
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;
  }