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.
 A: 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.
A: 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.
A: Formulae for reflected points may be simplified by a judicious choice of points on the reflection line, $\ell:  Ax + By + C = 0$. Choosing intersections of this line with the axes (assuming $A \ne 0,\;B \ne 0$) use the position vectors, $${\bf{p}} = \left( {\begin{array}{*{20}{c}}{ - \frac{C}{A}}\\0\end{array}} \right),\;{\bf{q}} = \left( {\begin{array}{*{20}{c}}0\\{-\frac{C}{B}}\end{array}} \right)$$
Then ${\bf{x}} = \left( {{\bf{p}} - {\bf{q}}} \right)t$ is a vector in the direction of $\ell$. For an arbitrary position vector ${\bf{a}} = \left( {\begin{array}{*{20}{c}}{{a_x}}\\{{a_y}}\end{array}} \right)$ we require vectors ${\bf{x}},\;{\bf{x}} - ({\bf{q}} - {\bf{a}})$ to be orthogonal giving$${\bf{x}} \cdot ({\bf{x}} - ({\bf{q}} - {\bf{a}})) = 0.$$Solving this for ${\bf{x}}$ and noting that the reflection of point ${\bf{a}}$ in line $\ell$ is the position vector$${\bf{a'}} = 2{\bf{x}} + 2{\bf{q}} - {\bf{a}}$$
we can finally, after filling the waste paper basket, arrive at $${\bf{a'}} = \frac{1}{{{A^2} + {B^2}}}\left( {\begin{array}{*{20}{c}}{({B^2} - {A^2}){a_x} - 2AB{a_y} - 2AC}\\{({A^2} - {B^2}){a_y} - 2AB{a_x} - 2BC}\end{array}} \right).$$
