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

3 Ansichten (letzte 30 Tage)
Sophia Rissberger
Sophia Rissberger am 3 Jun. 2019
Bearbeitet: Stephan am 3 Jun. 2019
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)

Akzeptierte Antwort

Stephan
Stephan am 3 Jun. 2019
Bearbeitet: Stephan am 3 Jun. 2019
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

Weitere Antworten (1)

darova
darova am 3 Jun. 2019
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

Community Treasure Hunt

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

Start Hunting!

Translated by