This program isn't working.

2 views (last 30 days)
Peter
Peter on 22 Feb 2014
Answered: Peter on 22 Feb 2014
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 Comments
Peter
Peter on 22 Feb 2014
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 on 22 Feb 2014
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?

Sign in to comment.

Accepted Answer

per isakson
per isakson on 22 Feb 2014
Edited: per isakson on 22 Feb 2014
.
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.

More Answers (1)

Peter
Peter on 22 Feb 2014
HAHAHHAHAHAHAHAHAHHAHA THAT'S IT!!!!!!!! OMG THANK YOU!!!!!!!!!!!!!!!!!!!!!!!!!

Tags

Community Treasure Hunt

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

Start Hunting!