calculating angle between three points
93 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Bjorn Gustavsson
le 21 Mar 2017
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
Réponses (2)
Jan
le 21 Mar 2017
Modifié(e) : Jan
le 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 commentaire
Bharath Reddy Adapa
le 21 Mar 2017
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
Voir également
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!