# figureing out the x,y, and z rotation of a right triangle?

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

## Recommended Posts

I have a triangle made of 3 vertices. I need to know what the angle of the triangle is.

How would I go about it?

##### Share on other sites
What do you mean by angle of the triangle?

##### Share on other sites
Triangles generally have three vertices, and three angles.

I need all three angles.

##### Share on other sites
Form 2 vectors from one vertex to each of the other two. The dot product of those vectors is proportional to the cosine of the angle between them.

Repeat for each of the other 2 vertices.

You can also google for "triangle angles from vertices" and get over 600,000 hits to examine.

##### Share on other sites
My bad. It's 2 points, not a triangle >_<
Alright, so I got two vectors that each contain a x y and z. We'll call them a and b.

I create a new point in the middle of the two points.

Vector3 middle = new Vector3(//order x,y,z
(max.getX() - min.getX())/2,
(max.getY() - min.getY())/2,
(max.getZ() - min.getZ())/2
);

To get the y angle I use the formula Math.toDegrees(Math.atan2(middle.getX(), middle.getZ()))

##### Share on other sites
Quote:
 Original post by CyanPrimeMy bad. It's 2 points, not a triangle >_

What do you mean by "y angle"?
We generally want "the angle between two vectors". If you want the angle between the vector that has the point A as its origin and the point B as its end, and the y axis, just do:
Vector3 A(...);Vector3 B(...);Vector3 v( substract( B, A ) );v.normalize();float cosine = dot_product( v, Vector3( 0, 1, 0 ) );float angle = std::acos( cosine );

This is easy to grasp once you know what the dot product means, and what the component of a vector along an axis means(for example v.y).

P.S: the angle is not signed. It's a value between 0(radian) and pi(radians).
dot_product( v, v ) = squared_length( v ).

Writing everything on paper, it's easy to grasp the formula for this special case.
The angle will be equal to:
float angle = acos( y / std::sqrt( dot_product( v, v ) );

If you want a signed angle, you can use trigonometry:
you have a right triangle, its hypothenuse being formed by v, and one of the adjacent side being formed by the projection of v into the y axis.
The tangent of the angle you're looking for is:
Vector3 A(...);Vector3 B(...);Vector3 v( substract( B, A ) );float xz_projection = std::sqrt( std::( pow( v.x, 2 ) + pow( v.z, 2 ) ) );float y_projection = v.y;float tangent = xz_projection / y_projection;

So the angle is equal to:
float angle = std:atan2( xz_projection / y_projection );

The angle will be a value between -pi/2 and pi/2 radians.

P.S: it's always nice when an OP acknowledges a provided solution.

[Edited by - johnstanp on December 7, 2010 8:52:45 AM]

##### Share on other sites
Thank you very much for your answer.

##### Share on other sites
Quote:
 Original post by CyanPrimeThank you very much for your answer.

You're welcome.