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.
DDRM
Posts: 398
Joined: Mon 02 Apr 2018, 18:04

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.

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

### Re: Recursion is your friend

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
px=0
py=0
branches%=6
gens%=5
ldivisor=3
totallength=1
FOR x%=1 TO gens%-1
totallength+=(1/ldivisor)^x%
NEXT x%
length=INT(768/totallength)
REM length=243
PROCDraw(px,py,length,gens%,ldivisor,branches%,360/branches%)
REMPROCDraw(px,py,length,gens%)
END
:
:
DEFPROCDraw(px,py,l%,gen%,ldiv,branches%,da)
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%
PROCDraw(nx,ny,l%/ldiv,gen%-1,ldiv,branches%,da)
NEXT x%
ENDPROC
:
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