Author Topic: Apple Dolphin  (Read 2047 times)

Cosmo

  • Guest
Apple Dolphin
« on: February 03, 2014, 12:49:45 am »
Hello,

« Last Edit: December 12, 2014, 05:01:25 pm by PeterMaria »

Cybermonkey

  • Administrator
  • *****
  • Posts: 0
Re: Apple Dolphin
« Reply #1 on: February 03, 2014, 04:20:21 pm »
Maybe this can help: http://lua-users.org/lists/lua-l/2009-03/msg00439.html
Recursion in Lua.

Tomaaz

  • Guest
Re: Apple Dolphin
« Reply #2 on: February 03, 2014, 11:23:54 pm »
Hello Markus,

EGSL knows no recursion ?

Of course, you can use recursion in EGSL, because you can use recursion in Lua (is there any serious programming language in which using recursion is not possible?). The problem in your example is variable scope. In Lua variables defined inside a function are by default global, while in other languages they may be by default local. You need to remember that when you copy/paste the code written in other language. Just add local to x2 and y2 and it'll be working properly.

Code: [Select]
openwindow(600,600,0,"Tree")
backcolor(240,255,255)

function tree(x1, y1, a, d)
   if d ~= 0 then
      local x2 = x1 + math.cos(math.rad(a)) * d * 10
      local y2 = y1 + math.sin(math.rad(a)) * d * 10
      color(170,108,0)
      line(x1, y1, x2, y2)
      redraw()
      tree(x2, y2, a - 20, d - 1)
      tree(x2, y2, a + 20, d - 1)
   end
end

tree(300, 550, -90, 9)
redraw()
inkey()
« Last Edit: February 03, 2014, 11:28:10 pm by Tomaaz »

Tomaaz

  • Guest
Re: Apple Dolphin
« Reply #3 on: February 04, 2014, 01:03:01 am »
I already had the solution, but...

... you just didn't want to waste your time for posting it. That's reasonable...

...there is no difference with local declaration.

What do you mean? Is it still not working? ???

By the way, there is no copy / paste,  I am not Aurel.  ;D

Well, then you should know that someone copied your example and posted it on Rosetta Code. Just have a look at Python, Ruby or PHP versions. ;)