From 870550d975d2036fc08659acbe1515fb7c896ba4 Mon Sep 17 00:00:00 2001 From: Martin Read Date: Tue, 18 Feb 2014 22:41:21 +0000 Subject: [PATCH] Add clamp() member function to constrain Offsets and Coords to a bounding box * coord.hh: Added Offset::clamp() and Coord::clamp() member functions which constrain the x and y values to fit inside a specified bounding box. --- coord.hh | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/coord.hh b/coord.hh index 7f06491..e8c07a6 100644 --- a/coord.hh +++ b/coord.hh @@ -63,6 +63,11 @@ public: bool operator <=(Offset const& right) const { return (y < right.y) || ((y == right.y) && (x <= right.x)); } bool operator >(Offset const& right) const { return (y > right.y) || ((y == right.y) && (x > right.x)); } bool operator >=(Offset const& right) const { return (y > right.y) || ((y == right.y) && (x >= right.x)); } + void clamp(int ymin, int xmin, int ymax, int xmax) + { + y = std::min(ymax, std::max(ymin, y)); + x = std::min(xmax, std::max(xmin, x)); + } }; /*! \brief A two-dimensional point @@ -93,6 +98,11 @@ public: bool operator <=(Coord const& right) const { return (y < right.y) || ((y == right.y) && (x <= right.x)); } bool operator >(Coord const& right) const { return (y > right.y) || ((y == right.y) && (x > right.x)); } bool operator >=(Coord const& right) const { return (y > right.y) || ((y == right.y) && (x >= right.x)); } + void clamp(int ymin, int xmin, int ymax, int xmax) + { + y = std::min(ymax, std::max(ymin, y)); + x = std::min(xmax, std::max(xmin, x)); + } }; extern Coord const Nowhere; -- 2.11.0