aboutsummaryrefslogtreecommitdiffstats
path: root/tools/chilbert_svg.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'tools/chilbert_svg.cpp')
-rw-r--r--tools/chilbert_svg.cpp52
1 files changed, 52 insertions, 0 deletions
diff --git a/tools/chilbert_svg.cpp b/tools/chilbert_svg.cpp
new file mode 100644
index 0000000..140b99a
--- /dev/null
+++ b/tools/chilbert_svg.cpp
@@ -0,0 +1,52 @@
+// Copyright 2018-2022 David Robillard <d@drobilla.net>
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#include "chilbert/chilbert.hpp"
+
+#include <array>
+#include <climits>
+#include <cmath>
+#include <cstdint>
+#include <cstdio>
+#include <cstdlib>
+
+int
+main(int argc, char** argv)
+{
+ if (argc != 2) {
+ fprintf(stderr, "Usage: %s NUM_POINTS\n", argv[0]);
+ return 1;
+ }
+
+ const unsigned long num_points = std::strtoul(argv[1], nullptr, 10);
+ if (num_points == 0 || num_points == ULONG_MAX) {
+ fprintf(stderr, "Usage: %s NUM_POINTS\n", argv[0]);
+ return 1;
+ }
+
+ const uint32_t w =
+ static_cast<uint32_t>(sqrt(1U << static_cast<uint32_t>(ceil(
+ log2(static_cast<double>(num_points)))))) -
+ 1U;
+
+ // Header
+ printf("<svg xmlns='http://www.w3.org/2000/svg'"
+ " version='1.2' baseProfile='tiny' viewBox='0 0 %u %u'>\n",
+ w,
+ w);
+ printf("<desc>Hilbert Curve</desc>\n");
+ printf("<polyline vector-effect='non-scaling-stroke' fill='none' "
+ "stroke='black' stroke-width='1' points='");
+
+ // One polyline through all vertices
+ for (uint64_t i = 0; i <= num_points; ++i) {
+ std::array<uint32_t, 2> point{};
+ chilbert::index_to_coords(point, 32, 2, i);
+ printf("%u,%u ", point[0], point[1]);
+ }
+
+ // Close off document
+ printf("' />\n</svg>\n");
+
+ return 0;
+}