How do I solve for the height of a position inside three nodes?

Given node a,b,c, and the point

solveY

How can I find the height of that point on the surface of the triangle?
I don’t need hacky ways, I already know one, I just want to solve the math for it so it’s better on performance.

What I already tried:
get the distance from the edges and get the average

3 Likes

I don’t know what is be the best way to do this, but here’s how I solved it.

a, b, and c are vectors representing the nodes and p is the point that we need to find the height for. ab and bc are the vectors describing distance from a to b and b to c. v1 is a vector that has the same direction as vector ab and its length (magnitude) is <= the length of ab. v2 has the same relation to bc. From this we can write an equation. a+v1+v2 = p

I wrote some equations. The ones I write here are not all exactly what I first wrote, but they may be more clear than those.

-- vectors
ab = b-a -- direction is from a towards b
bc = c-b -- direction is from b towards c

-- numbers (parts of vectors)
xa+x1+x2 = xp
za+z1+z2 = zp
ya+y1+y2 = yp

x1/z1 = xab/zab
x2/z2 = xbc/zbc

from these we can solve that

x1 = xab/zab*z1
x2 = xbc/zbc*z2

now we can turn

xa+x1+x2 = xp

into

xa+xab/zab*z1+xbc/zbc*z2 = xp

And from that we can solve that

z1 = (xap-xbc/zbc*z2)/xab*zab

And from the equation

za+z1+z2 = zp

we can solve that

z1 = zp-za-z2

So now we can combine those two z1 equations to get an equation that only has one unknown value.

(xap-xbc/zbc*z2)/xab*zab = zp-za-z2

Then, from that we can solve, that

z2 = (xab*zap-zab*xap)/(-zab*xbc/zbc+xab)

And with some more solving we get that

y2 = ybc/zbc*z2
z1 = zp-za-z2
y1 = yab/zab*z1

And then we have the y values needed to calculate the height of the point using an equation that was already written above

yp = ya+y1+y2

Here’s the code

-- a, b and c are Vector3 values. xp and zp are numbers.
-- They are the x and z coordinate of the point that were are getting the height for.
local function getPointHeight(a, b, c, xp, zp)
	local ab, bc = b-a, c-b

	local xa, ya, za = a.X, a.Y, a.Z
	local xab, yab, zab, xbc, ybc, zbc = ab.X, ab.Y, ab.Z, bc.X, bc.Y, bc.Z
	local xap, zap = xp-xa, zp-za

	local z2 = (xab*zap-zab*xap)/(-zab*xbc/zbc+xab)
	local y2 = ybc/zbc*z2
	local z1 = zp-za-z2
	local y1 = yab/zab*z1

	local yp = ya+y1+y2
	return yp
end
3 Likes

This is the most in-depth, well put together answer I’ve received yet. If I could thumbs it up 10x I would.

A+

10/10

1 Like