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

조회 수: 3 (최근 30일)
Sophia Rissberger
Sophia Rissberger 2019년 6월 3일
편집: Stephan 2019년 6월 3일
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