Getting several errors and bad output.

So the idea is I have this turtle. The turtle receives these commands R, L or F. (Right, Left, Forward)
If it's right, it turns 60 degrees clockwise, if it's left it turns counter clockwise. If it's F it moves forward one unit. The commands are coming from a recursion program I wrote to plot a snowflake.
This program, is supposed to output the coordinates after every time the turtle moves forward, but not when it turns left or right. I'm really stuck here. Can't figure out what's wrong. Not even sure if I get close to the outputs. Any help guys????!?!? Thanks so much!!!
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)
for k =1:length(commands)
if commands(j) == 'L'
theta = myLeft(theta);
elseif commands(j) == 'R'
theta = myRight(theta);
else
pos = myForward(pos,theta);
coords(k,1) = pos(1);
coords(j,2) = pos(2);
end
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

I’m not sure what you’re doing. It seems it never actually goes anywhere except forward in a straight line. It simply rotates left or right otherwise, but doesn’t move.
Shouldn’t the coordinate updates for myLeft and myRight be something similar to myForward?
The coordinates don't actually change when it goes left or right. It just rotates the direction that it will move the next time it goes forward. I've been messing with this program for so many hours. It's ruining my life right now haha.

Sign in to comment.

Answers (2)

Your mistake is in
coords(k,1) = pos(1);
coords(j,2) = pos(2);
The two first indices should be the same, and should reflect how many F commands have been found so far in the string.
You do not need a double-nested loop: you can do the processing in a single pass.

5 Comments

pos(k,1) = myForward(pos,theta);
If I do this, how do I update the coords array each iteration? And whenever I go this route I get dimension errors.
or would I do
coords(k,1) = pos(1)
coords(k,2) = pos(2)
this also runs me into dim errors.
else
coords(k,1) = myForward(pos(k,1),theta);
coords(k,2) = myForward(pos(k,2),theta);
>> A = myKochCurve(1)
A =
FLFRRFLF
>> myKochPts(A)
Attempted to access curPos(2); index out of bounds because numel(curPos)=1.
Error in myKochPts>myForward (line 58)
newPos = origin + [(curPos(1) + cos(curTheta)) , (curPos(2) +sin(curTheta))];
Error in myKochPts (line 30)
coords(k,1) = myForward(pos(k,1),theta);
When you eliminated the double loop in favour of a single loop, which loop variable did you use?
coords(k,:) = myForward(pos,theta);
should work if it was "k" that is being used as the counter of the number of F commands found so far. Remember to increment whichever counter it is you use.
so I set k=1:dim which is the counter for the number of Fs

Sign in to comment.

for j = 1:length(commands)
for k =1:dim
if commands(j) == 'L'
theta = myLeft(theta);
elseif commands(j) == 'R'
theta = myRight(theta);
else
coords(k,:) = myForward(pos(k,1),theta);

1 Comment

>> myKochPts(A)
Attempted to access curPos(2); index out of bounds because numel(curPos)=1.
Error in myKochPts>myForward (line 58)
newPos = origin + [(curPos(1) + cos(curTheta)) , (curPos(2)
+sin(curTheta))];
Error in myKochPts (line 30)
coords(k,:) = myForward(pos(k,1),theta);

Sign in to comment.

Tags

Asked:

on 22 Feb 2014

Commented:

on 22 Feb 2014

Community Treasure Hunt

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

Start Hunting!