Calculations of Angle between two points
Afficher commentaires plus anciens
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 commentaires
Megan Jurczak
le 6 Août 2021
Do you want the angles marked by θ? How are your values stored? You need to provide some sort of context...
J. Alex Lee
le 6 Août 2021
are you asking to use for loop in the calculation of the angles? or for loop to iterate through multiple versions of the same problem of finding the angle between three points?
i assume the latter, in which case your question should be about calculation of angles between 2 points w.r.t. some origin, because all of your problems can be reduced to this problem by a simple translations to place the vertex of interest at the origin.
Cris LaPierre
le 6 Août 2021
You need 3 points to define an angle.
Yadu Bhusal
le 7 Août 2021
Réponses (2)
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 commentaires
J. Alex Lee
le 6 Août 2021
Your code does not work...did you mean
cross(vect2,vect1)
Yadu Bhusal
le 7 Août 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
1 commentaire
Yadu Bhusal
le 7 Août 2021
Catégories
En savoir plus sur Axes Transformations dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!