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 commentaires

Peter
Peter le 22 Fév 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 le 22 Fév 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?

Connectez-vous pour commenter.

 Réponse acceptée

per isakson
per isakson le 22 Fév 2014
Modifié(e) : per isakson le 22 Fév 2014

0 votes

.
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.

Plus de réponses (1)

Peter
Peter le 22 Fév 2014

0 votes

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!

Translated by