Skip to content

Render #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ prefix/
CMakeLists.txt.user
compile_commands.json
conan_cache_*.tgz
CMakeUserPresets.json
.cache
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ target_include_directories(
target_compile_features(fractal-generator_lib PUBLIC cxx_std_20)

find_package(fmt REQUIRED)
target_link_libraries(fractal-generator_lib PRIVATE fmt::fmt)
find_package(argparse REQUIRED)
target_link_libraries(fractal-generator_lib PUBLIC fmt::fmt)
target_link_libraries(fractal-generator_lib PUBLIC argparse::argparse)

find_package(SFML REQUIRED graphics CONFIG)
target_link_libraries(fractal-generator_lib PUBLIC sfml-graphics)
Expand Down
2 changes: 1 addition & 1 deletion CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
{
"name": "ci-darwin",
"inherits": ["flags-appleclang", "ci-std"],
"generator": "Xcode",
"generator": "Unix Makefiles",
"hidden": true,
"cacheVariables": {
"CMAKE_CATCH_DISCOVER_TESTS_DISCOVERY_MODE": "PRE_TEST"
Expand Down
36 changes: 0 additions & 36 deletions CMakeUserPresets.json

This file was deleted.

1 change: 1 addition & 0 deletions conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def layout(self):
def requirements(self):
self.requires("fmt/11.0.2")
self.requires("sfml/2.6.1")
self.requires("argparse/3.1")

def build_requirements(self):
self.test_requires("catch2/3.7.0")
119 changes: 118 additions & 1 deletion source/main.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,128 @@
#include "graphics/basic_display.hpp"
#include "lib.hpp"

int main()
#include <argparse/argparse.hpp>

#include <complex>

#include <exception>
#include <iostream>
#include <string>

constexpr double DIVERGENCE_NORM = 4;
constexpr double X_DIM = 2;
constexpr double Y_DIM = 2;
constexpr int MAX_ITERATIONS = 50;

// https://en.wikipedia.org/wiki/Mandelbrot_set#Formal_definition
std::complex<double> step(std::complex<double> z_n, std::complex<double> constant)
{
return z_n * z_n + constant;
}

int compute_iterations(
std::complex<double> z_0, std::complex<double> constant, int max_iters
)
{
int iterations = 0;
std::complex<double> z_n = z_0;

while (iterations < max_iters && std::norm(z_n) < DIVERGENCE_NORM) {
z_n = step(z_n, constant);
++iterations;
}

return iterations;
}

void display_line()
{
fractal::BasicDisplay display;
for (std::size_t i = 0; i < 100; i++) {
display.set_pixel(100, 100 + i, 255);
}
display.display_window();
}

void display_julia(std::size_t width, std::size_t height, std::complex<double> constant)
{
fractal::BasicDisplay display;

auto x_step = X_DIM * 2 / static_cast<double>(width);
auto y_step = Y_DIM * 2 / static_cast<double>(height);

for (std::size_t j = 0; j < height; ++j) {
for (std::size_t i = 0; i < width; ++i) {
double x = -X_DIM + i * x_step;
double y = -Y_DIM + j * y_step;

auto iterations = compute_iterations({x, y}, constant, MAX_ITERATIONS);

display.set_pixel(
i, j,
static_cast<int>(iterations / static_cast<double>(MAX_ITERATIONS) * 255)
);
}
}

display.display_window();
}

void display_mandelbrot(
std::size_t width, std::size_t height, std::complex<double> constant
)
{
fractal::BasicDisplay display;

auto x_step = X_DIM * 2 / static_cast<double>(width);
auto y_step = Y_DIM * 2 / static_cast<double>(height);

for (std::size_t j = 0; j < height; ++j) {
for (std::size_t i = 0; i < width; ++i) {
// Compute complex coordinates from pixel index
double x = -X_DIM + i * x_step;
double y = -Y_DIM + j * y_step;

// Compute the number of iterations
auto iterations = compute_iterations(constant, {x, y}, MAX_ITERATIONS);

display.set_pixel(
i, j,
static_cast<int>(iterations / static_cast<double>(MAX_ITERATIONS) * 255)
);
}
}

display.display_window();
}

int main(int argc, char** argv)
{
auto const lib = library{};
argparse::ArgumentParser program(lib.name);

program.add_argument("width")
.help("Horizontal resolution")
.default_value(800)
.scan<'i', int>();

program.add_argument("height")
.help("Vertical resolution")
.default_value(600)
.scan<'i', int>();

try {
program.parse_args(argc, argv);
} catch (const std::exception& err) {
std::cerr << err.what() << std::endl;
std::cerr << program;
return 1;
}

auto width = program.get<int>("width");
auto height = program.get<int>("height");

display_mandelbrot(width, height, {0, 0});

return 0;
}
Loading