Menu
![](/uploads/1/2/5/7/125759256/235923151.jpg)
Given a point (x1, y1) and an equation for a line (y=mx+c), I need some pseudocode for determining the point (x2, y2) that is a reflection of the first point across the line. Spent about an hour trying to figure it out with no luck!
(4) Reflection Across Any Line The Axis of Reflection is the line PQ shown in green in the applet below (You can place this green line anywhere you want for your homework) Move around any red point, or the green points P and Q, to see the reflection effect on the blue points.
See here for a visualization - http://www.analyzemath.com/Geometry/Reflection/Reflection.html
McGinMcGin
9 Answers
Ok, I'm going to give you a cookbook method to do this. Download naruto mkv. If you're interested in how I derived it, tell me and I'll explain it.
Given
(x1, y1)
and a line y = mx + c
we want the point (x2, y2)
reflected on the line.Set
d:= (x1 + (y1 - c)*m)/(1 + m^2)
Then
x2 = 2*d - x1
and
y2 = 2*d*m - y1 + 2*c
Il-BhimaIl-Bhima
This is a simple explanation of Il-Bhima's solution. The trick is to notice that what you want is to project that point orthogonally on the line, move it by that much, and then move it once again, in the same direction.
For these types of problems, it's easier to work with a slightly more redundant representation for a line. Instead of
y = m x + b
, let's represent the line by a point p
that is on the line and a vector d
in the line's direction. Let's call this point p = (0, b)
, the vector d = (1, m)
, and your input point will be p1
. The projected point on the line will be pl
and your output point p2
, then, is p1 + 2 * (pl - p1) = 2 * pl - p1
The formula you need here is the projection of a vector
v
onto a line which goes through the origin in direction d
. It is given by d * <v, d> / <d, d>
where <a, b>
is the dot product between two vectors.To find
pl
, we have to move the whole problem so that the line goes through the origin by subtracting p
from p1
, using the above formula, and moving it back. Then, pl = p + (d * <p - p1, d> / <d, d>)
, so pl_x = p_x + (b * p1_x) / (1 + m * m)
, pl_y = p_y + (m * p1_x) / (1 + m * m)
, and then use p2 = 2 * pl - p1
to get the final values.Carlos ScheideggerCarlos Scheidegger
With reference to the fig in here.
We want to find the reflection of the point
A(p,q)
to line L1,eqn y = m*x + c
. Say reflected point is A'(p',q')
Suppose,The line joining the points A and A' is L2 with eqn: y= m'*x + c'L1 & L2 intersect at M(a,b)
The algorithm for finding the reflection of the point is as follows:1) Find slope of L2 is = -1/m , as L1 and L2 are perpendicular2) Using the m' and A(x,y) find c' using eqn of L23) Find the intersection point 'M' of L1 anSd L2 4) As now we have coordinate of A and M so coordinate of A' can be easily obtained using the relation
[ A(p,q)+A'(p',q') ]/2 = M(a,b)
I haven't checked the following code but the crude form of code in the FORTRAN is
Yugal_kishoreYugal_kishore
Reflection can be found in two steps. First translate (shift) everything down by
b
units, so the point becomes V=(x,y-b)
and the line becomes y=mx
. Then a vector inside the line is L=(1,m)
. Now calculate the reflection by the line through the origin,where
V.L
and L.L
are dot product and *
is scalar multiple.Finally, shift everything back up by adding
b
, and the final answer is (x',y'+b)
.As an affine transformation you can write the above operation as the composition (product) of three matrices, first representing the shift
y => y-b
, then the reflection through the line through the origin, then the shift y => y+b
:The situation is very similar to rotation matrices in affine geometry. If you already have matrix multiplication routines available, because you're also doing rotations for example, this might be the most maintainable way of implementing reflections.
Edward DoolittleEdward Doolittle
I have a simpler and an easy way to implement in c++
}
akash mahajanakash mahajan
![Refction Refction](/uploads/1/2/5/7/125759256/592146037.jpg)
This link contains an algorithm that is similar to what you are trying to do:
That is reflect a ray off a normal.
C JohnsonC Johnson
Reflection of point in the line Given point P(x,y) and a line L1 Then P(X,Y) is the reflected point on the line L1If we join point P to P’ to get L2 then gradient of L2=1/m1 where m1 is gradient of L1L1 and L2 are perpendicular to each otherGet the point of intersection of L1 and L2 say m(a,b)Since m(a,b) is the midpoint of PP’ i.e. L2, thenM=
i.e. = from this we can get coordinates of ExampleFind the image of point P(4,3) under a reflection in the line
M1=1M2 will be -1Equ. L2 with points, (4,3) , (x ,y) grad -1 is
i.e. = from this we can get coordinates of ExampleFind the image of point P(4,3) under a reflection in the line
M1=1M2 will be -1Equ. L2 with points, (4,3) , (x ,y) grad -1 is
Point of intersection say, M(a ,b) Note that, L1 =L2 ; Then This gives the point for M that is M( 6,1) Then;
P'(x,y) = P'(8,-1)
demasakhwidemasakhwi
Reflection of point A(x,y) in the line y=mx+c.
Given point P(x,y) and a line L1 y=mx+c.
Then P(X,Y) is the reflected point on the line L1.
If we join point P to P’ to get L2 then gradient of L2=-1/m1 where m1 is gradient of L1.
Given point P(x,y) and a line L1 y=mx+c.
Then P(X,Y) is the reflected point on the line L1.
If we join point P to P’ to get L2 then gradient of L2=-1/m1 where m1 is gradient of L1.
Example
zadock Masakhwizadock Masakhwi
Find slope of the given line.Say it is m.So the slope of line joining the point and its mirror image is -1/m.Use slope point form to find equation of the line and find its interaection with given line. Finally use the intersection point in midpoint formula to get the required point.Regards,Shashank Deshpande
ShankyShanky
Not the answer you're looking for? Browse other questions tagged algorithmgeometry or ask your own question.
![](/uploads/1/2/5/7/125759256/235923151.jpg)