Distance between point and a line (from two points)

后端 未结 8 2378
梦谈多话
梦谈多话 2020-11-28 07:30

I\'m using Python+Numpy (can maybe also use Scipy) and have three 2D points

(P1, P2, P3); 

I am trying to get the distance from P3 perpend

8条回答
  •  天涯浪人
    2020-11-28 07:39

    Based on the accepted answer

    Test with below line equation -

    Find the perpendicular distance from the point (5, 6) to the line −2x + 3y + 4 = 0

    • x-intercept p1 = [0, -4/3]
    • y-intercept p2 = [2, 0]
    • shortest distance from p3 = [5, 6] = 3.328
    import numpy as np
    norm = np.linalg.norm
    
    p1 = np.array([0,-4/3])
    p2 = np.array([2, 0])
    
    p3 = np.array([5, 6])
    d = np.abs(norm(np.cross(p2-p1, p1-p3)))/norm(p2-p1)
    # output d = 3.328201177351375
    
    

提交回复
热议问题