【面】计算点到直线的距离

1
2
3
4
5
6
7
8
9
10
11
import math

def getDis(pointX, pointY, lineX1, lineY1, lineX2, lineY2):
a = lineY2 - lineY1
b = lineX1 - lineX2
c = lineX2*lineY1 - lineX1*lineY2
dis = (math.fabs(a*pointX + b*pointY + c))/(math.pow(a*a + b*b, 0.5))
return dis

print(getDis(1, 0, 0, 0, 0, 5))
print(getDis(1, 0, 0, 0, 1, 0.5))
0%