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.
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.
So first, you can determine the equation for line AB. You have points A and B ( Ax, Ay
; and Bx, By
). Given that you can calculate the slope_AB as (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
. So the equation for AB is 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; so 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. First, find its Y-intercept: Y_intercept_CD = Cy - slope_CD * Cx
So the equation for CD is Y = slope_CD * X + Y_intercept_CD
.
So now you have equations for line AB and line 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.
From the above equations, since both right-hand sides are equal to Y
we can set them equal to each other:
slope_AB * X + Y_intercept_AB = slope_CD * X + Y_intercept_CD
and now it's just a matter of solving for 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
). For the Y-value, use either line equation. Let's use the one for 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)
:
#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); but it's a start.
Needed point is orthogonal projection of vertex point (say vertex C) onto the line containing opposite side (say AB).
To find projection point, get vectors for AB and 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
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.