11#include " graphics/basic_display.hpp"
2-
3- void display_line ()
4- {
5- fractal::BasicDisplay display;
6- for (std::size_t i = 0 ; i < 100 ; i++) {
7- display.set_pixel (100 , 100 + i, 255 );
8- }
9- display.display_window ();
10- }
11-
122#include " lib.hpp"
133
144#include < argparse/argparse.hpp>
@@ -19,10 +9,10 @@ void display_line()
199#include < iostream>
2010#include < string>
2111
22- constexpr double divergence_norm = 4 ;
23- constexpr double x_dim = 2 ;
24- constexpr double y_dim = 2 ;
25- constexpr int max_iterations = 50 ;
12+ constexpr double DIVERGENCE_NORM = 4 ;
13+ constexpr double X_DIM = 2 ;
14+ constexpr double Y_DIM = 2 ;
15+ constexpr int MAX_ITERATIONS = 50 ;
2616
2717// https://en.wikipedia.org/wiki/Mandelbrot_set#Formal_definition
2818std::complex <double > step (std::complex <double > z_n, std::complex <double > constant)
@@ -37,14 +27,50 @@ int compute_iterations(
3727 int iterations = 0 ;
3828 std::complex <double > z_n = z_0;
3929
40- while (iterations < max_iters && std::norm (z_n) < divergence_norm ) {
30+ while (iterations < max_iters && std::norm (z_n) < DIVERGENCE_NORM ) {
4131 z_n = step (z_n, constant);
4232 ++iterations;
4333 }
4434
4535 return iterations;
4636}
4737
38+ void display_line ()
39+ {
40+ fractal::BasicDisplay display;
41+ for (std::size_t i = 0 ; i < 100 ; i++) {
42+ display.set_pixel (100 , 100 + i, 255 );
43+ }
44+ display.display_window ();
45+ }
46+
47+ void display_julia (std::size_t width, std::size_t height, std::complex <double > constant)
48+ {
49+ fractal::BasicDisplay display;
50+
51+ auto x_step = X_DIM * 2 / static_cast <double >(width);
52+ auto y_step = Y_DIM * 2 / static_cast <double >(height);
53+
54+ for (std::size_t j = 0 ; j < height; ++j) {
55+ for (std::size_t i = 0 ; i < width; ++i) {
56+ // Compute complex coordinates from pixel index
57+ double x = -X_DIM + i * x_step;
58+ double y = -Y_DIM + j * y_step;
59+
60+ // Compute the number of iterations
61+ auto iterations = compute_iterations ({x, y}, constant, MAX_ITERATIONS);
62+
63+ // Draw the pixel
64+ display.set_pixel (
65+ i, j,
66+ static_cast <int >(iterations / static_cast <double >(MAX_ITERATIONS) * 255 )
67+ );
68+ }
69+ }
70+
71+ display.display_window ();
72+ }
73+
4874int main (int argc, char ** argv)
4975{
5076 auto const lib = library{};
@@ -68,21 +94,10 @@ int main(int argc, char** argv)
6894 return 1 ;
6995 }
7096
71- auto width = static_cast < double >( program.get <int >(" width" ) );
72- auto height = static_cast < double >( program.get <int >(" height" ) );
97+ auto width = program.get <int >(" width" );
98+ auto height = program.get <int >(" height" );
7399
74- auto x_step = x_dim * 2 / width;
75- auto y_step = y_dim * 2 / height;
76-
77- std::complex <double > constant = {1.0 / 4 , 0 };
78-
79- for (auto y = -y_dim; y < y_dim; y += y_step) {
80- for (auto x = -x_dim; x < x_dim; x += x_step) {
81- auto iterations = compute_iterations ({x, y}, constant, max_iterations);
82- std::cout << iterations << ' ' ;
83- }
84- std::cout << ' \n ' ;
85- }
100+ display_julia (width, height, {1.0 / 4 , 0 });
86101
87102 return 0 ;
88103}
0 commit comments