Take the 2-minute tour ×
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.

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.

share|improve this question
1  
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$. –  J. M. 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
add comment

2 Answers

up vote 5 down vote accepted

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.

share|improve this answer
    
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
add comment

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.

share|improve this answer
    
The case where $p$ and $q$ form a vertical or horizontal line is easily trapped, though. –  J. M. Nov 23 '10 at 14:45
    
that's my point: how to compute coordinates of point b? –  guram Nov 23 '10 at 14:50
1  
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... –  J. M. Nov 23 '10 at 15:21
    
Yes, I see. Thank's ;) –  guram Nov 23 '10 at 15:43
add comment

Your Answer

 
discard

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.