Given: The 3 triangle vertices in a 3D space, the x,y coordinates of a point on that triangle(triangle area included).
Wanted: The z coordinate of the given point.
All 3 triangle vertices have different (x,y) coordinates(they’re heightmap coordinates), so the case of multiple(infinite) matches doesn’t need to be handled.
I’m trying to do this in C, meaning an algorithm operating on simple number types e.g. floats would be best(no matrix or vector operations).
This is done typically with vectors / matrices, which are just shorter notations for the underlying operations.
make two vectors U=(ux,uy) = point2 – O; V=(vx,vy) = point3 – O
solve the linear system
x,y = u*U + v*V for u and v
x = u * (p2x-ox) + v * (p3x-ox)
y = u * (p2y-oy) + v * (p3y-oy)
Check that 0 <= u,v <= 1 and 0<=u+v<=1
If yes, then the point x,y is inside the triangle and
z = u*(p2z-oz)+v*(p3z-oz)