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.
double GetAngleBetweenPoints(Point3d endPt1, Point3d connectingPt, Point3d endPt2) { double x1 = endPt1[0] - connectingPt[0]; //Vector 1 - x double y1 = endPt1[1] - connectingPt[1]; //Vector 1 - y double z1 = endPt1[2] - connectingPt[2]; //Vector 1 - z double sqr1 = (x1*x1) + (y1*y1) + (z1*z1); //Square of vector 1 double x2 = endPt2[0] - connectingPt[0]; //Vector 2 - x double y2 = endPt2[1] - connectingPt[1]; //Vector 2 - y double z2 = endPt2[2] - connectingPt[2]; //Vector 2 - z double sqr2 = (x2*x2) + (y2*y2) + (z2*z2); //Square of vector 2 double xa = x1 * x2; double ya = y1 * y2; double za = z1 * z2; double costr = (xa + ya + za) / sqrt(fabs(sqr1*sqr2)); double angle = fabs(acos(costr)); //This produces a result in radians angle = angle * 180 / PI; //This converts the result into degrees return angle; }
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.