# calculate height on triangle

This topic is 4489 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello, I have a mesh (for the moment just a triangle), and i want to calculate the height on any position of the triangle. Is there any tutorial on "how to calculate height (y?)" on any (x,z) of a triangle ? Can someone help me ? Thanks :-) Lud2k

##### Share on other sites
I'll do it with an example. Say your triangle has vertices (1,2,-1), (0,0,3) and (-4,3,1). Now we want to find the height of the plane that passes through all those points at x=-1, z=2.

Any point on the plane that contains the three vertices can be expressed as a baricenter of the three points. That means writing it as
A*(1,2,-1) + B*(0,0,3) + C*(-4,3,1), with A+B+C = 1.

So we want A*(1,2,-1) + B*(0,0,3) + C*(-4,3,1) = (-1,y,2), which expands to
A*1 + B*0 + C*-4 = -1
A*-1 + B*3 + C*1 = 2
A + B + C = 1

Solve the system of equations using your favorite method (A=1/9, B=11/18, C=5/18), and then compute the second coordinate of the point as y = A*2 + B*0 + C*3 = 19/18.

##### Share on other sites
Define "height".
By definition, any triangle forms a plane in n-space (3d-space in your case). The "height" of a plane is actually zero, since a plane is always "flat".

Cheers,
Pat.

##### Share on other sites
Hello,

I think it's an very efficient way to do it for a human.. but for a computer, solving 3 equations with 3 variables must be difficult or slow to do.

Is there any other way ? For exemple, I have the normal vector already computed. Is there any way to use it to calculate the height ? (I think it would be quicker no ? I've tried several things and it's sort of working but it's not on all cases..).

Thanks a lot.
Lud2k

##### Share on other sites
Quote:
 Original post by lud2kI have the normal vector already computed. Is there any way to use it to calculate the height ? (I think it would be quicker no ?

you use the normal to form your plane equation

##### Share on other sites
Quote:
 Original post by lud2k...but for a computer, solving 3 equations with 3 variables must be difficult or slow to do.
No, not really. And if this is for a terrain (you might clarify if that's the case), you can narrow the candidate triangles down to one with a little arithmetic and a couple of conditionals. From there, any differences in performance between the various methods of computing the height associated with a 2D coordinate will most likely be insignificant.

In any case, the two methods which most readily spring to mind are barycentric coordinates, and intersecting a vertical ray with the triangle plane (I didn't read the entire thread carefully, but I'm guessing both of these have been mentioned). If the triangle planes are precomputed and cached, the latter will probably be the faster of the two, but again, if you're only doing it a few times per frame it really won't matter.

##### Share on other sites
How to find the point X:(x,y,z) in the plane of a triangle, given the three vertices P, Q, and R of the triangle, and x and y.
1. Find a vector N that is perpendicular to the plane of the triangle.        N = (R - P) cross (Q - P)    2. Find the value of z using this formula:               NX(x - PX) + NY(y - PY)    z = - ---------------------- + PZ                    NZ

You can derive this by noting that N dot (X - P) = 0 and solving for XZ

[Edited by - JohnBolton on July 9, 2006 3:23:59 AM]

##### Share on other sites
Hello,

Now it's working :) yeeaaah.

double Poly::Heigh (double x, double z) {
double ret = 0.0;
Point res = CalcNormal(pts[0],pts[1],pts[2]);
double D = pts[0].x*res.x + pts[0].y*res.y + pts[0].z*res.z;
return -(res.x*(x-pts[0].x-pts[2].x) + res.z*(z-pts[0].z-pts[2].z) + D)/res.y + pts[0].y + pts[2].y;
}

Thanks everybody.

##### Share on other sites
You can also use ray-tracing to figure out the height, that's what I'm doing.

1. 1
Rutin
46
2. 2
3. 3
4. 4
5. 5
JoeJ
19

• 13
• 10
• 12
• 10
• 13
• ### Forum Statistics

• Total Topics
632998
• Total Posts
3009808
• ### Who's Online (See full list)

There are no registered users currently online

×