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

#define NX 1000
#define NY 1000
#define N 10000000
#define SCALE (NX / 7)

int main(int argc,char **argv)
{
   int n,ix,iy;
   double x=0.1,y=0,x1,y1,x2,y2,r2,m2;
   BITMAP *image,white={255,255,255},black={0,0,0};
   BITMAP colour;
   char fname[64];
   FILE *fptr;

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

   for (n=0;n<N;n++) {
      if (n % (N/100) == 0)
         printf(stderr,"%d\n",n);
      x2 = 0.25 - x / 3;
      y2 = -y / 3;
      r2 = sqrt(x2 * x2 + y2 * y2);
      m2 = atan2(y2,x2);
      if (rand() % 2 == 0) {
         x1 = 0.5 - sqrt(r2) * cos(m2 / 2);
         y1 = -sqrt(r2) * sin(m2 / 2);
      } else {
         x1 = 0.5 + sqrt(r2) * cos(m2 / 2);
         y1 = sqrt(r2) * sin(m2 / 2);
      }
      x = x1 / (x1 * x1 + y1 * y1);
      y = -y1 / (x1 * x1 + y1 * y1);
      ix = x * SCALE + NX/2;
      iy = y * SCALE + NY/2;
      if (n > 100)
         DrawPixel(image,NX,NY,ix,iy,black);
   }

   /* Write the image to a ppm file */
   sprintf(fname,"roger20.tiff");
   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"

