#include <unistd.h>
#include <gd.h>
#include <stdlib.h>
long **IMG;
long MAXITER;
int TAILLE,SEUIL;
double XMIN,XMAX,YMIN,YMAX;
/* --- polynôme cubique pour « parcourir » l'espace RVB --------------------- */
float PolyCubique (int n, float x) {
int i;
for (i = 0; i < n ; i++) {
x = 16 * x * (x - 0.75) * (x - 0.75);
}
return x;
}
/* --- index mandelbrot ----------------------------------------------------- */
long Mandelbrot (double x, double y, int f) {
int n, m;
long iter = 0;
double t, tx = 0, ty = 0;
while ( tx * tx + ty * ty < 4 && iter < MAXITER ) {
t = tx * tx - ty * ty + x;
ty = 2 * tx * ty + y;
tx = t;
if (f && iter > SEUIL) {
n = (tx - XMIN) / (XMAX - XMIN) * TAILLE ;
m = (ty - YMIN) / (YMAX - YMIN) * TAILLE;
if (tx > XMIN && tx < XMAX && ty > YMIN && ty < YMAX)
IMG[n][m]++;
}
iter++;
}
return iter;
}
int Max (int a, int b) {
if ( a < b )
return b;
return a;
}
int main (int argc, char *argv[]) {
gdImagePtr image;
int i,j,rouge,vert,bleu,max;
int palette = 0;
int densite = 0;
long k;
double t,x,y;
/* récupération des arguments */
sscanf(argv[1],"%d",&TAILLE);
sscanf(argv[2],"%d",&MAXITER);
sscanf(argv[3],"%d",&SEUIL);
sscanf(argv[4],"%d",&palette);
XMIN = atof(argv[5]);
XMAX = atof(argv[6]);
YMIN = atof(argv[7]);
YMAX = atof(argv[8]);
/* allocation de la mémoire image */
IMG = malloc(TAILLE * sizeof(int *));
for (i=0;i<TAILLE;i++)
IMG[i] = malloc(TAILLE * sizeof(int));
for (i=0;i<TAILLE;i++) {
for (j=0;j<TAILLE;j++) {
IMG[i][j] = 0;
}
}
image = gdImageCreate(TAILLE,TAILLE);
switch (palette) {
case 0 : for (i = 0; i <= 255 ; i++ ) {
t = (double) i / 255;
rouge = i;
vert = 255 * PolyCubique(1,t);
bleu = 255 * PolyCubique(2,t);
j = gdImageColorAllocate(image,rouge,vert,bleu);
}
break;
default : for(i=0;i<=255;i++)
j = gdImageColorAllocate(image,i,i,i);
break;
}
for ( k = 0 ; k < 100 * TAILLE * TAILLE ; k++) {
x = 4 * drand48() - 2;
y = 4 * drand48() - 2;
if (Mandelbrot(x,y,0) < MAXITER)
Mandelbrot(x,y,1);
}
max = 0;
for ( i=0 ; i<TAILLE ; i++)
for (j=0 ; j<TAILLE ; j++)
max = Max(max,IMG[i][j]);
fprintf(stderr,"Le maximum atteint est %d\n",max);
for(i = 0 ; i < TAILLE ; i++)
for(j = 0 ; j < TAILLE ; j++) {
t = IMG[j][i] / ( max / 2.5 ) ;
if (t > 1)
t = 1;
gdImageSetPixel(image,i,j, t * 255);
}
gdImagePng(image,stdout);
gdImageDestroy(image);
exit(0);
}