aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdrian Kummerlaender2016-05-20 23:53:22 +0200
committerAdrian Kummerlaender2016-05-20 23:53:22 +0200
commitfefb5de652f3f07eda7bfb79ebb0134b64e5bd19 (patch)
treea5271847547d68b4265047d3ff9e9c0e367d6217
parentd87a9b0960ddd4397325fe7240b484ec9f79e7af (diff)
downloadvoronoi-fefb5de652f3f07eda7bfb79ebb0134b64e5bd19.tar
voronoi-fefb5de652f3f07eda7bfb79ebb0134b64e5bd19.tar.gz
voronoi-fefb5de652f3f07eda7bfb79ebb0134b64e5bd19.tar.bz2
voronoi-fefb5de652f3f07eda7bfb79ebb0134b64e5bd19.tar.lz
voronoi-fefb5de652f3f07eda7bfb79ebb0134b64e5bd19.tar.xz
voronoi-fefb5de652f3f07eda7bfb79ebb0134b64e5bd19.tar.zst
voronoi-fefb5de652f3f07eda7bfb79ebb0134b64e5bd19.zip
Implement unit circle example
`unit_circle.sh` generates a animated GIF of the circle states between p=1 and p=3.
-rw-r--r--CMakeLists.txt12
-rw-r--r--example.cc2
-rw-r--r--src/color.h2
-rw-r--r--unit_circle.cc39
-rw-r--r--unit_circle.sh17
5 files changed, 71 insertions, 1 deletions
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<std::uint8_t, std::uint8_t, std::uint8_t>;
+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 <imgen.h>
+
+#include <cmath>
+#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
+ )
+ ));
+}
+
+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 ..