Calculations of Angle between two points
13 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuestas (2)
M.B
el 6 de Ag. de 2021
Editada: M.B
el 6 de Ag. de 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
2 comentarios
J. Alex Lee
el 6 de Ag. de 2021
Your code does not work...did you mean
cross(vect2,vect1)
J. Alex Lee
el 6 de Ag. de 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
Ver también
Categorías
Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!