Obtaining the angle between two vectors for 360 degrees

This previous post demonstrated how to obtain the angle between two vectors from three geometric points, providing an angle between 0-180 degrees. However, there may be times when you need the angle between 0-360 degrees instead, as I did earlier this week. As such, this post aims to complete the previous with the solution for doing so.

Examples of when we might need the larger angle (i.e. above 180 degrees) include working out directionality or orientation around a point. 30 degrees is no good if we don’t know whether it’s 30 degrees to the left or to the right. However, 30 degrees vs 330 degrees can give us a bit more data to work with.

As with the previous post, the code example should work in any language, though you will need to find the equivalent function for the atan2 method used here.

Code solution

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 x2 = endPt2[0] - connectingPt[0]; //Vector 2 - x
    double y2 = endPt2[1] - connectingPt[1]; //Vector 2 - y

    double angle = atan2(y1, x1) - atan2(y2, x2);
    angle = angle * 360 / (2*PI);

    if(angle < 0) {
        angle += 360;
    }

    return angle;
}

Here, we use the atan2 method to return the angle between the two vectors. However, the calculations used in this function will return a value anywhere between -180 to +180. Therefore, to obtain an angle between 0-360 we need to correct the results that are less than 0. This is covered with the if statement which checks for answers less than 0, and adds 360 to them. This will move the figured from -180 to -1 up to 180-360, which, when combined with the ability to do 0-180, gives us the full 360 degrees calculation. As with the previous post, the line
double angle = atan2(y1, x1) - atan2(y2, x2);
produces a result in radians, which is converted to degrees with the following line
angle = angle * 360 / (2*PI);
If you wish to work in rads (for whatever reason) then you can omit that line, however, you would need to convert the if statement accordingly as well.

Due to the way the function works, you can assume that everything between 181 and 360 degrees is to the right hand side of the target, with everything 1 to 179 being on the left hand side and 180 and 0 being straight on or straight behind respectively. This is because the calculation provides an angle for a clockwise rotation. Now you can calculate the angles between three geometric points (or two vectors if you are working with vectors and not points by converting the functions accordingly) in your own software. Or, most likely, I will be able to come back and find my functions for the next time I need them without having to re-code them from memory as I had to last week!

Leave a Reply

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