我怎样才能检查一个点是否在线以下? [英] How can I check if a point is below a line or not ?

查看:135
本文介绍了我怎样才能检查一个点是否在线以下?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有以下数据:

  Line [{x1,y1},{x2,y2}] 
Points {xA,yA},{xB,yB} ...

我需要在python中编写一个小算法来检测行的一侧和另一侧的点。 p>

谢谢

解决方案

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

  v1 = {x2-x1,y2-y1}#Vector 1 
v2 = {x2-xA,y2-yA}#Vector 1
xp = v1.x * v2.y - v1.y * v2.x#跨产品
if xp> 0:
打印'一边'
elif xp< 0:
'在另一个'上打印'
else:
print''在同一行!'

您需要校准每个方面。如果您希望它位于下方或上方,您需要确保线上的点水平排序。



我没有测试过这个。

p>

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



幸运的是,我发现计算一个二维矢量的交叉产品


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

解决方案

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 1
xp = v1.x*v2.y - v1.y*v2.x  # 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.

I haven't tested this.

Edit I initially put in the dot product formula. :o

Luckily I found Calculating a 2D Vector's Cross Product.

这篇关于我怎样才能检查一个点是否在线以下?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆