How to rotate a line?
32 次查看(过去 30 天)
显示 更早的评论
Is there a way to rotate a line of 30 degree? For example: x=1; y=[2 4 6 8 10];
Does it require a center point of rotation?(example center point is [2,6])Or does it requires the center of the line?
Thanks.
0 个评论
采纳的回答
Kevin Moerman
2012-2-6
This should answer your question. This code does what you want and shows you what happens depending on your choice of centre point of rotation. (Look up rotation matrices and direction cosine matrices for more information). This is an example for 3D rotation for 2D in the plane you could simplify and only use the Rz part.
Kevin
clear all; close all; clc;
%Example coordinates
y=[2 4 6 8 10];
x=ones(size(y));
%Vertices matrix
V=[x(:) y(:) zeros(size(y(:)))];
V_centre=mean(V,1); %Centre, of line
Vc=V-ones(size(V,1),1)*V_centre; %Centering coordinates
a=30; %Angle in degrees
a_rad=((a*pi)./180); %Angle in radians
E=[0 0 a_rad]; %Euler angles for X,Y,Z-axis rotations
%Direction Cosines (rotation matrix) construction
Rx=[1 0 0;...
0 cos(E(1)) -sin(E(1));...
0 sin(E(1)) cos(E(1))]; %X-Axis rotation
Ry=[cos(E(2)) 0 sin(E(2));...
0 1 0;...
-sin(E(2)) 0 cos(E(2))]; %Y-axis rotation
Rz=[cos(E(3)) -sin(E(3)) 0;...
sin(E(3)) cos(E(3)) 0;...
0 0 1]; %Z-axis rotation
R=Rx*Ry*Rz; %Rotation matrix
Vrc=[R*Vc']'; %Rotating centred coordinates
Vruc=[R*V']'; %Rotating un-centred coordinates
Vr=Vrc+ones(size(V,1),1)*V_centre; %Shifting back to original location
figure;
plot3(V(:,1),V(:,2),V(:,3),'k.-','MarkerSize',25); hold on; %Original
plot3(Vr(:,1),Vr(:,2),Vr(:,3),'r.-','MarkerSize',25); %Rotated around centre of line
plot3(Vruc(:,1),Vruc(:,2),Vruc(:,3),'b.-','MarkerSize',25); %Rotated around origin
axis equal; view(3); axis tight; grid on;
更多回答(1 个)
Kevin Moerman
2012-2-8
Correct, and instead of E(3) just use a_rad straight away. Also if you find this too complex you could use POL2CART instead (make sure you understand the coordinate system transformation e.g. what is the positive direction etc):
x=-1:0.1:1; y=x; a=30; a_rad=((a*pi)./180);
[THETA,R] = cart2pol(x,y); %Convert to polar coordinates
THETA=THETA+a_rad; %Add a_rad to theta
[xr,yr] = pol2cart(THETA,R); %Convert back to Cartesian coordinates
plot(x,y,'g-'); hold on; %Original
plot(xr,yr,'b-'); axis equal; %Rotated
Kevin
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 3-D Scene Control 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!