Distance between point and a line (from two points)

后端 未结 8 2252
梦谈多话
梦谈多话 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:56

    Try using the norm function from numpy.linalg

    d = norm(np.cross(p2-p1, p1-p3))/norm(p2-p1)
    
    0 讨论(0)
  • 2020-11-28 08:00

    For the above-mentioned answers to work, the points need to be numpy arrays, here's a working example:

    import numpy as np
    p1=np.array([0,0])
    p2=np.array([10,10])
    p3=np.array([5,7])
    d=np.cross(p2-p1,p3-p1)/np.linalg.norm(p2-p1)
    
    0 讨论(0)
提交回复
热议问题