gosub initialise
gosub main
exit

label main
 repeat
  de=de-1
  if de=0 then
   de=cd
   dm=mod(dm+1,mdm)
  fi

  ang=ang+1
  if ang>360 ang=ang-360

  gosub flipscreen
  clear window

  for a=1 to mp
   ax=si(ang+a*mlt)*200
   ay=co(ang+a*mlt)*200
   avx=si(ang*10)*80
   avy=co(ang*10)*80
   aaa=a+1
   if a=mp aaa=1
   dx=si(ang+aaa*mlt)*200
   dy=co(ang+aaa*mlt)*200
   dvx=co(ang*10)*80
   dvy=si(ang*10)*80
   drawbezier(ax,ay,dx,dy,avx,avy,dvx,dvy)
  next a
 until (1=2)
return

label flipscreen
 setdispbuf cb
 cb=1-cb
 setdrawbuf cb
return

sub drawbezier(ax,ay,dx,dy,avx,avy,dvx,dvy)
 new curve
 bx=ax+avx
 by=ay+avy
 cx=dx+dvx
 cy=dy+dvy

 for t=0 to 1 step 0.05
  aa=t
  bb=1-t
  x=ax*bb^3+3*bx*bb^2*aa+(3*cx)*bb*aa^2+dx*aa^3
  y=ay*bb^3+3*by*bb^2*aa+(3*cy)*bb*aa^2+dy*aa^3
  setrgb 1,x,y,-x-y
  si=si(ang)*20
  if dm=0 rect x,y to x+1,y+1
  if dm=1 line to x,y
  if dm=2 line 0,0 to x,y
  if dm=3 line x,y to -x,-y
  if dm=4 rect x-si,y-si to x+si,y+si
  if dm=5 line x,y to y,x
  if dm=6 line x,y to x*4,y*4
  if dm=6 line 0,0 to y,x
  if dm=7 line x,0 to 0,y
  if dm=8 line x,y to si(ang*10)*100,co(ang*10)*100
 next t
end sub

label initialise
 open window 640,512
 window origin "cc"
 mdm=9
 cd=200
 de=cd

 dim co(3600),si(3600)
 f=3.14/180
 for a=0 to 3600
  co(a)=cos(a*f)
  si(a)=sin(a*f)
 next a

 mp=4
 mlt=360/mp
return

