Error in finding the angle between three points using atan2

7 views (last 30 days)
Hi everyone,
I was using the following formula to find the angle between three coordinate values (i.e., P1, P0, P2; finding angle at P0)
ang = (180/pi)*(atan2(abs(det([P2-P0;P1-P0])),dot(P2-P0,P1-P0)));
So, it worked fine when I took the coordinate values as ;
P0 = [3,1];
P1 = [1,3];
P2 = [4,4];
figure;
plot(P0(1),P0(2),"*"); hold on;
plot(P1(1),P1(2),"*"); hold on;
plot(P2(1),P2(2),"*"); hold on;
ylim([0 7])
xlim([0 5])
legend("P0","P1","P2")
% P0 is the center where the angle would be
ang = (180/pi)*(atan2(abs(det([P2-P0;P1-P0])),dot(P2-P0,P1-P0))); % formula to get angle
% ang = 63.439 deg
But when I took three coordinate values as the following then it gave me a strange angle value, which is probably the supplmentary angle at P0.
P1 = [-16.49,-17.69];
P0 = [-25.83,-21.73];
P2 = [-40.77,-18.10]
figure;
plot(P0(1),P0(2),"*"); hold on;
plot(P1(1),P1(2),"*"); hold on;
plot(P2(1),P2(2),"*"); hold on;
legend("P0","P1","P2");
ang = (180/pi)*(atan2(abs(det([P2-P0;P1-P0])),dot(P2-P0,P1-P0)));
% ang = 142.9526 deg ; seems like the supplementary angle at P0.
Why is it happening? Any clues?
I need to apply this formula to calculate angle over 100s of coordinates and but if this kind of non-uniformity would happen then I wouldn't be able to use it.
Any help would be appreciated!
Thank you.

Accepted Answer

Bruno Luong
Bruno Luong on 3 Sep 2022
Edited: Bruno Luong on 3 Sep 2022
142.9 degree seem right to be. If you don't make the same aspect ratio of x and y with axis equal, angles are deformed on your screen monitor.
P1 = [-16.49,-17.69];
P0 = [-25.83,-21.73];
P2 = [-40.77,-18.10];
figure;
plot(P0(1),P0(2),"*"); hold on;
plot(P1(1),P1(2),"*"); hold on;
plot(P2(1),P2(2),"*"); hold on;
P102 = [P1; P0; P2];
plot(P102(:,1),P102(:,2))
axis equal % This is important
legend("P0","P1","P2");
ang = (180/pi)*(atan2(abs(det([P2-P0;P1-P0])),dot(P2-P0,P1-P0))) % correct
ang = 142.9526

More Answers (0)

Categories

Find more on Contour Plots in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!