How to calculate a angle between two vectors in 3D

13 views (last 30 days)
Hi, I have a question, I have a table with 12 reference points
if true
% POINT X Y Z
1 0 0 0
2 70.5 0 0
3 141 0 0
4 141 0 141.5
5 70.5 0 141.5
6 0 0 141.5
7 0 137.5 0
8 70.5 137.5 0
9 141 140 0
10 141 141.5 141.5
11 70.5 139 141.5
12 0 141.5 141.5
end
The segment 1 is defined by the point 1 and point 2 and the segment 2 is defined by the point 1 and point 7. I am able to calculate the angle with the following rotine,
if true
% angle_x1_x13 = atan2(norm(cross(v1,v2)),dot(v1,v2));
anglout = radtodeg(angle_x1_x13);
end
The result is ~90º and this result is correct if I think in xy plan, but I need to calculate the angle to yz plan and xz plan. Anyone help me?

Accepted Answer

Vivek Selvam
Vivek Selvam on 18 Oct 2013
This code uses your angle calculation and shows for different reference points (3d). 2d is the same for your formula.
% Origin is reference point
p1 = 1*ones(1,3); % directly v1 = p1
p2 = 2*ones(1,3); % directly v2 = p2
% With origin as the reference point, the angle between vectors is 0
ang = rad2deg(atan2(norm(cross(p1,p2)),dot(p1,p2)));
disp(ang)
% Choosing reference point such that new v1, v2 are at 90 degrees
refpoint = [2 2 1];
v1 = p1 - refpoint;
v2 = p2 - refpoint;
angNew = rad2deg(atan2(norm(cross(v1,v2)),dot(v1,v2)));
disp(angNew)
  4 Comments
Paulo Oliveira
Paulo Oliveira on 21 Oct 2013
V1 is defined by P1 and P2 and V2 is defined by P1 and P7. As the point belong to a parallelepiped I know the angles, but I need a rotine to calculate the angles when I analyse a human motion. Do you understand me?
Vivek Selvam
Vivek Selvam on 21 Oct 2013
This is an idea. Play around and modify it. Feel free to ask any questions.
function planar3d
% points
x = [ 1 1 1;
2 1 2
9 7 3
8 4 5];
% legend
xyz = 0;
yz = 1;
xz = 2;
xy = 3;
% v1 = p1 & p3; v2 = p1 & p4 --> here xyz plane is same as xy plane
v1 = x(3,:)-x(1,:);
v2 = x(4,:)-x(1,:);
ang3 = planar2d(v1, v2, xyz);
ang2 = planar2d(v1, v2, xy);
disp(['ang3 = ' num2str(ang3) ', ang2 = ' num2str(ang2)]);
% v1 = p2 & p3; v2 = p2 & p4 --> here xyz plane is not same as xz plane
v1 = x(3,:)-x(2,:);
v2 = x(4,:)-x(2,:);
ang3 = planar2d(v1, v2, xyz);
ang2 = planar2d(v1, v2, yz);
disp(['ang3 = ' num2str(ang3) ', ang2 = ' num2str(ang2)]);
function ang = planar2d(v1,v2,plane)
% % plane
% xyz = 0;
% yz = 1;
% xz = 2;
% xy = 3;
if plane ~= 0 % reducing a plane is same as eliminating that coordinate
v1(plane) = 0;
v2(plane) = 0;
end
ang = rad2deg(atan2(norm(cross(v1,v2)),dot(v1,v2)));

Sign in to comment.

More Answers (0)

Categories

Find more on Resizing and Reshaping 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!