Algorithm for reflecting a point across a line

前端 未结 9 1965
感动是毒
感动是毒 2020-12-03 03:02

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

9条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-03 03:43

    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
    

提交回复
热议问题