Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.
|
Forum Index : Microcontroller and PC projects : MM/DM Recursion
Author | Message | ||||
TassyJim Guru Joined: 07/08/2011 Location: AustraliaPosts: 6097 |
I was curious to see if MM Basic could handle recursion so I adapted this fractal tree program. Not quite a Christmas tree but close enough. 100 'Title: Fractal Tree 110 newX=160 'Tree starting point 120 newY=220 130 angle = -30 140 delta = 10 150 length = 60 160 dir = 270 170 TurtleSpeed = 50 'speed options 180 rad=6.283185/360 'degrees to radians 190 dim stack(100) 'FIFO stack 200 stackpos=1 210 cls 220 line(newX, newY-1) - (newX, newY) 230 gosub 270 240 print "Subroutine called ";loopcount;" times." 250 end 260 270 '[drawtree] 280 loopcount = loopcount + 1 290 If (length > 0) Then 300 oldX=newX 310 oldY=newY 320 newX=oldX - cos(dir*rad)*length 330 newY=oldY + sin(dir*rad)*length 340 line - (newX, newY) 350 pause TurtleSpeed 360 dir=dir+angle 370 stack(stackpos)=length 380 stackpos=stackpos+1 390 length = length - delta 400 gosub 270 'recursive calling itself 410 dir=dir-(angle * 2) 420 gosub 270 'recursive calling itself 430 dir=dir+angle 440 stackpos=stackpos-1 450 length = stack(stackpos) 460 oldX=newX 470 oldY=newY 480 newX=oldX - cos(dir*rad+3.14159)*length 490 newY=oldY + sin(dir*rad+3.14159)*length 500 line - (newX, newY) 510 pause TurtleSpeed 520 EndIf 530 return Jim VK7JH MMedit MMBasic Help |
||||
Geoffg Guru Joined: 06/06/2011 Location: AustraliaPosts: 3194 |
MMBasic can keep track of up to 250 nested GOSUB's. This is allows for a reasonable level of recursion. For interest, you can have up to 20 nested FOR..NEXT loops and a similar number of nested DO..LOOP loops. And, while I am quoting numbers, you can define up to 250 variables. Geoff Geoff Graham - http://geoffg.net |
||||
larny Guru Joined: 31/10/2011 Location: AustraliaPosts: 346 |
Jim, That's an intersting programme. I have never heard of recursive calling before. But I can't see how it ever gets out of the 270 to 400 loop. Obviously I'm missing something. Len |
||||
Gadget Regular Member Joined: 22/06/2011 Location: AustraliaPosts: 70 |
Larny, If you look at the code there is an IF statement at line 290, if the condition is not met the program falls through to the EndIf statement at line 590 which is then followed by a Return statement. so the program returns to the point of call which may be within the routine itself. So what the routine does is to call itself until an end condition is met and then successively 'Returns' from each call to itself. hope this sheds some light on it. Terry |
||||
larny Guru Joined: 31/10/2011 Location: AustraliaPosts: 346 |
Thanks Terry, I thought that if the condition in the IF statement is false, then it would jump over line 300 & execute 310 next. 290 If (length > 0) Then
300 oldX=newX 310 oldY=newY But what I assume you're saying is that:- 1. my assumption is true if there is no endif later in the sequence. or 2. it goes to the endif if it is present - as you described. Is this correct? Len |
||||
Gadget Regular Member Joined: 22/06/2011 Location: AustraliaPosts: 70 |
Len, In this example, if the value of length is equal or less than zero none of the statements between the then statement and the endif statement are executed so execution goes from line 290 to line 520. I'm not sure if the mmbasic alows an if statement without an endif, i think if there isnt an endif then the code to be executed has to be on the same line as the if..then statement (someone correct me if I'm wrong). I'll have to try it when i get home. Terry |
||||
TassyJim Guru Joined: 07/08/2011 Location: AustraliaPosts: 6097 |
There are two choices for if: A one liner where the syntax is "IF condition THEN do something" In this case there is no ELSE and no ENDIF A multiline IF has more options: IF condition THEN do something ELSE do the other thing ENDIF ELSE is not needed but ENDIF is. There can also be ELSEIF in the mix to give more options for testing. Recursion is a good way to make unreadable code but it does have uses. If you want to see whats happening put a few PRINT statements in the loop. Jim VK7JH MMedit MMBasic Help |
||||
larny Guru Joined: 31/10/2011 Location: AustraliaPosts: 346 |
Thanks Terry & Jim, I think I understand it now. I'll try it next time I fire up the MM. Len |
||||
BobDevries Senior Member Joined: 08/06/2011 Location: AustraliaPosts: 266 |
Hi TassyJim, Actually it *is* possible to have ELSE on a one line IF statement..... IF VAR=5 THEN PRINT "5" ELSE PRINT "NOT 5" Regards, Bob Devries Dalby, QLD, Australia |
||||
ajkw Senior Member Joined: 29/06/2011 Location: AustraliaPosts: 290 |
indeed (in regards max nested gosub's) 140 delta = 1 150 length = 250 causes an error yet 140 delta = 1 150 length = 249 works ok. I was going to let it go but after some thought I concluded loopcount was going to max out at 3.40282347e+38 long before it finished, which would be somewhere around 3.232856e+492 !! I stopped it after about 30mins which had loop count at ~1e+06 Cheers, Anthony. Screenshot of another. Image is Delta 3, Length 50, turtlespeed = 0, 194s to draw. |
||||
Print this page |