필터 지우기
필터 지우기

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

조회 수: 1 (최근 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에서 Startup and Shutdown에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by