如何检查一个点是否低于一条线?

How can I check if a point is below a line or not ?(如何检查一个点是否低于一条线?)
本文介绍了如何检查一个点是否低于一条线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

如何检查点是否低于线?

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.

幸运的是,我发现计算二维向量的叉积.

这篇关于如何检查一个点是否低于一条线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)
Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)
Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)
Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)
Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)
Grouping pandas DataFrame by 10 minute intervals(按10分钟间隔对 pandas 数据帧进行分组)