#include "stdio.h"
#include "stdlib.h"
#include "math.h"
#include <sys/types.h>
#include <time.h>
#include "paulslib.h"
#include "bitmaplib.h"

#define NX 2000
#define NY 2000
#define N 100000000
#define SCALE (NX / 5)

int main(int argc,char **argv)
{
   int i,m,n,c,ix,iy;
   double x=1,y=1,x1,y1;
   double r,ra;
   double a[25],b[25];
   BITMAP *image,white={255,255,255},green={0,100,0},black={0,0,0};
   BITMAP colour;
   char fname[64];
   FILE *fptr;

   if (argc < 2) {
      fprintf(stderr,"Usage: %s m\n",argv[0]);
      exit(-1);
   }
   m = atoi(argv[1]);
   if (m < 2 || m > 12) {
      fprintf(stderr,"m out of range, must be between 2 and 12\n");
      exit(-1);
   }

   /* Create the image */
   image = CreateBitmap(NX,NY);
   EraseBitmap(image,NX,NY,white);
   srand(time(NULL));

   for (i=0;i<m;i++) {
      a[i] = cos(2 * PI * i / (double)m);
      b[i] = sin(2 * PI * i / (double)m);
   }

   r = sqrt(1.05) * sqrt((double)m);
   for (n=0;n<N;n++) {
      if ((n % (N/100)) == 0)
         fprintf(stderr,"%d\n",n);
      c = rand() % m;
      ra = sqrt(3.0) * sqrt(x*x+y*y);
      if (c % 2 == 0) {
         x1 = - (- x / r + y / (r * ra)) + a[c];
         y1 = - (- x / (r * ra) - y / r) + b[c];
      } else {
         x1 = x / r + y / (r * ra) + a[c];
         y1 = -x / (r * ra) + y / r + b[c];
      }
      x = x1;
      y = y1;
      if (n < 100)
         continue;
      ix = x * SCALE + NX/2;
      iy = y * SCALE + NY/2;
      DrawPixel(image,NX,NY,ix,iy,black);
   }

   /* Write the image to a ppm file */
   sprintf(fname,"roger13_%d.tiff",m);
   if ((fptr = fopen(fname,"w")) == NULL) {
      fprintf(stderr,"Unable to open bitmap file\n");
      exit(0);
   }
   WriteBitmap(fptr,image,NX,NY,5);
   fclose(fptr);
   DestroyBitmap(image);
}

#include "bitmaplib.c"
#include "paulslib.c"


