Error in finding the angle between three points using atan2

조회 수: 7 (최근 30일)
Deepak Singh
Deepak Singh 2022년 9월 3일
댓글: Deepak Singh 2022년 9월 3일
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.

채택된 답변

Bruno Luong
Bruno Luong 2022년 9월 3일
편집: Bruno Luong 2022년 9월 3일
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
  댓글 수: 1
Deepak Singh
Deepak Singh 2022년 9월 3일
Thank you for your answer
Yes, you are right. Thanks a lot for the answer.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

Community Treasure Hunt

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

Start Hunting!

Translated by