Rem Dark Basic Classic Project:
Rem Created: 5/4/2007 3:42 AM
Rem Title: fern fractal
Rem Author: Phaelax
 
set display mode 1024,768,32
 
 
ink rgb(0,255,0),0
 
 
startAngle# = 90
bendAngle# = 7
branchAngle# = 50
height# = 150
 
repeat
   cls
 
   fern(512.0, 668.0, 7, startAngle#, bendAngle#, branchAngle#, height#)
 
   timestamp = timer()
   while timestamp+5000 > timer()
      rem display final image for 5 seconds
   endwhile
 
   rem create new random(somewhat) fern
   startAngle# = 65 + rnd(50)
   bendAngle# = -8 + rnd(20)
   branchAngle# = 30 + rnd(40)
   height# = 100 + rnd(100)
 
until breaker() = 1
end
 
function breaker()
   if mousemovex() <> 0 then exitfunction 1
   if mousemovey() <> 0 then exitfunction 1
   if mousemovez() <> 0 then exitfunction 1
   if mouseclick() <> 0 then exitfunction 1
   if scancode() <> 0 then exitfunction 1
endfunction 0
 
 
REM ====== FERN FRACTAL =======
REM X,Y         - starting position for fern, root of first stem
REM passes      - number of iterations
REM startAngle  - angle to start drawing on this pass
REM bendAngle   - overall bending angle of the whole leaf
REM branchAngle - angle to branch off each stem at
REM height      - starting height
function fern(x#, y#, passes, startAngle#, bendAngle#, branchAngle#, height#)
   spKey = spacekey()
   if spKey = 1 then passes = 0
 
   rootAngle# = wrapvalue(startAngle# - bendAngle#)
 
   x2# = x# + cos(rootAngle#)*height#
   y2# = y# - sin(rootAngle#)*height#
   line x#,y#,x2#,y2#
 
   height# = height#*0.5
 
   x3# = x# + cos(wrapvalue(rootAngle#+branchAngle#))*height#
   y3# = y# - sin(wrapvalue(rootAngle#+branchAngle#))*height#
   line x#,y#,x3#,y3#
 
   x4# = x# + cos(wrapvalue(rootAngle#-branchAngle#))*height#
   y4# = y# - sin(wrapvalue(rootAngle#-branchAngle#))*height#
   line x#,y#,x4#,y4#
 
   if passes > 1 and spKey = 0
      fern(x2#,y2#,passes-1, rootAngle#, bendAngle#, branchAngle#, height#)
      fern(x3#,y3#,passes-1, wrapvalue(rootAngle#+branchAngle#), bendAngle#, branchAngle#, height#)
      fern(x4#,y4#,passes-1, wrapvalue(rootAngle#-branchAngle#), bendAngle#, branchAngle#, height#)
   endif
 
endfunction