How can I tell if a point belongs to a certain line?

前端 未结 11 2212
你的背包
你的背包 2020-12-01 08:22

How can I tell if a point belongs to a certain line?

Examples are appreciated, if possible.

11条回答
  •  鱼传尺愫
    2020-12-01 08:35

    Given two points on the line L0 and L1 and the point to test P.

                   (L1 - L0) * (P - L0)
    n = (P - L0) - --------------------- (L1 - L0)
                   (L1 - L0) * (L1 - L0)
    

    The norm of the vector n is the distance of the point P from the line through L0 and L1. If this distance is zero or small enough (in the case of rounding errors), the point lies on the line.

    The symbol * represents the dot product.

    Example

    P = (5, 5)
    
    L0 = (0, 10)
    L1 = (20, -10)
    
    L1 - L0 = (20, -20)
    P  - L0 = (5, -5)
    
                  (20, -20) * (5, -5)
    n = (5, -5) - --------------------- (20, -20)
                  (20, -20) * (20, -20)
    
                  200
      = (5, -5) - --- (20, -20)
                  800
    
      = (5, -5) - (5, -5)
    
      = (0, 0)
    

提交回复
热议问题