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
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
回答(2 个)
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 个评论
Bharath Reddy Adapa
2017-3-21
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 Center 和 File Exchange 中查找有关 Logical 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!