This program isn't working.

15 次查看(过去 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