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.
幸运的是,我发现计算二维向量的叉积.
这篇关于如何检查一个点是否低于一条线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何检查一个点是否低于一条线?
基础教程推荐
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
