//*************************************//
//******** created by mamo139 *********//
//*** http://mamo139.altervista.org ***//
//*************************************//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *** crea_bmp(char *nome, long x, long y);
void disegna_bmp(char *nome, char *** array, long x, long y);
struct complesso
{
double r;
double i;
};
struct complesso julia;
int main (void){
char immagine_name[]="out_julia.bmp";
//*********** PARAMETRI *************//
long larghezza = 1500;
long altezza = 1500;
long deep = 1000;
julia.r = -0.835;
julia.i = -0.2321;
double y1 = 2;
double x0 = -2, x1 = 2;
double y0 = -2;
//**********************************//
//aggiustamento parametri
double agg_temp;
agg_temp=y1;
y1=y0*(-1);
y0=agg_temp*(-1);
//**********************************//
char *** matrice;
matrice = crea_bmp(immagine_name,larghezza,altezza);
//inizio elaborazione immagine
long x,y,d,i;
double real_x,real_y, colore;
struct complesso z,c,temp;
for(x=0;x<larghezza;x++){
real_x = ((x1-x0)/larghezza)*x+x0;
for(y=0;y<altezza;y++){
real_y = ((y1-y0)/altezza)*y+y0;
//analisi di mendelbrot
c.r = julia.r;
c.i = julia.i;
z.r = real_x;
z.i = real_y;
d = 0;
for(i=0;i<deep;i++){
if((z.r*z.r)+(z.i*z.i) > 4) {
d = i;
break;
}
temp.r = z.r;
temp.i = z.i;
//if(temp.r<0)temp.r=temp.r*(-1);if(temp.i<0)temp.i=temp.i*(-1); //condizione burningship
z.r = (temp.r*temp.r) - (temp.i*temp.i) + c.r;
z.i = (temp.r*temp.i)*2 + c.i;
}
//calcolo del colore
colore = ((double)d/deep)*255;
colore = colore*30; //amplificazione
if(colore>255) colore=255;
//controllo punto dell'insieme di mendelbrot
if(d==0) {
matrice[x][y][0]=0;
matrice[x][y][1]=0;
matrice[x][y][2]=0;
}
else {
//scrittura del colore
matrice[x][y][0]=colore; //blu: colore
matrice[x][y][1]=255-matrice[x][y][0]; //verde: comportamento opposto al blu
matrice[x][y][2]=255; //rosso: costante
}
}
printf("%d%%\n",x*100/larghezza);
}
disegna_bmp(immagine_name,matrice,larghezza,altezza);
printf("fine!");
system(immagine_name);
//getchar();
return 0;
}
char *** crea_bmp(char *nome, long x, long y){
FILE *stream, *cavia;
stream = fopen(nome,"wb");
cavia = fopen("cavia","rb");
char *buffer;
buffer = (char *)malloc(54 * sizeof(char));
long b = fread( buffer, 1, 54, cavia);
buffer[18] = x%256;//asse x
buffer[19] = x/256;
buffer[22] = y%256;//asse y
buffer[23] = y/256;
fwrite(buffer, 1, b, stream);
fclose(stream);
char *** array;
int i, j, h;
array = (char***)malloc(x * sizeof(char *));
for(i=0; i<x; i++) {
array[i] = (char**)malloc(y * sizeof(char *));
for (j=0; j<y; j++)
array[i][j] = (char*)malloc(3 * sizeof(char *));
}
int i1,i2,i3;
for(i1=0; i1<x; i1++)
for (i2=0; i2<y; i2++)
for (i3=0; i3<3; i3++)
array[i1][i2][i3] = 0;
return array;
}
void disegna_bmp(char *nome, char *** array, long x, long y){
FILE *stream;
stream = fopen(nome,"ab");
int i1,i2;
for (i2=y-1; i2>=0; i2--)
for(i1=0; i1<x; i1++)
fwrite(array[i1][i2], 1, 3, stream);
fclose(stream);
}