Recursion: 4 ways to get it working
#11
In order to talk about recursion, first you have to talk about recursion  Big Grin
10 PRINT "Hola! Smile"
20 GOTO 10
Reply
#12
Thumbs Up 
@TempodiBasic "Chess Pattern" Looks good!
b = b + ...
Reply
#13
(02-17-2023, 12:10 PM)Ikerkaz Wrote: In order to talk about recursion, first you have to talk about recursion  Big Grin

Then you have to know when to quit! Smile
b = b + ...
Reply
#14
Here is a Checkerboard fresh from my brain and QB64 IDE:
Code: (Select All)
_Title "Checkerboard by recursion" ' b+ 2023-02-17
' 8 X 80 = 640
Screen _NewImage(640, 640, 12) ' use 16 QB colors
Check 1, 1
Sleep
Sub Check (x, y)
    If (y Mod 2) Then
        If x Mod 2 Then c = 4 Else c = 0
    Else
        If x Mod 2 Then c = 0 Else c = 4
    End If
    Line ((x - 1) * 80, (y - 1) * 80)-Step(80, 80), c, BF
    Line ((x - 1) * 80, (y - 1) * 80)-Step(79, 79), 14, B
    x = x + 1
    If x > 8 Then y = y + 1: x = 1
    If y > 8 Then
        Exit Sub
    Else
        Check x, y
    End If
End Sub
b = b + ...
Reply
#15
Nice patterns. Nice checkerboard. Amazing what can be done in a few lines of code.

I like this -- having the program show what's going on with recursion:

Code: (Select All)
_Title "Recursion Demo" ' dcromley
Screen _NewImage(1024, 768, 12)
Color 0, 15
Cls

Print Chr$(13) + "Finally, factorial(5)=" + Str$(factorial(5))

Function factorial (n)
  Dim t ' t exists for the duration of THIS level of recursion
  Print "Getting factorial(" + Str$(n) + ")"
  If n = 1 Then
    Print "factorial(1) = 1"
    factorial = 1
  Else
    Print "Need factorial(" + Str$(n - 1) + ")"
    t = factorial(n - 1)
    Print "Got factorial(" + Str$(n - 1) + ") =" + Str$(t)
    factorial = n * t
  End If
End Function
___________________________________________________________________________________
I am mostly grateful for the people who came before me.  Will the people after me be grateful for me?
Reply
#16
@Bplus
Yes the old color scheme is very fashinating and so the the yellow border!

Here a screeenshot of our different chessboard


[Image: immagine-2023-02-18-101926240.png]

@dcromley
nice output about timing of phases of recursion
Reply
#17
here another simple example of recursion in tail for draw a circle
Code: (Select All)
_Title "Circle by recursion demo"
Screen _NewImage(1000, 1000, 32)
Randomize Timer
_ScreenMove 1, 1
Const Tail = 2, Head = 1
Dim Radius As Integer, centerX As Integer, centerY As Integer, Mode As Integer
Radius = 50
centerX = 500
centerY = 500
Mode = Tail
While InKey$ <> " "
    Locate 1, 1: Print "press spacebar to quit"
    Radius = Radius + 5
    colo~& = _RGB32((Rnd * 255) + 1, (Rnd * 255) + 1, (Rnd * 255) + 1)
    DrawPoint colo~&, 1, CInt(Sin(1) * 100), CInt(Cos(1) * 100), centerX, centerY, Radius
    _Limit 10
Wend
End

Sub DrawPoint (c~&, Sec As Single, Px As Integer, Py As Integer, Cx As Integer, Cy As Integer, Radius As Integer)
    If Sec < 360 Then Sec = Sec + 1 Else Exit Sub
    PSet (Cx, Cy), c~&
    PSet (Px + Cx, Py + Cy), c~&
    Px = CInt(Sin(Sec) * Radius)
    Py = CInt(Cos(Sec) * Radius)
    DrawPoint c~&, Sec, Px, Py, Cx, Cy, Radius ' this is an example of recursion in tail
End Sub
Reply




Users browsing this thread: 3 Guest(s)