找到高线和底边相交的点(Python)

3

给定一个锐角三角形的顶点坐标,如何高效快速地找到从特定顶点引出的高度与对边相交的点的坐标?

仅使用数学、numpy或scipy的解决方案将非常有帮助。


2
你到目前为止尝试了什么,你的尝试有什么问题? - G. Anderson
我已经计算出了高的长度,但是在获取它与底边相交点的坐标时遇到了困难。我想知道是否有一种算法可以找到这些坐标。我正在尝试创建一个适用于任何三角形的通用解决方案。 - fume_hood_geologist
我认为我可以利用高度的长度和90度角来找到从高度与底边相交的点到一个顶点的线,然后利用该线的直角三角形的两条边来找到我的坐标。 - fume_hood_geologist
2个回答

5

所需点是顶点C在包含对边AB的直线上的正交投影点。

enter image description here

要找到投影点,请获取AB和AC的向量。

 AB = (B - A)    //in coordinates ab.x = b.x-a.x, ab.y = b.y-a.y
 AC = (C - A)

使用向量AB和AC的数量积找到参数

t =(AB * AC) / (AB * AB) 
t =((b.x-a.x)*(c.x-a.x) + (b.y-a.y)*(c.y-a.y)) / ((b.x-a.x)*(b.x-a.x) + (b.y-a.y)*(b.y-a.y))

投影点坐标
 P = A + AB * t
 p.x = a.x + (b.x-a.x) * t
 p.y = a.y + (b.y-a.y) * t

That's all

def orthoProjection(ax, ay, bx, by, cx, cy):
    abx = bx - ax
    aby = by - ay
    acx = cx - ax
    acy = cy - ay
    t = (abx * acx + aby * acy) / (abx * abx + aby * aby)
    px = ax + t * abx
    py = ay + t * aby
    return px, py

print(orthoProjection(0, 0, 4, 4, -1, 5))
>>(2.0, 2.0)

请问,您能否将解决方案编写为3D向量而不是2D? - FirstVertex
@FirstVertex 对于三维向量,向量数学是相同的,只是标量积将包含3个加和项(带有z坐标)。 - MBo
@MBo,感谢您开发这个公式。它在我的研究项目中帮了我很多忙。我希望我能给您100分。 - Asif Mehmood
这正是我想要的 :-) 非常感谢。现在,我已经将用作车辆定位系统研究中纠正技术的功能嵌入其中。我需要找到一篇发表的论文来引用这个公式,有人能提供这样的文章链接吗!@MBo - Asif Mehmood

2
考虑一个三角形,其顶点为点A、B和C,您希望找到从顶点C延伸的高度与线段AB相交的位置。
首先,您可以确定线段AB的方程式。您有点A和B(Ax、Ay和Bx、By)。鉴于此,您可以计算出斜率slope_AB,即(By-Ay)/(Bx-Ax)。
现在,线的格式是Y=MX+B,其中M是刚刚计算出的斜率,B是Y截距,因此:Y_intercept_AB = Ay-slope_AB*Ax。因此,AB的方程是Y=slope_AB*X+Y_intercept_AB。
好的,现在,从C到其与线段AB相交处(我们称其为点D和高度线CD)的高度的斜率是AB斜率的负倒数;因此,slope_CD = -(1/slope_AB)。

现在,假设你已经知道了线段CD上的一个点C和它的斜率,你可以像求线段AB的方程一样来求解CD的方程。首先,找到它的Y轴截距:Y_intercept_CD = Cy - slope_CD * Cx

因此,CD的方程为Y = slope_CD * X + Y_intercept_CD

现在你已经得到了线段AB和线段CD的方程:

Y = slope_AB * X + Y_intercept_AB
Y = slope_CD * X + Y_intercept_CD

你的问题简化为找到这些线相交的地方,也就是点D。

从上述方程中,由于两个右侧都等于Y,我们可以将它们设置为相等:

slope_AB * X + Y_intercept_AB = slope_CD * X + Y_intercept_CD

现在只需要解决X的问题。

slope_AB * X - slope_CD*X = Y_intercept_CD - Y_intercept_AB 
(slope_AB - slope_CD)*X = Y_intercept_CD - Y_intercept_AB
X = (Y_intercept_CD - Y_intercept_AB)/(slope_AB - slope_CD)

这将为您提供D的X值(Dx)。对于Y值,请使用任一条线方程。让我们使用AB的那个:

Dy = slope_AB * Dx + Y_intercept_AB

将所有内容放在一起,假设一个三角形 A=(-4, 2), B=(0, 6), C=(6, -4):

#Points A, B,C:
Ax = -4; Ay = 2
Bx =  0; By = 6
Cx =  6; Cy = -4

#Line AB:
slope_AB = (By - Ay)/(Bx - Ax)
Y_intercept_AB = Ay - slope_AB*Ax
print("AB: slope: %s, intercept: %s" % (slope_AB, Y_intercept_AB))

#Line CD:
slope_CD = -(1/slope_AB)
Y_intercept_CD = Cy - slope_CD*Cx
print("CD: slope: %s, intercept: %s" % (slope_CD, Y_intercept_CD))

#Find the intersection of the two lines AB & CD:
Dx = (Y_intercept_CD - Y_intercept_AB)/(slope_AB - slope_CD)
Dy = slope_AB*Dx + Y_intercept_AB
print("Intersection at (%s, %s)" % (Dx, Dy))

输出:

AB: slope: 1.0, intercept: 6.0
CD: slope: -1.0, intercept: 2.0
Intersection at (-2.0, 4.0)

还有一件事:如果点 A 和 B 具有相同的 X 值(因为它除以 Ax-Bx,而这将为零),则会导致除以零并失败;但这是一个开端。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接