Recursion is your friend

Here I will post a series of (mostly) simple challenges to amuse you in the run-up to Christmas. I'll try to post one each weekday.
Posts: 398
Joined: Mon 02 Apr 2018, 18:04

Recursion is your friend

Post by DDRM »

Can you draw something using these rules:
a) Starting from a central point, draw a line of length L = 243 in each of 6 equally spaced directions (like spokes on a wheel).
b) At the end of each spoke, repeat the process, but using a line length of L/3
c) Repeat step b until the length drops to 1, or for a controlled number of generations

Hint: write a subroutine that calls itself recursively. Don’t try to do it in the order suggested above (i.e. don’t draw all the first-level spokes, then the second level ones, etc).

Extensions: can you generalise the routine to handle different numbers of branches, different ratios between the lengths, different numbers of generations? Can you make it scale gracefully to whatever mode you choose? Can you add colour, and maybe changing line thicknesses? What happens if you plot circles of the given radius instead of lines? What happens if you allow lines to get LONGER with each generation

Comment: This is heading towards some programs I did to generate biomorphs, similar to those described by Richard Dawkins in his book “the blind watchmaker”. I allowed lengths to change by different ratios in horizontal and vertical directions, and allowed the angle between branches to change (at a controllable rate) between generations. It’s amazing what a variety of shapes you can generate that way.

Posts: 398
Joined: Mon 02 Apr 2018, 18:04

Re: Recursion is your friend

Post by DDRM »

Let it snow!

Here's my version. Feel free to play with the parameters for the number of branches, generations, etc! The ldivisor parameter determines how fast the branches shorten - changing it to 2 will mean each successive generation of branches will be half the previous one, for example.

If you want the "minimal" version, uncomment the second version of PROCDraw (and comment out the first one), and uncomment the line forcing the length to 243.

Code: Select all

      MODE 23
      ORIGIN 1024,768
      FOR x%=1 TO gens%-1
      NEXT x%
      REM length=243
      LOCAL x%,nx,ny
      IF gen% = 0 THEN ENDPROC
      FOR  x%=0 TO (branches%-1)
        VDU 23,23,3*(gen%-1)+1;0;0;0;
        GCOL gen%
        LINE px,py,nx,ny
        REM CIRCLE px,py,l%
      NEXT x%
      REM DEFPROCDraw(px,py,l%,gen%)
      REM REM Simple version: assumes 6-fold symmetry and divide length by 3
      REM LOCAL x%,nx,ny
      REM IF gen% = 0 THEN ENDPROC
      REM FOR  x%=0 TO 5
      REM nx=px+l%*COSRAD(x%*60)
      REM ny=py+l%*SINRAD(x%*60)
      REM LINE px,py,nx,ny
      REM PROCDraw(nx,ny,l%/3,gen%-1)
      REM NEXT x%

Posts: 75
Joined: Wed 10 Apr 2019, 17:36

Re: Recursion is your friend

Post by Pete »

That was good to see the computer draw that. Initially I encountered the error "Invalid arguments" as I hadn't REMed out the first PROCdraw in the program. I learnt a bit more about recursion tonight when I looked up "what is recursion?". Wikipedia said: "Recursion occurs when a thing is defined in terms of itself or of its type. Recursion is used in a variety of disciplines ranging from linguistics to logic. The most common application of recursion is in mathematics and computer science, where a function being defined is applied within its own definition." Will be interesting to see the biomorph programs.