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

Started by
6 comments, last by johnstanp 13 years, 4 months ago
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?
Advertisement
What do you mean by angle of the triangle?
Triangles generally have three vertices, and three angles.

I need all three angles.
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.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

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()))
Quote:Original post by CyanPrime
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()))

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 ).

[edit]
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]
Thank you very much for your answer.
Quote:Original post by CyanPrime
Thank you very much for your answer.


You're welcome.

This topic is closed to new replies.

Advertisement