How to calculate a angle between two vectors in 3D
14 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Paulo Oliveira
am 18 Okt. 2013
Kommentiert: Vivek Selvam
am 21 Okt. 2013
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?
2 Kommentare
Akzeptierte Antwort
Vivek Selvam
am 18 Okt. 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 Kommentare
Vivek Selvam
am 21 Okt. 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)));
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Resizing and Reshaping Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!