MATLAB Answers

How can i measure Angle in 3-D?

17 views (last 30 days)
Mahmoud Sami
Mahmoud Sami on 8 Sep 2018
Commented: Mahmoud Sami on 22 Sep 2018
I have points
S =[0.5360 0.8850 2.3962;
0.5360 0.8850 2.4291;
0.5436 0.1708 1.8550;
0.7532 0.8089 0.9649;
0.9630 0.4010 1.1216]
plot3(S(:, 1), S(:, 2), S(:, 3), 'b.', 'MarkerSize', 30, 'LineWidth', 2);
grid on;
These points are connected to make connected lines. How can I measure the angle between the lines?


Mahmoud Sami
Mahmoud Sami on 8 Sep 2018
The 1st line connected between the first point and the second one, the second line connected between the second point and the third one, to the end of S. The angle the 1st line and 2nd line. Then 2nd line and 3 rd line.

Sign in to comment.

Accepted Answer

Jim Riggs
Jim Riggs on 9 Sep 2018
Edited: Jim Riggs on 9 Sep 2018
The link in the answer by Aquatris is not working for me.
The angle between any two lines is given by the dot product.
Define each line segment as a vector from one point to the next in the point sequence, i.e. segment D1 = P2 - P1 = {P2x-P1x, P2y-P1y, P2z-P1z}. Likewise, define segment D2 as P3 - P2 = {P3x-P2x, P3y-P2y, P3z-P2z}. Now normalize these two vectors to make then unit vectors U1 = D1/|D1| and U2 = D2/|D2|.
Take the dot product of U1 and U2; this is the cosine of the angle between line segment 1 and 2.


Show 1 older comment
Jim Riggs
Jim Riggs on 11 Sep 2018
This will give you the angles between each line segment. Note that I have used the acosd function, so the angles are in degrees. you can change this to acos if you prefer to work in radians. Note that for n points, there are n-1 line segments and n-2 angles.
Angle 1 is the angle between line segment 1 and 2, which are defined by points 1, 2 and 3.
Angle 2 is the angle between line segments 2 and 3, which are defined by points 2, 3, and 4, etc.
clear Ang
[n, m] = size(S); % n is the number of points, m should be 3
for i=2:n-1
V1(1:3) = S(j,:) - S(i,:); % line segment from pt j to i
U1(1:3) = V1./sqrt(V1(1)^2+V1(2)^2+V1(3)^2); % Unit vector pointing from pt i to j
V2(1:3) = S(k,:) - S(i,:); % line segment from pt i to k
U2(1:3) = V2./sqrt(V2(1)^2+V2(2)^2+V2(3)^2) % unit vector pointing from i to k
ang(j) = acosd(dot(U1,U2)) % Angle between adjacent line segments, vertex at point i
Jim Riggs
Jim Riggs on 11 Sep 2018
If you want some help visualizing the angles in the 3D plot, add the rotation axis to the plot as follows;
Add the calculation of the normal vector for each angle vertex right after the angle calculation inside the for loop:
ang(j) = acosd(dot(U1,U2));
norm(j) = 0.1*cross(U1,U2);
I use a 0.1 scale factor so that this line is not too long in the plot. Now make the 3D plot, and add the normal vectors to the plot as follows:
% The first part is the same as your plot, except I have drawn the lines connecting the points
plot3(S(:,1), S(:,2),S(:,3),'-ob','MarkerSize',6,'LineWidth',2);
grid on;
axis equal;
% now add the normal vectors
hold on;
for j=1:n-2
Vt = norm(j,:) + S(j+1,:)
plot3([S(j+1,1) Vt(1)], [S(j+1,2) Vt(2)], [S(j+1,3) Vt(3)] 'r','LineWidth',3)
These red lines are the rotation axes for each angle. Note that using "axis equal" makes all three axes of equal scale so that line lengths are drawn proportionally, and this makes the angles look right.
Now you can rotate the 3D plot and the red lines will help you line up the view to visualize the angles.
The five points in your S matrix define 4 line segments and 3 angles. I calculate the three angles to be 51.2, 93.2, and 48.8 degrees.
Mahmoud Sami
Mahmoud Sami on 22 Sep 2018
Thanks for your answer. Sorry for late feedback.

Sign in to comment.

More Answers (1)

Aquatris on 9 Sep 2018
Check out this link which has the answer.

  1 Comment

Mahmoud Sami
Mahmoud Sami on 11 Sep 2018
Thanks for your answer. I ask for Matlab coding for that.

Sign in to comment.


Community Treasure Hunt

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

Start Hunting!

Translated by