{
 ********************
 * fractal polygons *
 ********************

      Author: David Benn,
           C: 15th August 1990
   ACE BASIC: 5th,22nd July 1992

   Algorithm: C. Gross
}

const DEPTH  = 2
const WDTH   = 320
const HEIGHT = 225

longint sides,finished
dim xV&(9),yV&(9)

SUB GetSides 
shared sides,finished
const TRUE = -1

   cls
   locate 2,2
   color 2,0
   prints "How many sides [3..9, or q to quit]?"
   repeat
     op$=inkey$
     sides=val(op$)    
   until (sides>=3 and sides<=9) or ucase$(op$)="Q"
   if ucase$(op$)="Q" then finished=TRUE 
END SUB

SUB MakeVertices 
shared xV&,yV&,sides
longint x,y,count
single p,r,z

 z=1
 p=360.0/sides

   for count=1 to sides
      r=p*z/57.2957795
      x=fix(sin(r)*100.0)
      xV&(count)=x+160
      y=fix(cos(r)*99.0)
      yV&(count)=y+100
      z=z+1.0
   next
END SUB

SUB DoFractal 
shared xV&,yV&,sides
longint iteration,r,x,y,c,d,t

   iteration=25000&
   x=xV&(1)
   y=yV&(1)
   t=0&

   cls 

   color 3,0
   locate 26,6
   prints "Press left mouse button to stop."

   color 1,0

   while iteration >= 1& and not mouse(0)
      iteration=iteration-1&
      r=int(rnd*3)+1&
      if r=1& then t=t
      if r=2& then t=t+1&
      if r=3& then t=t-1&

      if t > sides then t=t-sides
      if t < 1& then t=sides
      
      { right shift by 1 is faster 
        than division by 2 }       
      c = shr((x+xV&(t)),1)
      d = shr((y+yV&(t)),1)

      pset (c,d)

      x=c
      y=d
   wend

   color 2,0
   locate 25,1
   for i=1 to 40:prints " ";:next
   locate 25,16
   prints "Hit a key.";
   while inkey$="":wend
END SUB

{ ** main ** }

screen 1,WDTH,HEIGHT,DEPTH,1

palette 0,0,0,0
palette 1,1,1,1
palette 2,0,1,0
palette 3,1,0,0

   repeat
    GetSides 
    if not finished then
      MakeVertices
      DoFractal 
    end if
   until finished

screen close 1
