1
1
#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
-
12
2
#include " lib.hpp"
13
3
14
4
#include < argparse/argparse.hpp>
@@ -19,10 +9,10 @@ void display_line()
19
9
#include < iostream>
20
10
#include < string>
21
11
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 ;
26
16
27
17
// https://en.wikipedia.org/wiki/Mandelbrot_set#Formal_definition
28
18
std::complex<double > step (std::complex<double > z_n, std::complex<double > constant)
@@ -37,14 +27,50 @@ int compute_iterations(
37
27
int iterations = 0 ;
38
28
std::complex<double > z_n = z_0;
39
29
40
- while (iterations < max_iters && std::norm (z_n) < divergence_norm ) {
30
+ while (iterations < max_iters && std::norm (z_n) < DIVERGENCE_NORM ) {
41
31
z_n = step (z_n, constant);
42
32
++iterations;
43
33
}
44
34
45
35
return iterations;
46
36
}
47
37
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
+
48
74
int main (int argc, char ** argv)
49
75
{
50
76
auto const lib = library{};
@@ -68,21 +94,10 @@ int main(int argc, char** argv)
68
94
return 1 ;
69
95
}
70
96
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" );
73
99
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 });
86
101
87
102
return 0 ;
88
103
}
0 commit comments