problem with matrix rotation
Show older comments
hi to all, i have a problem when i try to rotate a line or an object the problem is centre of rotation that is not what i want. with teta=0 i have a line that starts at (15,15) and go to right. instead if i use teta=20 i want i line that start always in (15,15) with an angle of 20 degree but the start point change.WHY?????? i use tha rotation matrix. can somebody help me please?????? here is my code
x0_center=0;
y0_center=0;
w=30;
ha=30;
teta=0;
%r=rectangle('Position',[x0_center,y0_center,w,ha], 'LineWidth',3,'LineStyle','--');
figure;
DrawRectangle([x0_center,y0_center,w,ha,teta]);
axis([0 40 0 40])
axis equal
%set(r,'edgecolor','r')
%xlim([0,25])
%ylim([0,15])
hold on
x0=15;
y0=15;
x=20;
y=15;
n=10;
k=1;
teta=20;
xvet=linspace(x0,x,n);
yvet=linspace(y0,y,n);
P = [xvet;yvet];
theta=(teta*pi)/180;
ct = cos(theta);
st = sin(theta);
R = [ct -st;st ct];
P_t = R * P;
xvet_t=P_t(1,:);
yvet_t=P_t(2,:);
for index=1:10
xvet1(index)=xvet_t(index);
yvet1(index)=yvet_t(index);
h1(k) = line(xvet1, yvet1,'EraseMode','none');
set(h1, 'XData', xvet1, 'YData', yvet1);
pause(0.05);
end
x0=x;
y0=y;
xvet = NaN(1, n);
yvet = NaN(1, n);
xvet1 = NaN(1, n);
yvet1 = NaN(1, n);
Answers (1)
Your current code performs a rotation about the origin, wherever that happens to be. To specify [15,15] as the center of rotation, you can use FEX:AxelRot as follows
P(3,:)=0; %embed in 3D
P_t=AxelRot(P,20,[0 0 1],[15,15,0]); %perform the rotation about [15,15,0]
and then the rest of your code.
Categories
Find more on Interactions, Camera Views, and Lighting in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!