Filter löschen
Filter löschen

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

3 Ansichten (letzte 30 Tage)
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 Kommentar
Geoff Hayes
Geoff Hayes am 18 Okt. 2016
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.

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Kategorien

Mehr zu Startup and Shutdown finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by