Calculations of Angle between two points

23 views (last 30 days)
Yadu Bhusal
Yadu Bhusal on 6 Aug 2021
Commented: Yadu Bhusal on 7 Aug 2021
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
Cris LaPierre
Cris LaPierre on 6 Aug 2021
You need 3 points to define an angle.
Yadu Bhusal
Yadu Bhusal on 7 Aug 2021
Yes. Suppose i want to calculate angle between AP and AB. I can use either cosine law of dot product to find angle between. But, in actual case, I need a code to generate angle. For example P( 1 2 3), A(000),B(111),C(222). I would like to put these coordinates in a single array like S = [000;111;222]. Then i have to generate code using for loop to find angle between point P and the listed coordinates in array. I hope you understand my question.

Sign in to comment.

Answers (2)

M.B
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
  2 Comments
J. Alex Lee
J. Alex Lee on 6 Aug 2021
Your code does not work...did you mean
cross(vect2,vect1)
Yadu Bhusal
Yadu Bhusal on 7 Aug 2021
According to my question I want to find angle between AP and AB. Angle between BP and BC and finally CP and CB. I need a code to run program using for loop to calculate angle. And furthermore i want to list points A, B and C in a single array which for example, i have posted in above comment.

Sign in to comment.


J. Alex Lee
J. Alex Lee on 6 Aug 2021
If you have the list of 3D coordinates
rng(1) % control random generator
NPoints = 5
NPoints = 5
vertices = rand(NPoints,3)
vertices = 5×3
0.4170 0.0923 0.4192 0.7203 0.1863 0.6852 0.0001 0.3456 0.2045 0.3023 0.3968 0.8781 0.1468 0.5388 0.0274
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;
]
T = 4×3
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
th = 2.1337
th = 1×2
2.1337 0.3245
th = 1×3
2.1337 0.3245 1.8679
th = 1×4
2.1337 0.3245 1.8679 0.3338
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 Comment
Yadu Bhusal
Yadu Bhusal on 7 Aug 2021
Thanks to all who tried to help me. But still it didn't work. I tried but the angles didn't match and angle with hand calculations. I have posted a sample of program. I have to calculate the distance from each point (inside A)to calculation point P and also the angles from each point to calculation point P.

Sign in to comment.

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!