From 0e367817210ac8bdb8c6c04dafc55082fe9c8f50 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Wed, 18 Sep 2013 13:52:11 +1000 Subject: [PATCH] Add manhattan closest calculation --- region.cpp | 15 +++++++++++++++ region.hpp | 5 +++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/region.cpp b/region.cpp index 1ce9f143..8a7bc6d3 100644 --- a/region.cpp +++ b/region.cpp @@ -102,6 +102,21 @@ region::centre (void) const { } +template +point<2> +region::closest (point<2> p) const { + return { + p.x < x ? x : + p.x > x + w ? x + w : + p.x, + + p.y < y ? y : + p.y < y + h ? y + h : + p.y + }; +} + + //----------------------------------------------------------------------------- template bool diff --git a/region.hpp b/region.hpp index 905969a5..f3c28283 100644 --- a/region.hpp +++ b/region.hpp @@ -50,8 +50,9 @@ namespace util { bool empty (void) const; - point2 base (void) const; - point2 centre (void) const; + point2 base (void) const; + point2 centre (void) const; + point2 closest (point2) const; bool includes (const point2&) const; // inclusive of borders bool contains (const point2&) const; // exclusive of borders