calculating angle between three points

100 次查看(过去 30 天)
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 个评论
Bjorn Gustavsson
Bjorn Gustavsson 2017-3-21
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
KalMandy
KalMandy 2017-3-21
Thanks. I know that I can try that, but I wanted to see other options too.

请先登录,再进行评论。

回答(2 个)

Jan
Jan 2017-3-21
编辑:Jan 2017-3-21
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 个评论
clarammd
clarammd 2023-4-23
编辑:clarammd 2023-4-23
What does it mean the angle to be instable at (anti-)parallel n1 and n2? Also, why the vectors need to be normalized?

请先登录,再进行评论。


Bharath Reddy Adapa
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

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by