X-Git-Url: https://main.carlh.net/gitweb/?p=dcpomatic.git;a=blobdiff_plain;f=src%2Flib%2Frect.h;h=a01e0f8858ffba6d31eb6b9675d0cff571b4571d;hp=963119b507b793998d370ffdf1acd786af1dbe9b;hb=HEAD;hpb=341f0a81591ab83b96069d6e6bb70cbf8e6cf878 diff --git a/src/lib/rect.h b/src/lib/rect.h index 963119b50..a01e0f885 100644 --- a/src/lib/rect.h +++ b/src/lib/rect.h @@ -1,41 +1,47 @@ /* - Copyright (C) 2013 Carl Hetherington + Copyright (C) 2013-2021 Carl Hetherington - This program is free software; you can redistribute it and/or modify + This file is part of DCP-o-matic. + + DCP-o-matic is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. - This program is distributed in the hope that it will be useful, + DCP-o-matic is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + along with DCP-o-matic. If not, see . */ + #ifndef DCPOMATIC_RECT_H #define DCPOMATIC_RECT_H + #include "position.h" +#include #include + /* Put this inside a namespace as Apple put a Rect in the global namespace */ namespace dcpomatic { - + + /** @struct Rect * @brief A rectangle. */ -template +template class Rect { public: - + Rect () : x (0) , y (0) @@ -67,24 +73,43 @@ public: return Position (x, y); } - Rect intersection (Rect const & other) const + boost::optional> intersection (Rect const & other) const { - T const tx = max (x, other.x); - T const ty = max (y, other.y); - - return Rect ( + /* This isn't exactly the paragon of mathematical precision */ + + T const tx = std::max (x, other.x); + T const ty = std::max (y, other.y); + + Rect r ( tx, ty, - min (x + width, other.x + other.width) - tx, - min (y + height, other.y + other.height) - ty + std::min (x + width, other.x + other.width) - tx, + std::min (y + height, other.y + other.height) - ty ); + + if (r.width < 0 || r.height < 0) { + return {}; + } + + return r; } void extend (Rect const & other) { + T old_x = x; + T old_y = y; x = std::min (x, other.x); y = std::min (y, other.y); - width = std::max (x + width, other.x + other.width) - x; - height = std::max (y + height, other.y + other.height) - y; + width = std::max (old_x + width, other.x + other.width) - x; + height = std::max (old_y + height, other.y + other.height) - y; + } + + Rect extended (T amount) const { + Rect c = *this; + c.x -= amount; + c.y -= amount; + c.width += amount * 2; + c.height += amount * 2; + return c; } bool contains (Position p) const @@ -93,6 +118,15 @@ public: } }; + +template +bool operator== (Rect const& a, Rect const& b) +{ + return a.x == b.x && a.y == b.y && a.width == b.width && a.height == b.height; } + +} + + #endif