Algorithm for reflecting a point across a line

2019-01-11 10:45发布

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!

See here for a visualization - http://www.analyzemath.com/Geometry/Reflection/Reflection.html

9条回答
劳资没心,怎么记你
2楼-- · 2019-01-11 11:03

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 L1 If we join point P to P’ to get L2 then gradient of L2=1/m1 where m1 is gradient of L1 L1 and L2 are perpendicular to each other Get the point of intersection of L1 and L2 say m(a,b) Since m(a,b) is the midpoint of PP’ i.e. L2, then M=
i.e. = from this we can get coordinates of Example Find the image of point P(4,3) under a reflection in the line
M1=1 M2 will be -1 Equ. 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;

          This gives x = 8 and y = -1 hence,

P'(x,y) = P'(8,-1)

查看更多
聊天终结者
3楼-- · 2019-01-11 11:08

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 perpendicular 2) Using the m' and A(x,y) find c' using eqn of L2 3) 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

SUBROUTINE REFLECTION(R,p,q)

IMPLICIT NONE

REAL,INTENT(IN)     ::  p,q

REAL, INTENT(OUT)   ::  R(2)

REAL                ::  M1,M2,C1,C2,a,b

M2=-1./M1                       ! CALCULATE THE SLOPE OF THE LINE L2 

C2=S(3,1)-M2*S(3,2)             ! CALCULATE THE 'C' OF THE LINE L2  

q= (M2*C1-M1*C2)/(M2-M1)        ! CALCULATE THE MID POINT O

p= (q-C1)/M1

R(1)=2*a-p                      ! GIVE BACK THE REFLECTION POINTS COORDINATE

R(2)=2*b-q

END SUBROUTINE REFLECTION
查看更多
时光不老,我们不散
4楼-- · 2019-01-11 11:10

Ok, I'm going to give you a cookbook method to do this. 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

查看更多
不美不萌又怎样
5楼-- · 2019-01-11 11:13

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

查看更多
成全新的幸福
6楼-- · 2019-01-11 11:15

I have a simpler and an easy way to implement in c++

#include<graphics.h>
#include<iostream>
#include<conio.h>
using namespace std;

int main(){
cout<<"Enter the point";
float x,y;
int gm,gd=DETECT;
initgraph(&gd,&gm,"C:\\TC\\BGI");

cin>>x;
cin>>y;
putpixel(x,y,RED);
cout<<"Enter the line slop and intercept";
float a,c;
cin>>a;
cin>>c;
float x1,y1;
x1 = x-((2*a*x+2*c-y)/(1+a*a))*a;
y1=(2*a*x+2*c-y)/(1+a*a);
cout<<"x = "<<x1;
cout<<"y = "<<y1;

putpixel(x1,y1,BLUE);
getch();

}

查看更多
冷血范
7楼-- · 2019-01-11 11:16

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.

  L1 and L2 are perpendicular to each other.
        therefore,
    Get the point of intersection of L1 and L2 say m(a,b)
    Since m(a,b) is the midpoint of PP’ i.e. L2, then
    M= (A+A')/2 
    i.e. m(a,b)=(A(x,y)+ A^' (x^',y^' ))/2.
      from this we can get coordinates of  A^' (x^',y^' )

Example

Find the image of point P(4,3) under a reflection in the line  y=x-5
M1=1
M2 will be -1
Equ. L2 with points, (4,3) , (x ,y) grad -1 is
     y=-x+7
Point of intersection say, M(a ,b)
 Note that, L1 =L2 ;
  Then x-5=-x+7
  This gives the point for M that is M( 6,1)
      Then;
         M(6,1)=(P(4,3)+P^' (x^',y^' ))/2
          M(6,1)=[(4+x)/2  ,(3+y)/2]
              This gives x = 8 and y = -1 hence,
                  P^' (x^',y^' )=         P^' (8,-1)
查看更多
登录 后发表回答