如何检查一个点是否在线下方?
如何检查一个点是否在线下方?
我有以下数据:
Line [ {x1,y1}, {x2,y2} ]
Points {xA,yA}, {xB,yB} ...
我需要在 python 中编写一个小算法来检测线一侧和另一侧的点。
谢谢
How can I check if a point is below a line or not ?
I've the following data:
Line [ {x1,y1}, {x2,y2} ]
Points {xA,yA}, {xB,yB} ...
I need to write a small algorithm in python to detect points on one side and the other side of the line.
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以尝试使用交叉产品 - http://en.wikipedia.org/wiki/Cross_product。
您需要校准每一侧的内容。如果您希望它位于“下方”或“上方”,您需要确保线上的点水平排序。
我没有测试过这个。
编辑我最初输入了点积公式。 :o
编辑 2 天哪,我将坐标放入集合而不是元组中。如果您运行的是相当现代的 Python 版本,那么对向量使用
namedtuple('point', 'x y')
会很好。幸运的是,我找到了计算 2D 矢量叉积。
You could try using a cross product -- http://en.wikipedia.org/wiki/Cross_product.
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.
I haven't tested this.
Edit I initially put in the dot product formula. :o
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.Luckily I found Calculating a 2D Vector's Cross Product.
你可以尝试使用叉积,但技巧是如何选择点来形成向量,这里我选择点中最近的点,假设我得到了点A(你可以公平地循环点来计算从循环点到线的距离):
You could try using a cross product, but the trick is how to choose the point to form a vector, here I choose the closest point from points, assume I got pointA(you can fairly loop points to calculate to distance from loop point to Line):
假设您给出了 2 个点 A、B,并且您想知道在哪个 halfplane 第三点C所在。作为标准的术语“下方”和“上方”非常模糊,因此您需要一个参考点,例如原点。只要确保该参考点不与 A 和 B 共线即可。
您现在拥有的是一个三角形(A、B、C)。使用行列式,您可以计算签名面积 (请参阅此处,或此处)。这里唯一有趣的是记住这个标志。
下一步:对于给定点 D 计算三角形的有符号面积 (A, B, D)。如果结果与参考三角形的面积具有相同的符号 -> C和D位于(A,B)的同一侧。如果符号不同-> C 和 D 位于线的两侧。如果 (A, B, D) 的面积为 0,则 A、B 和 D 共线。注意:使用Python内置
cmp
来比较三角形面积。Lets say you've given 2 points A, B and you want to know in which halfplane a third point C lies. The terms "below" and "above" as criteria are very vague, so you need a reference point, for example the origin. Just be sure this reference point is not collinear with A and B.
What you have now is a triangle (A, B, C). Using the determinant you can calculate the signed area (see here, or here). The only interesting thing here is to remember the sign.
Next step: for a given point D calculate the signed area of the triangle (A, B, D). If the result has the same sign as the area of your reference triangle -> C and D are on the same side of (A, B). If the sign differs -> C and D lie on opposite sides of the line. If the area of (A, B, D) is 0 then A, B and D are collinear. Note: use the Python builtin
cmp
to compare the triangle areas.