How to rotate a line?
30 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Risposta accettata
Kevin Moerman
il 6 Feb 2012
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;
Più risposte (1)
Kevin Moerman
il 8 Feb 2012
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 Commenti
Vedere anche
Categorie
Scopri di più su 3-D Scene Control in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!