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; endend 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 endendfunction [ newPos] = myForward(curPos,curTheta)origin = [0,0];newPos = origin + [(curPos(1) + cos(curTheta)) , (curPos(2) +sin(curTheta))];endfunction [ newTheta ] = myLeft( curTheta)newTheta = curTheta + pi/3;endfunction [ newTheta ] = myRight( curTheta)newTheta = curTheta - (pi/3);end
Best Answer