Posts: 46
Threads: 9
Joined: Apr 2022
Reputation:
2
In order to talk about recursion, first you have to talk about recursion
10 PRINT "Hola! "
20 GOTO 10
Posts: 3,972
Threads: 177
Joined: Apr 2022
Reputation:
219
02-17-2023, 04:17 PM
(This post was last modified: 02-17-2023, 04:19 PM by bplus.)
@TempodiBasic "Chess Pattern" Looks good!
b = b + ...
Posts: 3,972
Threads: 177
Joined: Apr 2022
Reputation:
219
(02-17-2023, 12:10 PM)Ikerkaz Wrote: In order to talk about recursion, first you have to talk about recursion
Then you have to know when to quit!
b = b + ...
Posts: 3,972
Threads: 177
Joined: Apr 2022
Reputation:
219
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 + ...
Posts: 64
Threads: 12
Joined: Apr 2022
Reputation:
5
02-17-2023, 08:58 PM
(This post was last modified: 02-17-2023, 09:02 PM by dcromley.
Edit Reason: note
)
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?
Posts: 345
Threads: 24
Joined: Jul 2022
Reputation:
20
@Bplus
Yes the old color scheme is very fashinating and so the the yellow border!
Here a screeenshot of our different chessboard
@dcromley
nice output about timing of phases of recursion
Posts: 345
Threads: 24
Joined: Jul 2022
Reputation:
20
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
|