# Point reflection across a line

Let's say that we have three points: $p = (x_p,y_p)$, $q = (x_q,y_q)$ and $a = (x_a,y_a)$. How can i find point $b$ which is reflection of $a$ across a line drawn through $p$ and $q$? I know it's simple to calculate, when we have $p$, $q$ etc. But I want to do this in my program, and I'm not sure, how to compute this.

OK, I've found solution by myself (but answers in this topic really helped me).

Suppose, that we have a line $Ax+By+C=0$, and $A^2+B^2 \not= 0$. $M (a,b)$ reflection across the line is point: $M' (\frac{aB^2-aA^2-2bAB-2AC}{A^2+B^2}, \frac{bA^2-bB^2-2aAB-2BC}{A^2+B^2})$

In my case, we don't have line, but only 2 points. How we can find $A,B,C$? It's simple:

Let's say, that $p=(p_x,p_y)$ and $q = (q_x,q_y)$. Line equation is: $(y-p_y)(q_x-p_x) - (q_y-p_y)(x-p_x) = 0$

After some calculations we have: $y(q_x-p_x) - x(q_y-p_y) - (p_y(q_x-p_x)+p_x(p_y-q_y)) = 0$

So: $A = p_y-q_y$, $B = q_x-p_x$ and $C = -p_y(q_x-p_x)-p_x(p_y-q_y)$.

That's all.

-
Find the slope of the segment through $p$ and $q$, get the slope of the perpendicular, construct the equation of the line that passes through $a$, parametrize such that $t=0$ is the intersection point and $t=1$ corresponds to $a$, and find the point corresponding to $t=-1$. –  Ｊ. Ｍ. Nov 23 '10 at 14:28
Do you mean that $b$ is the reflection of $a$ w.r.t the line? (Maybe edit the question a little.) –  J. J. Nov 23 '10 at 14:28

It is useful to think about this by using vectors. Suppose that your points are $p,q,a \in \mathbb{R^2}$.

Then $x = p + (q-p)t$ is a point on the line $pq$. We wish to find the projection of $a$ on the line. To do this we require $\|x - a\|^2 = \|x\|^2 + \|a\|^2 - 2 (x \cdot a)$ to be minimal. That is we have to minimize $$\|p + (q-p)t\|^2 + \|a\|^2 - 2 (p + (q-p) t) \cdot a)$$ w.r.t $t$. To do this we write this as $$\|p\|^2 + \|q-p\|^2 t^2 + 2 t p \cdot (q-p) + \|a\|^2 - 2(p \cdot a) - 2 t (q-p) \cdot a.$$ This is a quadratic in $t$ with minimum at the tip of the parabola: $$t = \frac{-2 p \cdot (q-p) + 2 (q-p) \cdot a}{2\|q-p\|^2} = \frac{(q-p) \cdot (a-p)}{\|q-p\|^2}.$$

Thus the projection is given by $$x = p + (q-p) \frac{(q-p) \cdot (a-p)}{\|q-p\|^2}$$ and the reflection is then just $x + (x-a) = 2x-a$.

This method doesn't have problems with infinite slopes.

-
That's interesting method, but I'm not sure, how can I use it, to calculate coordinates of point b (I need this to use in computer program)? –  guram Nov 23 '10 at 17:39
To calculate the coordinates of $b$ you just first calculate $\frac{(q-p) \cdot (a-p)}{\|q-p\|^2} = \frac{(q_x - p_x)(a_x - p_x) + (q_y - p_y)(a_y - p_y)}{(q_x-p_x)^2 + (q_y-p_y)^2}$. Let's call this number $C$. Then you can calculate $b_x = 2p_x + (q_x - p_x)C$ and $b_y = 2p_y + (q_y - p_y)C$. –  J. J. Nov 23 '10 at 18:25
Thank You! This is what I need. –  guram Nov 23 '10 at 19:11
Wait... I'm not sure, if this works right, because it returns wrong results (or I do some mistakes...) –  guram Nov 23 '10 at 20:43
I did a small example by hand and I got the right result at least in that case.. (I had p = (2,3), q = (6,5), a = (5,2) and result b = (3,6).) –  J. J. Nov 24 '10 at 5:50

You find the equation of the line through p and q, one version of which is $$\frac{y-y_p}{x-x_p}=\frac{y_q-y_p}{x_q-x_p}$$ The slope is the RHS. So the slope of the line through a and perpendicular to the line through p and q is $$\frac{-(x_q-x_p)}{y_q-y_p}$$ and the line is $$y-y_a=\frac{-(x_q-x_p)}{y_q-y_p}(x-x_a)$$ Calculate the distance from a to the line through p and q and extend that far again. I think there is a formula for this, but don't know it, so I would have to solve the two equations for x and y to get the intersection. Note that there is a divide by zero concern.

-
The case where $p$ and $q$ form a vertical or horizontal line is easily trapped, though. –  Ｊ. Ｍ. Nov 23 '10 at 14:45
that's my point: how to compute coordinates of point b? –  guram Nov 23 '10 at 14:50
If you solve the first and third equations for $x$ and $y$, that will find the intersection of the line through (p and q) and the line through a that is perpendicular to it. Call this point $(x_c,y_c)$ Then $x_b=2x_c-x_a$ and similarly for y. Do you see why? –  Ross Millikan Nov 23 '10 at 14:56
Just so you can visualize the geometry clearly, imagine that the two perpendicular lines intersect at the origin... –  Ｊ. Ｍ. Nov 23 '10 at 15:21
Yes, I see. Thank's ;) –  guram Nov 23 '10 at 15:43