Trying to make a recursive tree function that calls on itself using OOP, where the object is a turtle.

3 次查看(过去 30 天)
I have a separate turtle class created already. Here is my code.
function [obj] = tree(obj,n) % Creates tree to the order n
obj = Turtle();
t = Turtle();
a = 60; % angle in degrees
d = 4; % distance turtle travels
% If n is 0,
if n <= 0
% do nothing.
t = t.fd(0);
% Otherwise,
else
% drive forward d,
t = t.fd(d);
% turn left a degrees,
t = t.lt(a);
% draw a level n-1 tree,
obj = tree(n-1)
% turn right 2a degrees,
t = t.rt(2*a);
% draw a level n-1 tree,
obj = tree(n-1)
% turn left a degrees,
t = t.lt(a);
% and drive backward d
t = t.bk(d);
end
end
Matlab gives me an error saying "Undefined function or variable 'Turtle'". Does anyone know how to fix this?
  1 个评论
Geoff Hayes
Geoff Hayes 2016-10-18
Kyle - you indicate that you want to make a recursive tree function that calls on itself but your function tree never calls itself. It does call the function (or is this an object that you are instantiating?) called Turtle. The MATLAB error message is indicating that this function (or class) cannot be found within the MATLAB search path. Is Turtle something that you have written? If so, then you need to add this file to the search path so that your tree function can find it.

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Software Development Tools 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by