How to compute directional angle between two 2D vectors in MatLab?

Sebastian Echeverri picture Sebastian Echeverri · Feb 25, 2015 · Viewed 7.6k times · Source

I am trying to implement a script to analyze some of my data. I have position information for three points (p1,p2,p3). I would like to find the angular displacement of point p3 from the vector p1p2, as in this picture:

angles

p3a, p3b, p3c, p3d show possible relative positions for p3. As shown, I would like the sign of the output angle to describe its relative position to vector p1p2.

The code I am using is as follows (adapted to the diagram):

v1 = p2 - p1;
x1 = v1(1);
y1 = v1(2);
v2 =  p1 - p3;
x2 = v2(1);
y2 = v2(2);
angle = atan2d(x1*y2-y1*2,x1*x2+y1*y2);

This works as desired when p3 is at p3a, giving a negative angle of the right size (-77 degrees). However, when p3 is at p3d, it outputs a large positive angle (+150 degrees), as opposed to the desired large negative angle.

Answer

whataberk picture whataberk · Feb 26, 2015

To start with, an easier way to think about the angle between two 2D vectors with coordinates is to align an axis with your coordinate vectors and think about the relationship between two vectors. Using the drawing below, we can see that a relative angle can be found by subtracting one angle from the other.

Source: http://almaer.com/blog/uploads/atan2.png

It is not too hard to figure out looking at this graph that we can say

angle = atan2d(y2,x2) - atan2d(y1,x1)

However, since neither of your vectors are known to be aligned along the coordinate axis, the case can arise where the difference above is not in the range (-180, 180). This means we need to code in a check to add or subtract 360 degrees to get our desired angle:

if abs(angle) > 180
  angle = angle - 360*sign(angle)
end

Note, you are using a kind of reverse notation (CW positive) so the final code would look like:

v1 = p1 - p2;
x1 = v1(1);
y1 = v1(2);
v2 = p3 - p1;
x2 = v2(1);
y2 = v2(2);
angle = atan2d(y1,x1) - atan2d(y2,x2)

if abs(angle) > 180
    angle = angle - 360*sign(angle)
end

Where v1 and v2 have been changed to match your drawing.