This program isn't working.

2 ビュー (過去 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 Center および File ExchangeOrdinary Differential Equations についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by