Calculations of Angle between two points
31 次查看(过去 30 天)
显示 更早的评论
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/704672/image.jpeg)
4 个评论
回答(2 个)
M.B
2021-8-6
编辑:M.B
2021-8-6
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
2 个评论
J. Alex Lee
2021-8-6
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
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!