From 301c689285d49a969c335cd93132ee84ca064ddb Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Sun, 8 May 2016 20:39:59 +0200 Subject: Adapt example to generate a Voronoi diagram for n points --- CMakeLists.txt | 9 +++++++-- example.cc | 40 ++++++++++++++++++++++++++++++++++++++++ test.cc | 12 ------------ 3 files changed, 47 insertions(+), 14 deletions(-) create mode 100644 example.cc delete mode 100644 test.cc 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 + +#include +#include + +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::array 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 - -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); - } - ); -} -- cgit v1.2.3