MAXScript FAQ > Working With Vectors > How do I find the angle between 3 vertices? |
A face always has 3 vertices. Each pair of vertices define one of the 3 edges of the face, and at the same time can be seen as a vector. As shown in How do I make a vector from a vertex position? FAQ topic, every vertex is already a vector, and the edge vector connecting two vertices can be calculated easily by subtracting the two vertex positions.
Let's call the 3 vertices A, B and C. If you want to calculate the angle at vertex A, you will need the two vectors which start from vertex A and point at B and C respectively. These two vectors would be expressed as
The image shows point A with coordinates [0,0,0], point B with coordinates [2,0,1] and point C with coordinates [-1,0,2].
Now you have two vectors, but the vector dot product requires normalized vectors to return usable results.A normalized vector has the same direction as the original vector, but the length of 1.0.
You can calculate the normalized vector by using the normalize method on the vectors V1 and V2:
In the case of V1 = [2,0,1], N1 = normalize V1 returns [0.894427,0,0.447214] because length V1 =2.23607, and the normalized vector is calculated by dividing the X, Y and Z coordinates by the length, in this case [2/2.23607, 0/2.23607, 1/2.23607].
N2 = normalize V2 returns [-0.447214,0,0.894427] because the length of the vector [-1,0,2] is also 2.23607, and the normalized vector is calculated as [-1/2.23607, 0/2.23607, 2/2.23607].
Finally, as explained in the How do I find the angle between two vectors?, we need to calculate the acos of the dot product of these normalized vectors. acos is the reverse operations of cos, returning the angle whose cos is equal to the operand (in other words, if X = cos Alpha, then Alpha = acos X).
The Angle will range between 0 (when the two vectors are parallel) and 180 degrees (when the two vectors are pointing in opposite directions).
In the above case of coordinates [0,0,0], [2,0,1] and [-1,0,2], the angle at point A is 90 degrees.
To calculate the angle at B, you would apply the same rules, but will use the vectors A-B and C-B,
then normalize the vectors, calculate the dot product and get the acos of the result. The angle at point B in this example is 45 degrees.