[= Demo - Recursive Circles Command: $prg$ -bmp 800x800 $src$ Purpose: Show the use of recursion. =] [= set up some general constants =] Private Const Number StartRadius = 150; Private Const Number MinRadius = 2; Private Const Number Scale = 0.5; Program() Begin [= do the initial call for a centred circle =] RecCircle(StartRadius) => { Canvas.width / 2, Canvas.height / 2 }; OnError [= standard error reporting =] Output "**** An error occured"; Output Status; End; [= REC CIRCLE ---------- Draw recursive circles. =] Private Shape RecCircle(Number radius, Number direction = Null) [= local working variables =] Number newdir, newrad, shift, angle; Begin [= draw the main circle =] Circle(radius); [= rescale for attached circles =] newrad := radius * Scale; [= if too small stop the recursion =] If newrad < MinRadius Then Return; EndIf; [= work out how far to offset the new centre =] shift := radius + newrad; [= do all the attached circles =] For newdir From 0 To 3 Do [= if attached to original or not going back =] If direction == Null Or ( newdir + 2 ) Mod 4 <> direction Then [= every 90 degrees - 45 to make diagonal =] angle := 90 * newdir + 45; [= draw attached recursive circle at new location =] RecCircle(newrad, newdir) => { shift * Sin(angle), shift * Cos(angle) }; EndIf; EndFor; End;