#include <stdio.h>
#include "complex.h"

enum {
    WIDTH= 60,   /* Appropriate dimensions for terminal window we are printing on */
    HEIGHT= 24
};

#define REAL_MIN -1.5
#define REAL_MAX 1.5
#define IMAG_MIN -1.5
#define IMAG_MAX 1.5

int escape_time (CMPLX);
/* The "escape time" for the mandelbrot set equation for the complex point c returns -1 for "does not escape"*/

int main()
{
   double real, imag;   /* real and imaginary co-ordinates we are looking at*/
   CMPLX c;   /* C in the mapping z->z^2+c */
   int time;  /* How quickly do we "escape" */
   int i, j;
   
   for (i= 0; i < HEIGHT; i++) {
       for (j= 0; j < WIDTH; j++) {
           imag= ((IMAG_MAX-IMAG_MIN)*(double)i/HEIGHT) +IMAG_MIN;
           real= ((REAL_MAX-REAL_MIN)*(double)j/WIDTH) +REAL_MIN;
           c= create_complex (real, imag);
           time= escape_time(c);
           if (time == -1) {
               putchar ('+');
           } else if (time > 25) {
               putchar ('O');
           } else if (time > 6) {
               putchar ('o');
           } else if (time > 2) {
               putchar ('.');
           } else {
               putchar (' ');
           }
       }
       putchar ('\n');
   }
   return 0;
}

int escape_time (CMPLX c)
/* Calculates the "escape time" for the complex point c - returns -1 for "doesn't escape" */

{
   CMPLX z;  /* See mandelbrot explanation */
   int i;
   z= create_complex (0,0);
   for (i= 0; i < 100; i++) {
       if (magnitude(z) > 2)
           return i;
        z= multiply (z,z);
        z= add(z,c);
   }
   return -1;
}


