This program isn't working.

조회 수: 14 (최근 30일)
Peter
Peter 2014년 2월 22일
답변: Peter 2014년 2월 22일
The program turns a point left or right by 60 degrees and moves it forward based on the commands you input. L = Counter Clockwise turn, R = Clockwise turn. If its F it moves forward by 1 unit in the current direction of theta. So an input is from a recursion formula that gives LRRLFLFRF and stuff life that. Here is the code. I just don't see why I am getting the wrong coordinates out to the N by 2 array storing the new coordinates. It only stores a new coordinate if it's an F command. Anyway heres my code. Please help.
function [ coords ] = myKochPts( commands )
theta = 0;
dim = 1;
pos = [0 0];
for i = 1:length(commands)
if commands(i) == 'F'
dim = dim + 1;
end
end
coords = zeros(dim,2);
for j = 1:length(commands)
if commands(j) == 'L'
theta = myLeft(theta);
elseif commands(j) == 'R'
theta = myRight(theta);
else
pos=myForward(pos,theta);
coords = [coords;pos];
end
end
end
function [ newPos] = myForward(curPos,curTheta)
origin = [0,0];
newPos = origin + [(curPos(1) + cos(curTheta)) , (curPos(2) +sin(curTheta))];
end
function [ newTheta ] = myLeft( curTheta)
newTheta = curTheta + pi/3;
end
function [ newTheta ] = myRight( curTheta)
newTheta = curTheta - (pi/3);
end
  댓글 수: 2
Peter
Peter 2014년 2월 22일
Quick update!!
I am getting the correct output but it is putting a BUNCH of 0's in the beginning.
ans =
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
1.0000 0
1.5000 0.8660
2.0000 0
3.0000 0
3.5000 0.8660
3.0000 1.7321
4.0000 1.7321
4.5000 2.5981
5.0000 1.7321
6.0000 1.7321
5.5000 0.8660
6.0000 0.0000
7.0000 0.0000
7.5000 0.8660
8.0000 0.0000
9.0000 0.0000
Peter
Peter 2014년 2월 22일
I realized it's because I have the iteration set to the length of the commands array. So how do I make it so it doesn't add the 0s in the beginning?

댓글을 달려면 로그인하십시오.

채택된 답변

per isakson
per isakson 2014년 2월 22일
편집: per isakson 2014년 2월 22일
.
Try to change
coords = zeros(dim,2);
to
coords = zeros(0,2);
I guess dim is the number of rows with zeros at the beginning.

추가 답변 (1개)

Peter
Peter 2014년 2월 22일
HAHAHHAHAHAHAHAHAHHAHA THAT'S IT!!!!!!!! OMG THANK YOU!!!!!!!!!!!!!!!!!!!!!!!!!

카테고리

Help CenterFile Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by