/*
 * Soubor:  20120202-Thu-mandelbrot.c
 * Datum:   02.02.2012 08:21
 * Autor:   Marek Nožka, nozka <@t> spseol <d.t> cz
 * Licence: GNU/GPL 
 * Úloha: 
 * Popis:   
 ****************************************************/
#define _ISOC99_SOURCE
#define _GNU_SOURCE
#include <stdio.h>
#include <complex.h>

/* ***************    Makra    ******************** */
#define SLOUPCU 1600
#define RADKU 1200


char mandel(double x,double y)
{
    unsigned int hloubka = 128;
    double complex C = x + y*I;
    double complex Z = 0;
    unsigned short int n = 0;
    while ( n < hloubka && cabs(Z)< 2 ) {
        n++;
        Z = Z*Z + C;
    }
    if (n <hloubka) {
        return '-'; // do množiny nepatří
    } else {
        return '+'; // do množiny patří
    }



}

/****************************************************
 *               Hlavní program.
 ****************************************************/
int main(void)
{
    int i, j;
    int r, s;
    double zoom = 1;
    double x0 = -0;
    double y0 = 0;
    char matice[RADKU][SLOUPCU];

/* - je černá 
 * + je bílá */

    /* naplním matici daty */
    unsigned int MIN = SLOUPCU > RADKU ? SLOUPCU : RADKU;
    for (r = 0; r < RADKU; r++) {
        for (s = 0; s < SLOUPCU; s++) {
            matice[r][s] = 
                mandel(2*((double)s-SLOUPCU/2.0)/(MIN*zoom)+x0, 
                       2*((double)r-  RADKU/2.0)/(MIN*zoom)+y0 );
        }
    }


//////////////////////////////////////////////////////////////
    /* uložím matici do obrázku */
    FILE *soubor;
    soubor = fopen("matice.xpm", "w");
    fprintf(soubor, "! XPM2\n%d %d 2 1\n", SLOUPCU, RADKU);
    fprintf(soubor, "- c #000000\n");
    fprintf(soubor, "+ c #ffffff\n");
    for (i = 0; i < RADKU; i++) {
        for (j = 0; j < SLOUPCU; j++) {
            fputc(matice[i][j], soubor);
        }
        fputc('\n', soubor);
    }

    fclose(soubor);


    return 0;
}

