Calculations of Angle between two points

조회 수: 46 (최근 30일)
Yadu Bhusal
Yadu Bhusal 2021년 8월 6일
댓글: Yadu Bhusal 2021년 8월 7일
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
Cris LaPierre
Cris LaPierre 2021년 8월 6일
You need 3 points to define an angle.
Yadu Bhusal
Yadu Bhusal 2021년 8월 7일
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.

댓글을 달려면 로그인하십시오.

답변 (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
J. Alex Lee 2021년 8월 6일
Your code does not work...did you mean
Yadu Bhusal
Yadu Bhusal 2021년 8월 7일
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.

댓글을 달려면 로그인하십시오.

J. Alex Lee
J. Alex Lee 2021년 8월 6일
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 = [
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,:),:))
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))));
  댓글 수: 1
Yadu Bhusal
Yadu Bhusal 2021년 8월 7일
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.

댓글을 달려면 로그인하십시오.


Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by