简体   繁体   English

查找海拔与基准相交的点(Python)

[英]Find point where altitude meets base (Python)

Given only the coordinates of the vertices of an acute triangle, how can I efficiently and quickly find the coordinates of the point at which the altitude from a particular vertex meets the opposite base? 仅给出锐角三角形顶点的坐标,我如何高效而快速地找到特定顶点的高度与相反的底点相交的点的坐标?

A solution using only math, numpy, or scipy would be incredibly helpful. 仅使用数学,numpy或scipy的解决方案将非常有用。

Consider the triangle with vertices at points A, B and C, and you wish to find where the altitude extending from vertex C intersects the line AB. 考虑在顶点A,B和C处具有顶点的三角形,您希望找到从顶点C延伸的高度与直线AB相交的位置。

So first, you can determine the equation for line AB. 因此,首先,您可以确定AB线的方程式。 You have points A and B ( Ax, Ay ; and Bx, By ). 您有点A和B( Ax, Ay ;和Bx, By )。 Given that you can calculate the slope_AB as (By-Ay)/(Bx-Ax) . 假设您可以将lope_AB计算为(By-Ay)/(Bx-Ax)

Now the format of a line is Y = MX+B where M is the slope just calculated, and B is the Y intercept, so: Y_intercept_AB = Ay - slope_AB * Ax . 现在,线的格式为Y = MX+B ,其中M是刚刚计算出的斜率,B是Y截距,因此: Y_intercept_AB = Ay - slope_AB * Ax So the equation for AB is Y = slope_AB*X + Y_intercept_AB . 因此,AB的等式为Y = slope_AB*X + Y_intercept_AB

OK, so now, the slope of the altitude from C to where it intersects line AB (let's call that point D, and the altitude line CD) is the negative reciprocal of the slope of AB; 好,现在,从C到与直线AB相交的高度的斜率(我们称该点D为高度线CD)是AB斜率的负倒数; so slope_CD = -(1/slope_AB) . 所以slope_CD = -(1/slope_AB)

So now, given that you have one point (C) on line CD and its slope, you can get the equation for CD the same way as you did for AB. 因此,现在,假设CD线上有一个点(C)及其斜率,则CD的方程式与AB相同。 First, find its Y-intercept: Y_intercept_CD = Cy - slope_CD * Cx 首先,找到其Y轴截距: Y_intercept_CD = Cy - slope_CD * Cx

So the equation for CD is Y = slope_CD * X + Y_intercept_CD . 因此,CD的等式为Y = slope_CD * X + Y_intercept_CD

So now you have equations for line AB and line CD: 所以现在您有了AB行和CD行的方程式:

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

And your problem is simplified to finding where those lines intersect, which is point D. 您的问题已简化为找到这些线相交的位置,即D点。

From the above equations, since both right-hand sides are equal to Y we can set them equal to each other: 根据以上等式,由于两个右侧都等于Y我们可以将它们彼此设置为相等:

slope_AB * X + Y_intercept_AB = slope_CD * X + Y_intercept_CD

and now it's just a matter of solving for X . 现在只需要解决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)

That will give you the X-value for D ( Dx ). 这将为您提供D( Dx )的X值。 For the Y-value, use either line equation. 对于Y值,请使用任一线性方程式。 Let's use the one for AB: 让我们将其用于AB:

Dy = slope_AB * Dx + Y_intercept_AB

Putting it all together, assume a triangle of A=(-4, 2) , B=(0, 6) , C=(6, -4) : 将它们放在一起,假设三角形为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))

Prints: 印刷品:

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

One more thing: this will divide-by-zero and fail where points A & B have the same X-value (because it divides by Ax-Bx , which would be zero); 还有一件事:这将被零除并在A点和B点具有相同X值的情况下失败(因为它被Ax-Bx除以零); but it's a start. 但这是一个开始。

Needed point is orthogonal projection of vertex point (say vertex C) onto the line containing opposite side (say AB). 所需点是顶点(例如顶点C)在包含相反面(例如AB)的线上的正交投影。

在此处输入图片说明

To find projection point, get vectors for AB and AC 要找到投影点,请获取AB和AC的向量

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

and find parameter using scalar product of AB and AC 并使用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))

Projection point coordinates 投影点坐标

 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM