calculating angle between three points

angle = atan2(abs(det([P2-P0;P1-P0])),dot(P2-P0,P1-P0));
I have seen that the angle between three points can be calculated as above where P0 = [x0,y0], P1 = [x1,y1], and P2 = [x2,y2]
can someone tell me at which point is this angle calculated? OR any other code which does the same thing is also appreciated. Thanks

2 Comments

Well, why not give it a try? Put down 3 points on a paper in a triangle, use them as an input to the expression and see what it gives you. After that you should be able to figure out the answer.
HTH
Thanks. I know that I can try that, but I wanted to see other options too.

Sign in to comment.

Answers (2)

Jan
Jan on 21 Mar 2017
Edited: Jan on 21 Mar 2017
The shown code calculates the angle between the lines from P0 to P1 and P0 to P2.
Neither the dot nor the cross product are stable. This means that there are some inputs for both command, which reply inaccurate output:
P0 = [x0, y0];
P1 = [x1, y1];
P2 = [x2, y2];
n1 = (P2 - P0) / norm(P2 - P0); % Normalized vectors
n2 = (P1 - P0) / norm(P1 - Po);
angle1 = acos(dot(n1, n2)); % Instable at (anti-)parallel n1 and n2
angle2 = asin(norm(cropss(n1, n2)); % Instable at perpendiculare n1 and n2
angle3 = atan2(norm(cross(n1, n2)), dot(n1, n2)); % Stable
Note that Matlab's cross does not handle 2D vectors. Therefore use this for the 2D case:
angle3 = atan2(norm(det([n2; n1])), dot(n1, n2));

1 Comment

clarammd
clarammd on 23 Apr 2023
Edited: clarammd on 23 Apr 2023
What does it mean the angle to be instable at (anti-)parallel n1 and n2? Also, why the vectors need to be normalized?

Sign in to comment.

dot product dot(P2-P0,P1-P0) can give the idea. P2-P0 can be seen as vector (or displacement) starting from P0 ending at P2.
a = (P2-P0) = [x2-x0, y2-y0] = [a1,a2] = a1 + i a2, and similary a vector b for P1-P0. dot product is a.b = ab cos(theta)
You can simply use 'theta = acos(dot(P2-P0,P1-P0))'. First part of your equation using atan2 is just the sin(theta) part

Products

Tags

Asked:

on 21 Mar 2017

Edited:

on 23 Apr 2023

Community Treasure Hunt

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

Start Hunting!