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

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.

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

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

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

相关文档推荐

build conda package from local python package(从本地 python 包构建 conda 包)
How can I see all packages that depend on a certain package with PIP?(如何使用 PIP 查看依赖于某个包的所有包?)
How to organize multiple python files into a single module without it behaving like a package?(如何将多个 python 文件组织到一个模块中而不像一个包一样?)
Check if requirements are up to date(检查要求是否是最新的)
How to upload new versions of project to PyPI with twine?(如何使用 twine 将新版本的项目上传到 PyPI?)
Why #egg=foo when pip-installing from git repo(为什么从 git repo 进行 pip 安装时 #egg=foo)