Calculations of Angle between two points
23 views (last 30 days)
Show older comments
How to generate code using for loop to calculate angle between the points A and B. Similarly between points B and C and so on. I have attached figure.
4 Comments
Answers (2)
M.B
on 6 Aug 2021
Edited: M.B
on 6 Aug 2021
As mensioned by others, you need 3 points or two vectors to define an angle.
You can use this code to compute the angle defined by three points at p1:
p1 = [x1_coordinate, y1_coordinate, z1_coordinate];% p1, p2, and p3 are your three points
p2 = [x2_coordinate, y2_coordinate, z2_coordinate];
p3 = [x3_coordinate, y3_coordinate, z3_coordinate];
vect1 = (p3 - p1)/ norm(p3-p1);
vect2 = (p2 - p1) / norm(p2 - p1);
angle = atan2(norm(cross([vect2;vect1])), dot(vect1, vect2));% *180/pi if you want it in degrees
J. Alex Lee
on 6 Aug 2021
If you have the list of 3D coordinates
rng(1) % control random generator
NPoints = 5
vertices = rand(NPoints,3)
You can define the list of angles (you have specified 4 specific angles in your figure, assuming your coordinate numbering and that "p" is the fifth) that you want by a trio of indices, asserting the convention that you want the angle about the 2nd point in the trio (second column)
T = [
5,1,2;
5,2,3;
5,3,4;
5,4,3;
]
By the way it is ambiguous whether you intend points 1-4 to be co-linear.
Then your workhorse angle calculator function can be defined as below so that
for i = 1:size(T,1)
th(i) = AngleFinder(vertices(T(i,:),:))
end
Workshorse angle calculator (probably same result as above answer by M.B)
function th = AngleFinder(verts)
v = verts([1,3],:) - verts(2,:); % vectors you want the angle between
% use the relation that cos(th) = dot(v1,v2)/||v1||/||v2||
th = acos(dot(v(1,:),v(2,:))/prod(sqrt(sum(v.^2,2))));
end
See Also
Categories
Find more on Creating and Concatenating Matrices 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!