MATLAB Answers

0

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

Asked by Sophia Rissberger on 3 Jun 2019
Latest activity Edited by Stephan
on 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)

  0 Comments

Sign in to comment.

2 Answers

Answer by Stephan
on 3 Jun 2019
Edited by Stephan
on 3 Jun 2019
 Accepted Answer

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

  0 Comments

Sign in to comment.


Answer by darova
on 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

  0 Comments

Sign in to comment.