- Home /
How to calculate internal angles of a mesh triangle?
Hi Guys!
I'm trying to find the internal angles a, b and c of the triangle in the picture below. The red points form a triangle of a mesh. I know the Vector3 positions of the red points and the normal of the triangle, but I don't know how to work out the angles between these points. How do I do this?
My triangle is below:
Answer by Bunny83 · Mar 02, 2020 at 05:09 AM
Well, you can simply use Unity's Vector3.Angle method like this:
public static float GetVectorInternalAngle(Vector3 a, Vector3 b, Vector3 c)
{
return Vector3.Angle(b-a, c-a);
}
Following is an explanation what's actually happening here. You can ignore the following unless you are interested what happens behind the scenes.
What we actually do here is calculate the two vectors that meet at point a. The Angle function simply calculates the dot product between the two vectors which is just multiplying the two vectors component wise and adding all values up to a single value. This value has the nice property that it's the product of the lengths of the two vectors multiplied by the cosine of the angle between them.
To actually get the angle we just have to divide that value by the lengths of both vectors. To calculate the length of a vector we just use the Pythagorean Theorem. Unity's Vector3 struct does this calculation for us with the "magnitude" property. Though since we have to divide through both vector lengths the Angle function calculates the product using only one square root for both lengths.
Once the value is divided by the lengths the remaining value represents the cosine of the angle we're looking for. Now we could simply use the arcus cosine function (Acos()). However due to floating point imprecisions the Angle function first clamps the value between -1 and 1 because the arcus cosine of a value smaller than -1 or greater than 1 is not defined and would result in a NaN (Not a Number) value.
In actual mathematics and computer science the trigonometric functions (sine, cosine, tangent, ...) do not work in degrees but in radians. A full "360°" turn equals 2*PI in radians. So a value of PI represents 180°. To convert the angle in radians to degree we just have to divide by PI and multiply by 180. Unity's Mathf class provides this as a precalculated factor "Mathf.Rad2Deg".
Well, all internal angles of a square (or rectangle) are 90°, otherwise it's not a square (rectangle) :) Maybe you think about a quadrilateral (or quad for short)? Well if the mesh / submesh actually has "Quads" as topology it would work similarly. You just need to use 3 consecutive points with the method I've posted above. Note that the "middle" point (or the point you want to get the angle from) is the point "a" in my method. The other two points are simply the neighboring points.
However if the mesh is an actual triangle mesh, there's no general meaningful way to "combine" triangles into quads. So unless you specify the 4 points exactly, there's not much you can do here.
If you have a more concrete example, please ask a seperate question. Feel free to include a link to this question but don't forget to explain your specific problem more in detail.
Thanks for answering, after modifying the above method getInternalvector i figured it how to do it
Answer by IrishSenthil · Mar 02, 2020 at 04:03 AM
Hi Guys,
I figured it out. Here is my solution:
public static float GetVectorInternalAngle(Vector3 a, Vector3 b, Vector3 c)
{
double num = (b.x - a.x) * (c.x - a.x) + (b.y - a.y) * (c.y - a.y) + (b.z - a.z) * (c.z - a.z);
double den = Math.Sqrt(Math.Pow((b.x - a.x), 2) + Math.Pow((b.y - a.y), 2) + Math.Pow((b.z - a.z), 2)) *
Math.Sqrt(Math.Pow((c.x - a.x), 2) + Math.Pow((c.y - a.y), 2) + Math.Pow((c.z - a.z), 2));
double angle = Math.Acos(num / den) * (180.0 / Math.PI);
return (float) angle;
}
Answer by Sanjay97123 · Jan 01 at 08:48 AM
This Works For Square
public static float GetVectorInternalAngle(Vector3 a, Vector3 b, Vector3 c,Vector3 d) { Vector3 from = d-a; Vector3 to = a-b; return Vector3.Angle(-from, to); }