From 9577bd535a18be9c3348f63ebaf434e1f14b8bcd Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Sun, 22 May 2016 16:47:38 +0200 Subject: Introduce `imgen::vector` tuple alias, generate metrics as double --- src/imgen.h | 3 ++- unit_circle.cc | 15 +++++++-------- voronoi.cc | 43 +++++++++++++++++++------------------------ 3 files changed, 28 insertions(+), 33 deletions(-) diff --git a/src/imgen.h b/src/imgen.h index 6954239..20705b4 100644 --- a/src/imgen.h +++ b/src/imgen.h @@ -7,7 +7,8 @@ namespace imgen { -using colored_vector = std::tuple; +using vector = std::tuple; +using colored_vector = std::tuple; void write_ppm( const std::string& path, diff --git a/unit_circle.cc b/unit_circle.cc index 283fb53..aa12bf7 100644 --- a/unit_circle.cc +++ b/unit_circle.cc @@ -4,13 +4,12 @@ #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 - ) - )); +double p_norm(double p, int x, int y) { + return std::pow( + std::pow(std::abs(x), p) + + std::pow(std::abs(y), p), + 1.0 / p + ); } void generate_p_unit_circle( @@ -24,7 +23,7 @@ void generate_p_unit_circle( 128, 128, [p](std::ptrdiff_t x, std::ptrdiff_t y) -> imgen::color { - if ( p_norm(p, x, y) <= 32 ) { + if ( p_norm(p, x, y) <= 32.0 ) { return imgen::black(); } else { return imgen::white(); diff --git a/voronoi.cc b/voronoi.cc index 4c864fd..00a49d6 100644 --- a/voronoi.cc +++ b/voronoi.cc @@ -5,33 +5,24 @@ #include #include -std::ptrdiff_t minkowski_metric( - const double p, - const std::ptrdiff_t refX, const std::ptrdiff_t refY, - const std::ptrdiff_t x, const std::ptrdiff_t y +double minkowski_metric( + const double p, + const imgen::vector& a, + const imgen::vector& b ) { - return static_cast( - std::nearbyint( - std::pow( - std::pow(std::abs(refX - x), p) + std::pow(std::abs(refY - y), p), - 1.0 / p - ) - ) + return std::pow( + std::pow(std::abs(std::get<0>(a) - std::get<0>(b)), p) + + std::pow(std::abs(std::get<1>(a) - std::get<1>(b)), p), + 1.0 / p ); } -std::ptrdiff_t manhattan_metric( - const std::ptrdiff_t refX, const std::ptrdiff_t refY, - const std::ptrdiff_t x, const std::ptrdiff_t y -) { - return minkowski_metric(1, refX, refY, x, y); +double manhattan_metric(const imgen::vector& a, const imgen::vector& b) { + return minkowski_metric(1, a, b); } -std::ptrdiff_t euclidean_metric( - const std::ptrdiff_t refX, const std::ptrdiff_t refY, - const std::ptrdiff_t x, const std::ptrdiff_t y -) { - return minkowski_metric(2, refX, refY, x, y); +double euclidean_metric(const imgen::vector& a, const imgen::vector& b) { + return minkowski_metric(2, a, b); } void generate_minkowski_voronoi(const double p) { @@ -52,14 +43,18 @@ void generate_minkowski_voronoi(const double p) { 512, 512, [&ref, p](std::ptrdiff_t x, std::ptrdiff_t y) -> imgen::color { - std::array distances; + std::array distances; std::transform( ref.begin(), ref.end(), distances.begin(), [x, y, p](const imgen::colored_vector& pos) { - return minkowski_metric(p, std::get<0>(pos), std::get<1>(pos), x, y); + return minkowski_metric( + p, + imgen::vector{std::get<0>(pos), std::get<1>(pos)}, + imgen::vector{x, y} + ); }); const auto& minimal_distance = std::min_element( @@ -70,7 +65,7 @@ void generate_minkowski_voronoi(const double p) { std::distance(distances.begin(), minimal_distance) ]; - if ( *minimal_distance <= 5 ) { + if ( *minimal_distance <= 5.0 ) { return imgen::black(); } else { return std::get<2>(nearest); -- cgit v1.2.3