How to rotate a line?
조회 수: 30 (최근 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!