aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdrian Kummerlaender2016-05-08 20:39:59 +0200
committerAdrian Kummerlaender2016-05-08 20:39:59 +0200
commit301c689285d49a969c335cd93132ee84ca064ddb (patch)
tree7a7106139cda86362e874e468dde9d36bda64afe
parent21423a44d6e3091addc00c1b0769336e1f154d4e (diff)
downloadvoronoi-301c689285d49a969c335cd93132ee84ca064ddb.tar
voronoi-301c689285d49a969c335cd93132ee84ca064ddb.tar.gz
voronoi-301c689285d49a969c335cd93132ee84ca064ddb.tar.bz2
voronoi-301c689285d49a969c335cd93132ee84ca064ddb.tar.lz
voronoi-301c689285d49a969c335cd93132ee84ca064ddb.tar.xz
voronoi-301c689285d49a969c335cd93132ee84ca064ddb.tar.zst
voronoi-301c689285d49a969c335cd93132ee84ca064ddb.zip
Adapt example to generate a Voronoi diagram for n points
-rw-r--r--CMakeLists.txt9
-rw-r--r--example.cc40
-rw-r--r--test.cc12
3 files changed, 47 insertions, 14 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 88200b9..1d543a9 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -11,7 +11,12 @@ include_directories(
)
add_executable(
- test
- test.cc
+ example
+ example.cc
src/imgen.cc
)
+
+target_link_libraries(
+ example
+ m
+)
diff --git a/example.cc b/example.cc
new file mode 100644
index 0000000..e4d7420
--- /dev/null
+++ b/example.cc
@@ -0,0 +1,40 @@
+#include <imgen.h>
+
+#include <cmath>
+#include <algorithm>
+
+double metric(std::size_t refX, std::size_t refY, std::size_t x, std::size_t y) {
+ return std::sqrt(
+ (refX - x)*(refX - x) + (refY - y)*(refY - y)
+ );
+}
+
+int main(int, char*[]) {
+ using refpos = std::tuple<std::size_t, std::size_t, imgen::color>;
+
+ std::array<refpos, 5> ref{
+ refpos(100, 50, imgen::color(255, 0, 0 )),
+ refpos(500, 300, imgen::color(0, 255, 0 )),
+ refpos(250, 250, imgen::color(0, 0, 255)),
+ refpos(400, 20, imgen::color(100, 10, 100)),
+ refpos(60, 400, imgen::color(20, 60, 300))
+ };
+
+ imgen::write_ppm(
+ "test.ppm",
+ 500,
+ 500,
+ [&ref](std::size_t x, std::size_t y) -> imgen::color {
+ const refpos& nearest = *std::min_element(
+ ref.begin(),
+ ref.end(),
+ [x, y](const refpos& a, const refpos& b) -> bool {
+ return metric(std::get<0>(a), std::get<1>(a), x, y)
+ < metric(std::get<0>(b), std::get<1>(b), x, y);
+ }
+ );
+
+ return std::get<2>(nearest);
+ }
+ );
+}
diff --git a/test.cc b/test.cc
deleted file mode 100644
index 36f637d..0000000
--- a/test.cc
+++ /dev/null
@@ -1,12 +0,0 @@
-#include <imgen.h>
-
-int main(int, char*[]) {
- imgen::write_ppm(
- "test.ppm",
- 500,
- 500,
- [](std::size_t x, std::size_t y) -> imgen::color {
- return imgen::color(255, x % 255, y % 255);
- }
- );
-}