Effacer les filtres
Effacer les filtres

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

3 vues (au cours des 30 derniers jours)
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 commentaire
Geoff Hayes
Geoff Hayes le 18 Oct 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.

Connectez-vous pour commenter.

Réponses (0)

Catégories

En savoir plus sur Startup and Shutdown dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by