How to rotate a line?
27 ビュー (過去 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!