|
// mandelbrot.cpp
#include <iostream>
#include <fstream>
using namespace std;
fstream picture;
long steps(long maxi, double c1, double c2) {
double z1=0;
double z2=0;
double a;
double b;
int stop=0;
long i=0;
do {
i+=1;
a=z1*z1;
b=z2*z2;
z2=c2+2*z1*z2;
z1=c1+a-b;
if ((a+b)>9) stop=1;
}
while ((!stop) && (i<maxi));
return i;
}
int bmpx(int i) {
int ret=i;
while (ret%4) ret++;
return ret;
}
void pb(int b) {
char cb=(char) b;
picture.write((char*) &cb,sizeof(char));
}
void hilo(int m, int b[4]) {
int l=m;
int i;
for (i=0; i<4; i++) {
b[i]=l;
l=(l>>8);
}
b[2]-=(b[3]<<8);
b[1]-=(b[2]<<8)+(b[3]<<16);
b[0]-=(b[1]<<8)+(b[2]<<16)+(b[3]<<24);
}
void header(int x, int y) {
int i;
int j;
int res;
int size;
int b[4];
res=bmpx(x)*y;
size=1078+res;
pb(66);
pb(77);
hilo(size,b);
for (i=0; i<4; i++) pb(b[i]);
for (i=0; i<4; i++) pb(0);
pb(54);
pb(4);
for (i=0; i<2; i++) pb(0);
pb(40);
for (i=0; i<3; i++) pb(0);
hilo(x,b);
for (i=0; i<4; i++) pb(b[i]);
hilo(y,b);
for (i=0; i<4; i++) pb(b[i]);
pb(1);
pb(0);
pb(8);
for (i=0; i<5; i++) pb(0);
hilo(res,b);
for (i=0; i<4; i++) pb(b[i]);
hilo(3780,b);
for (j=0; j<2; j++)
for (i=0; i<4; i++)
pb(b[i]);
pb(0);
pb(1);
for (i=0; i<6; i++) pb(0);
}
void palette() {
int i;
double R;
double G;
double B;
for (i=0; i<256; i++) {
R=0;
G=0;
B=0;
if ((i>90) && (i<130)) R=(i-90)/40.0;
if (i>=130) R=1.0;
if ((i>43) && (i<85)) G=(i-43)/47.786;
if ((i>=85) && (i<130)) G=0.879+(i-85)/371.9;
if ((i>=130) && (i<175)) G=(175-i)/45.0;
if (i>215) G=(i-215)/40.0;
if (i<45) B=i/46.0;
if ((i>=45) && (i<85)) B=(85-i)/40.9;
if ((i>=175) && (i<220)) B=(i-175)/45.0;
if (i>=220) B=1.0;
pb((int) (0.1+255.8*B));
pb((int) (0.1+255.8*G));
pb((int) (0.1+255.8*R));
pb(0);
}
}
int main() {
int i;
int j;
int x;
int y;
int ki;
int kj;
int x_bmpx;
long f;
long maxi;
double c1;
double c2;
double prz=0;
double m=0.000003657;
double re=-0.743650449;
double im=0.1318204775;
cout << "\nresolution x (e.g., 1200) : ";
cin >> x;
cout << "maximum number of steps (e.g., 20000) : ";
cin >> maxi;
y=(3*x)/4;
cout << "creating " << x << "x" << y << " mandelbrot.bmp\n";
picture.open("mandelbrot.bmp", ios::out|ios::binary);
header(x,y);
cout << "writing palette\n";
palette();
cout << "calculating picture\n";
cout.precision(1);
cout.setf(ios::fixed);
x_bmpx=bmpx(x);
for (j=y-1; j>=0; j--) {
for (i=0; i<x; i++) {
f=0;
for (kj=0; kj<4; kj++) {
for (ki=0; ki<4; ki++) {
c1=re+(m*((i<<2)+ki))/x;
c2=im+(m*((j<<2)+kj))/x;
f+=steps(maxi,c1,c2);
}
}
if (f==(maxi<<4)) f=0;
f=f>>6;
if (f>255) f=255;
pb((int) f);
}
for (i=x; i<x_bmpx; i++) pb(0);
prz+=(100.0/y);
cout << "\r[" << prz << "%]";
}
picture.close();
cout << "\nready. (mhebel)\n";
return 0;
}
|
|