#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 50000000
#define SCALE (NX / 2.5)

int main(int argc,char **argv)
{
   int n,a;
   double x,y,x1,y1;
   BITMAP *image,white={65535,65535,65535},black={0,0,0},bitmap;
   COLOUR colour;
   FILE *fptr;

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

   x = (rand() % 10000) / 10000.0;
   y = (rand() % 10000) / 10000.0;
   
   for (n=0;n<N;n++) {
      switch (a = (rand() % 4)) {
      case 0:
         x1 = x * y / 9;
         y1 = (x / 3 + 2 / 3.0) * (y / 3 + 2 / 3.0);
         break;
      case 1:
         x1 = (x / 3 + 2 / 3.0);
         y1 = y / 3;
         break;
      case 2:
         x1 = x / 3;
         y1 = y / 3 + 2 / 3.0;
         break;
      case 3:
         x1 = (x / 3 + 2 / 3.0) * (y / 3 + 2 / 3.0);
         y1 = x * y / 9;
         break;
      }
      x = x1 * sqrt(2.5);
      y = y1 * sqrt(2.5);
      colour = GetColour((double)a,0.0,3.0,1);
      bitmap.r = colour.r * 255;
      bitmap.g = colour.g * 255;
      bitmap.b = colour.b * 255;
      if (n < 100)
         continue;
      DrawPixel(image,NX,NY,
         (int)(x * SCALE + NX/10),
         (int)(y * SCALE + NY/10),bitmap);
   }

   /* Write the image to a ppm file */
   if ((fptr = fopen("roger8.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"
#include "paulslib.c"

