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:

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

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;
}

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); }