aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--example.cc26
1 files changed, 18 insertions, 8 deletions
diff --git a/example.cc b/example.cc
index a62fc48..18d0e78 100644
--- a/example.cc
+++ b/example.cc
@@ -3,14 +3,20 @@
#include <cmath>
#include <algorithm>
-double euclidean_metric(int refX, int refY, int x, int y) {
- return std::sqrt(
- std::pow(refX - x, 2) + std::pow(refY - y, 2)
+template <int p>
+double minkowski_metric(int refX, int refY, int x, int y) {
+ return std::pow(
+ std::pow(std::abs(refX - x), p) + std::pow(std::abs(refY - y), p),
+ 1.0/p
);
}
+double euclidean_metric(int refX, int refY, int x, int y) {
+ return minkowski_metric<2>(refX, refY, x, y);
+}
+
int manhattan_metric(int refX, int refY, int x, int y) {
- return std::abs(refX - x) + std::abs(refY - y);
+ return minkowski_metric<1>(refX, refY, x, y);
}
int main(int, char*[]) {
@@ -18,7 +24,7 @@ int main(int, char*[]) {
std::array<refpos, 5> ref{
refpos(100, 50, imgen::color(255, 0, 0 )),
- refpos(500, 300, imgen::color(0, 255, 0 )),
+ refpos(490, 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))
@@ -33,12 +39,16 @@ int main(int, char*[]) {
ref.begin(),
ref.end(),
[x, y](const refpos& a, const refpos& b) -> bool {
- return manhattan_metric(std::get<0>(a), std::get<1>(a), x, y)
- < manhattan_metric(std::get<0>(b), std::get<1>(b), x, y);
+ return minkowski_metric<5>(std::get<0>(a), std::get<1>(a), x, y)
+ < minkowski_metric<5>(std::get<0>(b), std::get<1>(b), x, y);
}
);
- return std::get<2>(nearest);
+ if ( euclidean_metric(std::get<0>(nearest), std::get<1>(nearest), x, y) <= 5 ) {
+ return imgen::color(0, 0, 0);
+ } else {
+ return std::get<2>(nearest);
+ }
}
);
}