add ipi9
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
#include <iostream>
|
||||
#include <cmath>
|
||||
#include <string>
|
||||
|
||||
// Datentyp Complex in der Datei canvas.hh
|
||||
#include "canvas.hh"
|
||||
|
||||
// Summiert zwei komplexe Zahlen z und c und schreibt das Ergebnis in z
|
||||
void add_complex(Complex& z, Complex& c) {
|
||||
z.real += c.real;
|
||||
z.imag += c.imag;
|
||||
}
|
||||
|
||||
// Multipliziert zwei komplexe Zahlen z und c und schreibt das Ergebnis in z
|
||||
void multiply_complex(Complex& z, Complex& c) {
|
||||
double re = z.real*c.real - z.imag*c.imag;
|
||||
double im = z.real*c.imag + z.imag*c.real;
|
||||
z.real = re;
|
||||
z.imag = im;
|
||||
}
|
||||
|
||||
// Betrag einer komplexen Zahl
|
||||
double betrag(Complex z)
|
||||
{
|
||||
return std::sqrt(std::pow(z.real, 2) + std::pow(z.imag, 2));
|
||||
}
|
||||
|
||||
// Generiert die Mandelbrot Menge und speichert sie in canvas mit threshold als Entfernung, ab dem
|
||||
// ein Punkt ,,entkommt'', mit maxIt als maximale Anzahl an Iterationen, die für
|
||||
// die Folge durchgeführt werden soll. Wird als PGM Bild unter filename gespeichert
|
||||
void mandelbrot(Canvas& canvas, double threshold, int maxIt, std::string filename) {
|
||||
for (int i = 0; i < canvas.vertPixels(); i++) {
|
||||
for (int j = 0; j < canvas.horPixels(); j++) {
|
||||
Complex c = canvas.coord(i, j);
|
||||
Complex z;
|
||||
z.real = 0;
|
||||
z.imag = 0;
|
||||
int neededIterations = -1;
|
||||
for (int k = 1; k <= maxIt; k++) {
|
||||
multiply_complex(z, z);
|
||||
add_complex(z, c);
|
||||
if (betrag(z) > threshold) {
|
||||
neededIterations = k;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (neededIterations == -1) {
|
||||
canvas.writePixel(i, j, 0);
|
||||
} else {
|
||||
canvas.writePixel(i, j, std::log(neededIterations)*100);
|
||||
}
|
||||
}
|
||||
}
|
||||
canvas.write(filename);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
Canvas canvas = Canvas(-1, 0, 4, 3, 4000, 3000);
|
||||
mandelbrot(canvas, 1000, 10000, "mandelbrot.pgm");
|
||||
}
|
||||
Reference in New Issue
Block a user