|
1 | 1 | #include "graphics/basic_display.hpp" |
2 | 2 |
|
3 | | -int main() |
| 3 | +void display_line() |
4 | 4 | { |
5 | 5 | fractal::BasicDisplay display; |
6 | 6 | for (std::size_t i = 0; i < 100; i++) { |
7 | 7 | display.set_pixel(100, 100 + i, 255); |
8 | 8 | } |
9 | 9 | display.display_window(); |
| 10 | +} |
| 11 | + |
| 12 | +#include "lib.hpp" |
| 13 | + |
| 14 | +#include <argparse/argparse.hpp> |
| 15 | + |
| 16 | +#include <complex> |
| 17 | + |
| 18 | +#include <exception> |
| 19 | +#include <iostream> |
| 20 | +#include <string> |
| 21 | + |
| 22 | +constexpr double divergence_norm = 4; |
| 23 | +constexpr double x_dim = 2; |
| 24 | +constexpr double y_dim = 2; |
| 25 | +constexpr int max_iterations = 50; |
| 26 | + |
| 27 | +// https://en.wikipedia.org/wiki/Mandelbrot_set#Formal_definition |
| 28 | +std::complex<double> step(std::complex<double> z_n, std::complex<double> constant) |
| 29 | +{ |
| 30 | + return z_n * z_n + constant; |
| 31 | +} |
| 32 | + |
| 33 | +int compute_iterations( |
| 34 | + std::complex<double> z_0, std::complex<double> constant, int max_iters |
| 35 | +) |
| 36 | +{ |
| 37 | + int iterations = 0; |
| 38 | + std::complex<double> z_n = z_0; |
| 39 | + |
| 40 | + while (iterations < max_iters && std::norm(z_n) < divergence_norm) { |
| 41 | + z_n = step(z_n, constant); |
| 42 | + ++iterations; |
| 43 | + } |
| 44 | + |
| 45 | + return iterations; |
| 46 | +} |
| 47 | + |
| 48 | +int main(int argc, char** argv) |
| 49 | +{ |
| 50 | + auto const lib = library{}; |
| 51 | + argparse::ArgumentParser program(lib.name); |
| 52 | + |
| 53 | + program.add_argument("width") |
| 54 | + .help("Horizontal resolution") |
| 55 | + .default_value(800) |
| 56 | + .scan<'i', int>(); |
| 57 | + |
| 58 | + program.add_argument("height") |
| 59 | + .help("Vertical resolution") |
| 60 | + .default_value(600) |
| 61 | + .scan<'i', int>(); |
| 62 | + |
| 63 | + try { |
| 64 | + program.parse_args(argc, argv); |
| 65 | + } catch (const std::exception& err) { |
| 66 | + std::cerr << err.what() << std::endl; |
| 67 | + std::cerr << program; |
| 68 | + return 1; |
| 69 | + } |
| 70 | + |
| 71 | + auto width = static_cast<double>(program.get<int>("width")); |
| 72 | + auto height = static_cast<double>(program.get<int>("height")); |
| 73 | + |
| 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 | + } |
| 86 | + |
10 | 87 | return 0; |
11 | 88 | } |
0 commit comments