gpt4 book ai didi

python-3.x - 如何识别质心点是否接触到一条线?

转载 作者:行者123 更新时间:2023-12-02 16:16:31 25 4
gpt4 key购买 nike

我正在使用一种基于越线检测的入侵检测算法。我已经使用等式 y = mx+c 开发了一个基本算法,但是当人靠近线时它显示出一些错误的检测。我需要一些建议来使其成为完美的线接触算法。

enter image description here

enter image description here

最佳答案

如果您的直线有起点和终点[x1, y1][x2, y2],则直线方程为:

y - y1 = m * (x - x1),其中 m = (y2 - y1)/(x2-x1)

然后您可以检查一个点是否属于直线,替换 xy,并检查另一个是否与直线方程匹配。

在 Pyhton 中:

# the two points that define the line
p1 = [1, 6]
p2 = [3, 2]

# extract x's and y's, just for an easy code reading
x1, y1 = p1
x2, y2 = p2

m = (y2-y1)/(x2-x1)

# your centroid
centroid = [2,4]
x3, y3 = centroid

# check if centroid belongs to the line
if (m * (x3-x1) + y1) == y3:
print("Centroid belongs to line")

但可能...

...计算红点和线 ( distance from a point to a line ) 之间的距离,然后检查它是否足够近(即距离小于某个值),您会得到更好的结果。

在 Python 中:

# points that define the line
p1 = [1, 6]
p2 = [3, 2]
x1, y1 = p1
x2, y2 = p2

centroid = [2,4]
x3, y3 = centroid

# distance from centroid to line
import math # to calculate square root
dist = abs((y2-y1)*x3 - (x2-x1)*y3 + x2*y1 - y2*x1)/math.sqrt((y2-y1)**2 + (x2-x1)**2)

if dist < some_value:
print("Near enough")

关于python-3.x - 如何识别质心点是否接触到一条线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60718288/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com