how to calculate angle from coordinate
51 views (last 30 days)
Show older comments
noor faizah zohardin
on 2 Nov 2017
Commented: noor faizah zohardin
on 5 Nov 2017
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/169097/image.png)
how to calculate all angle for this image if i have coordinate
>> S1=[X1 Y1];
>> S2=[X2,Y2];
>> S3=[X3,Y3];
>> S4=[X4,Y4];
>> S5=[X5,Y5];
>> S6=[X6,Y6];
>> S7=[X7,Y7];
>> S8=[X8,Y8];
2 Comments
Walter Roberson
on 2 Nov 2017
Is it fully connected? I see some hints it might be, but from the diagram we cannot tell if the bottom left is connected to the mid left and upper left separately or if the bottom left is connect to the mid left and the mid left connected to the upper left without there being a lower left to upper left.
Accepted Answer
Roger Stafford
on 2 Nov 2017
Edited: Roger Stafford
on 2 Nov 2017
If A, B, and C are three points in two-dimensional space defined by 1 x 2 coordinate row vectors, here’s the way I like to compute the angle ABC - that is, the angle between line segments AB and CB:
AB = A-B;
CB = C-B;
ang = atan2(abs(det([AB;CB])),dot(AB,CB)); % Angle in radians
(Corrected)
2 Comments
Roger Stafford
on 2 Nov 2017
Edited: Roger Stafford
on 2 Nov 2017
You are right, Walter. I mistakenly left out the square brackets around AB:CB. I've made the correction.
More Answers (1)
Walter Roberson
on 3 Nov 2017
Vectorizing Roger's formula:
X = [X1, X2, X3, X4, X5, X6, X7, X8];
Y = [Y1, Y2, Y3, Y4, Y5, Y6, Y7, Y8];
[Aidx, Bidx, Cidx] = ndgrid(1:8, 1:8, 1:8);
Ax = X(Aidx);
Ay = Y(Aidx);
Bx = X(Bidx);
By = Y(Bidx);
Cx = X(Cidx);
Cy = Y(Cidx);
ABx = Ax - Bx;
ABy = Ay - By;
CBx = Cx - Bx;
CBy = Cy - By;
det_term = ABx .* CBy - ABy .* CBx;
dot_term = ABx .* CBx + ABy .* CBy;
ang = atan2(abs(det_term),dot_term); % Angle in radians
Now, ang will be 8 x 8 x 8, and ang(I,J,K) is the angle between [X(I),Y(I)] to [X(J),Y(J)] to [X(K), Y(K)] . These will not be useful for any duplicated coordinates, but I leave it to you to extract the 336 useful angles, which will probably depend upon what you intend to do with the information.
3 Comments
Walter Roberson
on 3 Nov 2017
No, that is not correct unless you are measuring the angles relative to a fixed axes, which is not what Roger's formula does.
Roger's formula asks: given line segment AB formed by the line between A and B, and line segment BC formed by the line between B and C, what is the angle AB to BC ? So like the angle for the line between stations (5,2) and (2,6), the 5-2-6 angle.
With 8 possible sources, 7 possible intermediate stations and 6 possible destinations (that is, avoiding lines that double back on themselves), that is 8 * 7 * 6 = 336 angles.
It is certainly possible to instead ask what the angle is relative to the y axis, but that is not what Roger's formula calculates.
To ask the angle relative to the y axis, you would use
atan2(ydest - ysource, xdest - xsource)
See Also
Categories
Find more on Surface and Mesh Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!