#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 5000000
#define SCALE (NX / 4)

/*
   Cantor set curved I.F.S.
   Union of circle curve set with Cantor dust set
*/

int main(int argc,char **argv)
{
   int i,j;
   int ix,iy;
   int n,p;

   double r,x=0.3,y=0.1,x1,y1;
   BITMAP *image,white={65535,65535,65535},black={0,0,0};
   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++) {
      p = ((rand() % 2) == 0 ? 1 : -1);
      r = sqrt(x*x + y*y);
      switch (rand() % 4) {
      case 0:
      case 1:
         x1 = x / r  - 1;
         y1 = p * y / r;
         break;
      case 2:
         x1 =  x / 3.0;
         y1 =  y / 3.0;
         break;
      case 3:
         x1 = x / 3.0 + 2.0 / 3.0;
         y1 = y / 3.0;
         break;
      }
      x = x1;
      y = y1;
      if (n < 100)
         continue;
      ix = NX / 6 + x * SCALE + NX / 2;
      iy = y * SCALE + NY / 2;
      DrawPixel(image,NX,NY,ix,iy,black);
      if (n % (N/100) == 0)
         fprintf(stderr,".");
   }
   fprintf(stderr,"\n");

   /* Write the image to a ppm file */
   if ((fptr = fopen("roger5.ppm","w")) == NULL) {
      fprintf(stderr,"Unable to open bitmap file\n");
      exit(0);
   }
   WriteBitmap(fptr,image,NX,NY,2);
   fclose(fptr);
   DestroyBitmap(image);
}

#include "bitmaplib.c"


