randomize timer()
 
sync on
sync rate 60
 
type startpt
   x as integer
   y as integer
endtype
 
dim startpt(3) as startpt
 
type pt
   x as integer
   y as integer
endtype
 
dim pt(0) as pt
 
msg$="This is a demonstration of a Fractal - Click to begin"
stage=1
 
do
 
   cls
 
   if stage=1
      if mouseclick()=1 and waitmouse=0
         waitmouse=1
         stage=2
         msg$="Start by choosing the first point of the triangle"
      endif
   endif
 
   if stage=2
      if mouseclick()=1 and waitmouse=0
         waitmouse=1
         startpt(1).x=mousex()
         startpt(1).y=mousey()
         stage=3
         msg$="Now the second point of the triangle"
      endif
   endif
 
   if stage=3
      if mouseclick()=1 and waitmouse=0
         waitmouse=1
         startpt(2).x=mousex()
         startpt(2).y=mousey()
         stage=4
         msg$="And finally the third point of the triangle"
      endif
   endif
 
   if stage=4
      if mouseclick()=1 and waitmouse=0
         waitmouse=1
         startpt(3).x=mousex()
         startpt(3).y=mousey()
         stage=5
         msg$="Now choose a point from where to start drawing the fractal"
      endif
   endif
 
   if stage=5
      if mouseclick()=1 and waitmouse=0
         waitmouse=1
         array insert at bottom pt(0)
         pt(1).x=mousex()
         pt(1).y=mousey()
         stage=6
         msg$="The fractal is created by moving halfway from the current point..."
      endif
   endif
 
   if stage=6
      if mouseclick()=1 and waitmouse=0
         waitmouse=1
         array insert at bottom pt(0)
         a=rnd(2)+1
         pt(2).x=(pt(1).x+startpt(a).x)/2
         pt(2).y=(pt(1).y+startpt(a).y)/2
         stage=7
         msg$="...to one of the three triangle points and drawing a dot"
      endif
   endif
 
   if stage=7
      if mouseclick()=1 and waitmouse=0
         stage=8
         counter=timer()
         msg$="this is then continued many times..."
      endif
   endif
 
   if stage=8
      if timer()>counter+200
         array insert at bottom pt(0)
         a=rnd(2)+1
         i=array count(pt(0))
         pt(i).x=(pt(i-1).x+startpt(a).x)/2
         pt(i).y=(pt(i-1).y+startpt(a).y)/2
         counter=timer()
      endif
      if array count(pt(0))=50
         stage=9
         msg$="and even faster..."
      endif
   endif
 
   if stage=9
      if timer()>counter+10
         array insert at bottom pt(0)
         a=rnd(2)+1
         i=array count(pt(0))
         pt(i).x=(pt(i-1).x+startpt(a).x)/2
         pt(i).y=(pt(i-1).y+startpt(a).y)/2
         counter=timer()
      endif
      if array count(pt(0))=1000
         stage=10
         msg$="Click to repeat 30,000 times... (May take a few seconds!)"
      endif
   endif
 
   if stage=10
      if mouseclick()=1 and waitmouse=0
         for i = 1001 to 30000
            array insert at bottom pt(0)
            a=rnd(2)+1
            i=array count(pt(0))
            pt(i).x=(pt(i-1).x+startpt(a).x)/2
            pt(i).y=(pt(i-1).y+startpt(a).y)/2
         next i
         stage=11
         msg$="and the fractal is shown!"
      endif
   endif
 
   if stage=11
      if mouseclick()=1 and waitmouse=0 then end
   endif
 
   ink rgb(255,0,0),1
   if stage>1 then dot startpt(1).x,startpt(1).y
   if stage>2 then dot startpt(2).x,startpt(2).y
   if stage>3 then dot startpt(3).x,startpt(3).y
 
   ink rgb(255,255,255),1
   if stage>4
      for i = 1 to array count(pt(0))
         dot pt(i).x,pt(i).y
      next i
   endif
 
 
   if waitmouse=1 and mouseclick()=0 then waitmouse=0
 
   ink rgb(0,0,255),1
   center text 320,460,msg$
 
 
   sync
 
loop