我想在MATLAB中旋转一个矢量,然后检查原始旋转和旋转角度之间的角度:
v = [-1 -12 5]; %arbitrarily rotated vector
theta =30; %arbitrary angle to rotate with
R = [cosd(theta) -sind(theta) 0; sind(theta) cosd(theta) 0; 0 0 1]; %rotate around Z axis
vR = R*v'; %calculate the new vector
angle=atan2d(norm(cross(v,vR)),dot(v,vR));
%the angle between the old and rotated vector, also do normalisation before.
%atan2d is better to resolve extremely small angle
angle =
27.6588
%THIS is the problem
正如你所看到的那样,我以30°旋转但是在检查时它是不同的。
答案 0 :(得分:2)
您实际上并没有计算相同的角度。考虑输入向量为v = [0,0,1]的情况(即垂直线)。如果围绕z轴旋转垂直线30度,则再次获得相同的垂直线,因此vR = [0,0,1]。根据您的分析,v和vR之间的角度将为0,因为您正在计算两个相交的矢量之间的实际角度。
所以,如果你想计算两个向量之间的角度,那么我相信你的代码是正确的。但是,如果要计算特定帧(即z轴)中的旋转量,则必须先将v和vR投影到x-y平面上,然后才能使用公式:
>>> x = eval(input("something: "))
something: __import__('os').listdir()
>>> x
['az.php', 'so', 'form.php', '.htaccess', 'action.php' ...
编辑:注意你仍然无法获得垂直线情况的角度(解决方案中有一个奇点)。此外,我的建议仅适用于z轴旋转的情况。要对任何一般旋转轴执行此操作,只需要更多的数学运算:
假设您有一个由单位向量v = [-1 -12 5]; %arbitrarily rotated vector
theta =30; %arbitrary angle to rotate with
R = [cosd(theta) -sind(theta) 0; sind(theta) cosd(theta) 0; 0 0 1]; %rotate around Z axis
vR = R*v'; %calculate the new vector
angle=atan2d(norm(cross(v,vR)),dot(v,vR)); %calculate the angle between the vectors
% copy over the vectors and remove the z-component to project onto the x-y
% plane
v_xy = v;
v_xy(3) = 0;
vR_xy = vR;
vR_xy(3) = 0;
angle_xy=atan2d(norm(cross(v_xy,vR_xy)),dot(v_xy,vR_xy)); %calculate the angle between the vectors in the xy-plane
定义的轴,然后围绕轴a
旋转向量v
以获取新向量a
。将vR
和v
项目投放到vR
正常的平面上,您可以获得向量a
和p
:
pR
然后根据公式找到这些投影矢量之间的角度:
p = v - dot(v,a)*a;
pR = vR - dot(vR,a)*a;