#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 100000

int main(int argc,char **argv)
{
   int i,j,m=3;
   double s,denom,x=1,y=1,x1,y1;
   double a[25],b[25];
   double scale=1500;
   BITMAP *image,white={65535,65535,65535},black={0,0,0};
   FILE *fptr;

   image = CreateBitmap(NX,NY);
   EraseBitmap(image,NX,NY,white);
   srand(time(NULL));

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

   for (i=0;i<N;i++) {
      j = rand() % m;
      s = fabs(fabs(cos(atan2(y,x))) - 0.1);
      denom = pow(pow(fabs(x),s) + pow(fabs(y),2*(s-1)),1/s);
      x1 = x / denom + a[j];
      y1 = y / denom + b[j];
      x = x1;
      y = y1;
      if (i < 100)
         continue;
      DrawPixel(image,NX,NY,(int)(x*scale+NX/2),(int)(y*scale+NY/2),black);
      if (i % (N/100) == 0)
         fprintf(stderr,"%d %d %g %g %g %g\n",i,j,s,denom,x,y);
   }
   
   /* Write the image to a ppm file */
   if ((fptr = fopen("roger1.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"
