Calculating angle between two vectors from geometric points - code solution

Calculating the angle between two vectors is a fairly easy solution to find online - there are plenty of maths help websites which will take you through step by step how to work out the angle, however, not many sites will provide you with the necessary programming code to put this into your software. As this is something I have had to do a few times now, I'm posting my code solutions as a reminder for myself, but hopefully also as an aide to others wishing to programmatically calculate the angle between vectors.

In my area of work, I tend to be calculating the angles between a set of geometric points/co-ordinates, so my code solution outlined below begins with the creation of the necessary vectors from the geometric points. The code set out below is written for C++, however, it should work in any language though you may need to double-check some of the mathematical functions which are being used and how they're called in your language. For example, in C#, the atan2 function would be called using Math.Atan2.

Code to calculate angles between points

This function takes three arguments to calculate the angle between two vectors drawn between the points. The points it takes in are:

  • The connecting point of both vectors
  • The ending point of the first vector
  • The ending point of the second vector

You could of course modify the function to accept two vectors as your inputs if that were necessary, but as this is working with points, the construction of the vectors is completed as the first operation of the function.

Things to be wary of

This function will return an angle between 0 and 180 degrees (if converting to degrees) or 0 - PI rads (if not converting to degrees). It will only return a result which is the smallest angle between the two vectors drawn between three points, so directionality is unable to be obtained from this function. This post provides a solution for calculating the angle between 0 and 360 degrees.

While this function should work in any language, it has been displayed here ready for use in C++. Primarily, as mentioned above, you should only need to check how to call the mathematical functions in your chosen language. For C# and Java implementations, these are provided below, but for other languages a quick Google search of the function in your preferred language should return the necessary information (for example, Googling "Javascript acos" will return the W3Schools examples of acos in Javascript).

C++ C# Java
sqrt() Math.Sqrt() Math.sqrt()
fabs() (floating point absolute numbers for C++) Math.Abs() Math.abs()
acos Math.Acos() Math.acos()
PI (defined variable) Math.PI Math.PI

Finally, this example used Point3d variables as used in Rhino geometry but would work with any 3d point, just be careful to ensure that your equivalent has access operators to access the x,y,z positions using [0],[1],[2] as this example has done. If not, them simply change them accordingly.

Leave a Reply

Your email address will not be published. Required fields are marked *