From fefb5de652f3f07eda7bfb79ebb0134b64e5bd19 Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Fri, 20 May 2016 23:53:22 +0200 Subject: Implement unit circle example `unit_circle.sh` generates a animated GIF of the circle states between p=1 and p=3. --- CMakeLists.txt | 12 ++++++++++++ example.cc | 2 +- src/color.h | 2 ++ unit_circle.cc | 39 +++++++++++++++++++++++++++++++++++++++ unit_circle.sh | 17 +++++++++++++++++ 5 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 unit_circle.cc create mode 100644 unit_circle.sh diff --git a/CMakeLists.txt b/CMakeLists.txt index ccbfde7..6aeef3a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,8 +17,20 @@ add_executable( src/ppm_pixel_stream.cc ) +add_executable( + unit_circle + unit_circle.cc + src/imgen.cc + src/ppm_pixel_stream.cc +) + target_link_libraries( example m pthread ) + +target_link_libraries( + unit_circle + m +) diff --git a/example.cc b/example.cc index 294c86a..8e4eee8 100644 --- a/example.cc +++ b/example.cc @@ -64,7 +64,7 @@ void generate_minkowski_voronoi( ]; if ( *minimal_distance <= 5 ) { - return imgen::color(0, 0, 0); + return imgen::black(); } else { return std::get<2>(nearest); } diff --git a/src/color.h b/src/color.h index 514c739..dcad5cf 100644 --- a/src/color.h +++ b/src/color.h @@ -7,6 +7,8 @@ namespace imgen { using color = std::tuple; +constexpr color black() { return color{ 0, 0, 0}; } +constexpr color white() { return color{255, 255, 255}; } constexpr color red() { return color{255, 0, 0}; } constexpr color lime() { return color{ 0, 255, 0}; } constexpr color blue() { return color{ 0, 0, 255}; } diff --git a/unit_circle.cc b/unit_circle.cc new file mode 100644 index 0000000..283fb53 --- /dev/null +++ b/unit_circle.cc @@ -0,0 +1,39 @@ +#include + +#include +#include +#include + +int p_norm(double p, int x, int y) { + return static_cast(std::nearbyint( + std::pow( + std::pow(std::abs(x), p) + std::pow(std::abs(y), p), + 1.0 / p + ) + )); +} + +void generate_p_unit_circle( + const double lower, + const double upper, + const double epsilon +) { + for ( double p = lower; p < upper + epsilon; p = p + epsilon ) { + imgen::write_ppm( + "unit_circle_" + std::to_string(p) + ".ppm", + 128, + 128, + [p](std::ptrdiff_t x, std::ptrdiff_t y) -> imgen::color { + if ( p_norm(p, x, y) <= 32 ) { + return imgen::black(); + } else { + return imgen::white(); + } + } + ); + } +} + +int main(int, char*[]) { + generate_p_unit_circle(1.0, 3.0, 0.05); +} diff --git a/unit_circle.sh b/unit_circle.sh new file mode 100644 index 0000000..40c834f --- /dev/null +++ b/unit_circle.sh @@ -0,0 +1,17 @@ +#! /usr/bin/fish + +mkdir unit ^/dev/null +cd unit +rm *.ppm *.gif ^/dev/null + +../unit_circle + +for f in unit_circle_* + convert $f -annotate +75+120 (echo "p =" (echo $f | grep -o "[0-9].[0-9]\{2\}" | head -n 1)) $f +end + +convert -delay 8 -loop 0 *.ppm circle.gif +convert -reverse circle.gif circle_reversed.gif +convert circle.gif circle_reversed.gif circle_cycle.gif + +cd .. -- cgit v1.2.3