Für Vorlesungen, bitte die Webseite verwenden. https://flavigny.de/lecture
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
1.9KB

  1. #include <iostream>
  2. #include <cmath>
  3. #include <string>
  4. // Datentyp Complex in der Datei canvas.hh
  5. #include "canvas.hh"
  6. // Summiert zwei komplexe Zahlen z und c und schreibt das Ergebnis in z
  7. void add_complex(Complex& z, Complex& c) {
  8. z.real += c.real;
  9. z.imag += c.imag;
  10. }
  11. // Multipliziert zwei komplexe Zahlen z und c und schreibt das Ergebnis in z
  12. void multiply_complex(Complex& z, Complex& c) {
  13. double re = z.real*c.real - z.imag*c.imag;
  14. double im = z.real*c.imag + z.imag*c.real;
  15. z.real = re;
  16. z.imag = im;
  17. }
  18. // Betrag einer komplexen Zahl
  19. double betrag(Complex z)
  20. {
  21. return std::sqrt(std::pow(z.real, 2) + std::pow(z.imag, 2));
  22. }
  23. // Generiert die Mandelbrot Menge und speichert sie in canvas mit threshold als Entfernung, ab dem
  24. // ein Punkt ,,entkommt'', mit maxIt als maximale Anzahl an Iterationen, die für
  25. // die Folge durchgeführt werden soll. Wird als PGM Bild unter filename gespeichert
  26. void mandelbrot(Canvas& canvas, double threshold, int maxIt, std::string filename) {
  27. for (int i = 0; i < canvas.vertPixels(); i++) {
  28. for (int j = 0; j < canvas.horPixels(); j++) {
  29. Complex c = canvas.coord(i, j);
  30. Complex z;
  31. z.real = 0;
  32. z.imag = 0;
  33. int neededIterations = -1;
  34. for (int k = 1; k <= maxIt; k++) {
  35. multiply_complex(z, z);
  36. add_complex(z, c);
  37. if (betrag(z) > threshold) {
  38. neededIterations = k;
  39. break;
  40. }
  41. }
  42. if (neededIterations == -1) {
  43. canvas.writePixel(i, j, 0);
  44. } else {
  45. canvas.writePixel(i, j, std::log(neededIterations)*100);
  46. }
  47. }
  48. }
  49. canvas.write(filename);
  50. }
  51. int main()
  52. {
  53. Canvas canvas = Canvas(-1, 0, 4, 3, 4000, 3000);
  54. mandelbrot(canvas, 1000, 10000, "mandelbrot.pgm");
  55. }