MathGem:Height of a Point in a Triangle
Height of a Point in a Triangle
This shows how to find the value of one component of a point in a triangle given the other two components. In this article, we find Z given X and Y, but this method could apply to any component simply by swapping axis names.
Given three vertices of a triangle, P, Q, and R, and x and y of the point,
- Find a vector that is perpendicular to the plane of the triangle. If you already have the normal, use that. Otherwise, this will do:
- Find the value of z using this formula:
Note: If Nz is 0, then the triangle is parallel to the Z axis and there is no unique z.
The Height of the Terrain
One instance where this is used is to find the height of the terrain at a particular location. Once the particular triangle at the location is found, the height of the object can be determined from the equation above. It should be noted that if the vertices of the terrain are on an axis-aligned regular grid (such as a heightmap), the calculation of is simplified. Assuming that is parallel to the X axis, is parallel to the Y axis, and the distance between points in the grid is h, is computed as follows:
Another common application is interpolating the color of a point in a triangle in 2D given the colors at the vertices. In this case, the Z component is a color instead of a location.
If the point X:[x,y,z] is in the plane of the triangle, then the dot product of a vector, N perpendicular to the plane, and the vector (X − P) is 0.
if P,R,Q is a triangle vertices, and X,Y - is a coordinates of point, then:
a:=-(R*Q-P*Q-R*P+P*Q+R*P-Q*R); b:= (P*R+Q*P+R*Q-Q*R-P*Q-R*P); c:= (Q*R+P*Q+R*P-P*R-Q*P-Q*R); d:=-a*P-b*P-c*P;
result:= -(a*X+c*Y+d)/b; //z of point