Ho appena scritto questo programma in C++ per creare l'insieme di Mandelbrot in bianco e nero ma non funzione. O meglio: fa schizzare l'attività di processore e ventole alle stelle senza però produrre un bel cavolo di niente in tempi apprezzabili.
La prima esecuzione ho sbagliato io. Ho passato al programma i seguenti argomenti, palesemente troppo elevati: 1024 786 100.
Ho poi rieseguito il programma con i dati seguenti, ma con lo stesso risultato: 20 12 25.
Il programma è giusto ed è un problema di efficienza, vi sono altri problemi oppure mi tocca proprio aspettare un sacco?
codice:
#include <iostream>
using std::cout;
using std::endl;
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;
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, int const argv[])
{
ofstream oFile("mandelbrot.txt", ios::out);
if (argc =! 4)
{
cout << "Invalid arguments." << endl;
exit(1);
}
bool** image = createPBM(argv[1], argv[2]);
mandelbrot(argv[1], argv[2], image, argv[3]);
printPBM(argv[1], argv[2], image, oFile);
system("convert mandelbrot.txt mandelbrot.png");
}