如何检查一个点是否在线下方?

发布于 2024-09-25 20:29:40 字数 170 浏览 5 评论 0原文

如何检查一个点是否在线下方?

我有以下数据:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

甜扑 2024-10-02 20:29:40

您可以尝试使用交叉产品 - 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 (magnitude)
if xp > 0:
    print('on one side')
elif xp < 0:
    print('on the other')
else:
    print('on the same line!')

您需要校准每一侧的内容。如果您希望它位于“下方”或“上方”,您需要确保线上的点水平排序。

我没有测试过这个。

编辑我最初输入了点积公式。 :o

编辑 2 天哪,我将坐标放入集合而不是元组中。如果您运行的是相当现代的 Python 版本,那么对向量使用 namedtuple('point', 'x y') 会很好。

幸运的是,我找到了计算 2D 矢量叉积

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 (magnitude)
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.

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.

离笑几人歌 2024-10-02 20:29:40

你可以尝试使用叉积,但技巧是如何选择点来形成向量,这里我选择点中最近的点,假设我得到了点A(你可以公平地循环点来计算从循环点到线的距离):

v1 = {x2-x1, y2-y1}   # Vector 1
v2 = {xA-x1, yA-y1}   # Vector 2
cross_product = v1.x*v2.y - v1.y*v2.x
if cross_product > 0:
    print 'pointA is on the counter-clockwise side of line'
elif cross_product < 0:
    print 'pointA is on the clockwise side of line'
else:
    print 'pointA is exactly on the line'

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):

v1 = {x2-x1, y2-y1}   # Vector 1
v2 = {xA-x1, yA-y1}   # Vector 2
cross_product = v1.x*v2.y - v1.y*v2.x
if cross_product > 0:
    print 'pointA is on the counter-clockwise side of line'
elif cross_product < 0:
    print 'pointA is on the clockwise side of line'
else:
    print 'pointA is exactly on the line'
你与清晨阳光 2024-10-02 20:29:40

假设您给出了 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文