如何检查点是否低于线?
How can I check if a point is below a line or not ?
我有以下数据:
Line [ {x1,y1}, {x2,y2} ]
Points {xA,yA}, {xB,yB} ...
我需要在python中编写一个小算法来检测线的一侧和另一侧的点.
I need to write a small algorithm in python to detect points on one side and the other side of the line.
谢谢
你可以尝试使用交叉产品 -- http://en.wikipedia.org/wiki/Cross_product.
You could try using a cross product -- http://en.wikipedia.org/wiki/Cross_product.
v1 = (x2-x1, y2-y1) # Vector 1
v2 = (x2-xA, y2-yA) # Vector 2
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!')
您需要校准每一面的内容.如果您希望它位于下方";或以上"您需要确保线上的点是水平排序的.
You'd need to calibrate what each side is. If you want it to be "below" or "above" you need to ensure the points on the line are sorted horizontally.
我还没有测试过.
编辑我最初输入了点积公式.:o
Edit I initially put in the dot product formula. :o
编辑 2 哦,我将坐标放入集合而不是元组.如果您运行的是相当现代的 Python 版本,则对向量使用 namedtuple('point', 'x y')
会很好.
Edit 2 D'oh, I was putting the coordinates into a set instead of a tuple. Using namedtuple('point', 'x y')
for the vectors is nice if you're running a reasonably modern version of Python.
幸运的是,我发现计算二维向量的叉积.
这篇关于如何检查一个点是否低于一条线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!