Python 求点到直线的垂足
在已知一个点,和一条已知两个点的直线的情况下
运算公式参考链接:https://www.cnblogs.com/mazhenyu/p/3508735.html
1 def getFootPoint(point, line_p1, line_p2): 2 """ 3 @point, line_p1, line_p2 : [x, y, z] 4 """ 5 x0 = point[0] 6 y0 = point[1] 7 z0 = point[2] 8 9 x1 = line_p1[0] 10 y1 = line_p1[1] 11 z1 = line_p1[2] 12 13 x2 = line_p2[0] 14 y2 = line_p2[1] 15 z2 = line_p2[2] 16 17 k = -((x1 - x0) * (x2 - x1) + (y1 - y0) * (y2 - y1) + (z1 - z0) * (z2 - z1)) / 18 ((x2 - x1) ** 2 + (y2 - y1) ** 2 + (z2 - z1) ** 2)*1.0 19 20 xn = k * (x2 - x1) + x1 21 yn = k * (y2 - y1) + y1 22 zn = k * (z2 - z1) + z1 23 24 return (xn, yn, zn)