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

#define NX 2000
#define NY 1200
#define N 10000000
#define SCALE (NX / 3)

int main(int argc,char **argv)
{
   int c,i,ix,iy;
   double x=0,y=0,x1,y1,s,r2,w2;
   double a = -0.74543, b = 0.11301;
   double p = 0.99;
   BITMAP *image,white={255,255,255,0},black={0,0,0,0};
   char fname[64];
   FILE *fptr;

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

   image = CreateBitmap(NX,NY);
   EraseBitmap(image,NX,NY,black);
   srand48((int)time(NULL));

   for (i=0;i<N;i++) {
      r2 = sqrt((x-a)*(x-a) + (y-b)*(y-b));
      w2 = atan2(y - b,x - a);
      
      s = -1;
      if (drand48() > p)
         s = 1;

      x1 = s * sqrt(r2) * cos(w2/2);
      y1 = s * sqrt(r2) * sin(w2/2);
      x = x1;
      y = y1;
      if (i < 100)
         continue;
      ix = x1 * SCALE + NX/2;
      iy = y1 * SCALE + NY/2;
      DrawPixel(image,NX,NY,ix,iy,white);
      ix = -x1 * SCALE + NX/2;
      iy = -y1 * SCALE + NY/2;
      DrawPixel(image,NX,NY,ix,iy,white);
   }

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


