如何检查点是否在线下?

ane*_*yzm 8 python math geometry line

如何检查点是否在线下?

我有以下数据:

Line [ {x1,y1}, {x2,y2} ]
Points {xA,yA}, {xB,yB} ...
Run Code Online (Sandbox Code Playgroud)

我需要在python中编写一个小算法来检测线的一侧和另一侧的点.

谢谢

Edm*_*und 7

您可以尝试使用跨产品 - http://en.wikipedia.org/wiki/Cross_product.

v1 = (x2-x1, y2-y1)   # Vector 1
v2 = (x2-xA, y2-yA)   # Vector 1
xp = v1[0]*v2[1] - v1[1]*v2[0]  # Cross product
if xp > 0:
    print 'on one side'
elif xp < 0:
    print 'on the other'
else:
    print 'on the same line!'
Run Code Online (Sandbox Code Playgroud)

您需要校准每个方面的内容.如果您希望它在"下方"或"上方",则需要确保线上的点水平排序.

我没有测试过这个.

编辑我最初放入点积公式.:○

幸运的是我发现了计算2D矢量的交叉积.