aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdrian Kummerlaender2016-05-22 16:47:38 +0200
committerAdrian Kummerlaender2016-05-22 16:47:38 +0200
commit9577bd535a18be9c3348f63ebaf434e1f14b8bcd (patch)
tree6033baebe85679d623eec5bcbb16f8f06cedc248
parente565d4e44c8013607a372c0f2a4af0eee8164843 (diff)
downloadvoronoi-9577bd535a18be9c3348f63ebaf434e1f14b8bcd.tar
voronoi-9577bd535a18be9c3348f63ebaf434e1f14b8bcd.tar.gz
voronoi-9577bd535a18be9c3348f63ebaf434e1f14b8bcd.tar.bz2
voronoi-9577bd535a18be9c3348f63ebaf434e1f14b8bcd.tar.lz
voronoi-9577bd535a18be9c3348f63ebaf434e1f14b8bcd.tar.xz
voronoi-9577bd535a18be9c3348f63ebaf434e1f14b8bcd.tar.zst
voronoi-9577bd535a18be9c3348f63ebaf434e1f14b8bcd.zip
Introduce `imgen::vector` tuple alias, generate metrics as double
-rw-r--r--src/imgen.h3
-rw-r--r--unit_circle.cc15
-rw-r--r--voronoi.cc43
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<int, int, color>;
+using vector = std::tuple<std::ptrdiff_t, std::ptrdiff_t>;
+using colored_vector = std::tuple<std::ptrdiff_t, std::ptrdiff_t, color>;
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 <vector>
#include <algorithm>
-int p_norm(double p, int x, int y) {
- return static_cast<int>(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 <vector>
#include <algorithm>
-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::ptrdiff_t>(
- 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<std::ptrdiff_t, 9> distances;
+ std::array<double, 9> 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);