#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 / 10)

int main(int argc,char **argv)
{
   int n,ix,iy;
   double x=0,y=0,x1=0,y1=0,denom;
   BITMAP4 *image,white={255,255,255},black={0,0,0};
   char fname[64];
   FILE *fptr;

   if (argc < 1) {
      fprintf(stderr,"Usage: %s m\n",argv[0]);
      exit(-1);
   }

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

   for (n=0;n<N;n++) {
      if ((n % (N/10)) == 0)
         fprintf(stderr,"%d\n",n);
      switch (rand()%2) {
      case 0:
         x1 = y / SQRT2 + 1.5;
         y1 = -x / SQRT2 + 1.5;
         break;
      case 1:
         x1 = -y / SQRT2 + 0.5;
         y1 = x / SQRT2 + 0.5;
         break;
      }
      switch (rand()%2) {
      case 0:
         x = x1 - 2;
         y = y1;
         break;
      case 1:
         denom = (x1 - 2) * (x1 - 2) + y1 * y1;
         x = (x1 - 2) / denom;
         y = y1 / denom;
         break;
      }
      if (n < 100)
         continue;
      ix = (x+1.0) * SCALE + NX/2;
      iy = (y-0.75) * SCALE + NY/2;
      Draw_Pixel(image,NX,NY,ix,iy,black);
   }

   /* Write the image to a tga file */
   sprintf(fname,"roger.tga");
   if ((fptr = fopen(fname,"w")) == NULL) {
      fprintf(stderr,"Unable to open image file\n");
      exit(0);
   }
   Write_Bitmap(fptr,image,NX,NY,12);
   fclose(fptr);
   Destroy_Bitmap(image);

   exit(0);
}

