1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
|
#include <iostream>
using std::cout;
using std::endl;
using std::cerr;
using std::ios;
#include<cmath>
using std::pow;
#include<vector>
using std::vector;
#include <fstream>
using std::ofstream;
#include <complex>
using std::complex;
#include<cstdlib>
using std::exit;
using std::atoi;
bool** createPBM (int imageW, int imageH)
{
bool** image = new bool*[imageH];
for (int i = 0; i < imageH; i++)
{
image[i] = new bool[imageW];
}
return image;
}
void mandelbrot (int imageW, int imageH, bool** image, int iterations)
{
double const Rmin = 2, Rmax = 1;
double const Imin = -1.2, Imax = 1.2;
double const scaleR = (Rmax - Rmin) / (imageW - 1), scaleI = (Imax - Imin) / (imageH - 1);
complex<double> Z (0, 0);
complex<double> Zn (0, 0);
complex<double> C (0, 0);
for (int y = 0; y < imageH; y++)
{
complex<double> C (C.real(), Imax - y * scaleI);
for (int x = 0; x < imageW; x++)
{
complex<double> C (Rmin + x * scaleR, C.imag());
complex<double> Z (C.real(), C.imag());
for (int n = 0; n < iterations; n++)
{
complex<double> Zn (pow(Z.real(), 2), pow(Z.imag(), 2));
if (Zn.real() + Zn.imag() > 4)
{
image[x][y] = false;
break;
}
complex<double> Z (Zn.real() - Zn.imag() + C.real(), 2 * Z.real() * Z.imag() + C.imag());
}
}
}
}
void printPBM (int imageW, int imageH,bool** image, ofstream& oFile)
{
for (int i = 0; i < imageW; i++)
{
for (int j = 0; j < imageH; j++)
{
if (image[i][j])
{
oFile << 1 << ' ';
}
else
{
oFile << 0 << ' ';
}
}
}
}
int main (/*int argc, const char* argv[]*/)
{
ofstream oFile("mandelbrot.txt", ios::out);
if (! oFile.is_open())
{
cerr << "File could not be opened." << endl;
exit(1);
}
/*
if (argc != 4)
{
cout << "Invalid arguments." << endl;
exit(1);
}
int arg[4] = {argc, atoi(argv[1]), atoi(argv[2]), atoi(argv[3])};
bool** image = createPBM(arg[1], arg[2]);
mandelbrot(arg[1], arg[2], image, arg[3]);
printPBM(arg[1], arg[2], image, oFile);
*/
bool** image = createPBM(200, 120);
mandelbrot(200, 120, image, 10);
printPBM(200, 120, image, oFile);
system("convert mandelbrot.txt mandelbrot.png");
}
|