Why doesn't this code rotate and transform the spiral?

3 次查看(过去 30 天)
clear
clc
clf
t = linspace( 0,4*pi,1000)
r=@(t) sqrt(t)
x= r(t).*cos(t)
y= r(t).*sin(t)
plot(x,y)
%turns x and y into a 3xn matrix
pts=[x;y;ones(1,length(x))]
%create matrix
mTrans=eye(3,3);
%declare translation variables
dx=5;
dy=1.5;
%apply to matrix
mTrans(1,3)=dx;
mTrans(2,3)=dy;
%create matrix
mRot=eye(3,3);
%define rotation angle
theta=pi/6;
%change matrix
mRot(1,1)=cos(theta);
mRot(1,2)=sin(theta);
mRot(2,1)=-sin(theta);
mRot(2,2)=cos(theta);
%new points after tranformation and rotation
ptsNew=mTrans*mRot*pts
hold on
plot(pts)

采纳的回答

Stephan
Stephan 2019-6-3
编辑:Stephan 2019-6-3
No for loop is needed:
t = linspace( 0,4*pi,1000);
r=@(t) sqrt(t);
x = r(t).*cos(t);
y = r(t).*sin(t);
plot(x,y)
%turns x and y into a 3xn matrix
pts=[x;y;ones(1,length(x))];
%create matrix
mTrans=eye(3);
%declare translation variables
dx=5;
dy=1.5;
%apply to matrix
mTrans(1,3)=dx;
mTrans(2,3)=dy;
%define rotation angle
theta=pi;
%rotation matrix
mRot = [cos(theta), -sin(theta), 0; sin(theta), cos(theta), 0; 0, 0, 1];
%new points after tranformation and rotation
ptsNew=mTrans*mRot*pts;
hold on
plot(ptsNew(1,:),ptsNew(2,:))
hold off

更多回答(1 个)

darova
darova 2019-6-3
Because you have to mulptiply each set of point separately
ptsNew = zeros(size(pts));
for i = 1:1000
ptsNew(:,i) = mTrans*mRot*pts(:,i);
end
hold on
plot(ptsNew(1,:),ptsNew(2,:))
hold off

类别

Help CenterFile Exchange 中查找有关 Calculus 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by