X-Git-Url: https://git.carlh.net/gitweb/?a=blobdiff_plain;f=src%2Flib%2Frect.h;h=d0daec43712ab4c5cd7a0bf88cf27c3c5fc69a74;hb=55861a0b4ad4270f80b8d1ae10230cf2ebb25735;hp=5758dd04e4496955af9b7cc994c492bf785c40b6;hpb=218ffe2c5f92ed19ec488cda5012b64fcabd5e3f;p=dcpomatic.git diff --git a/src/lib/rect.h b/src/lib/rect.h index 5758dd04e..d0daec437 100644 --- a/src/lib/rect.h +++ b/src/lib/rect.h @@ -1,19 +1,20 @@ /* Copyright (C) 2013 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 . */ @@ -21,6 +22,7 @@ #define DCPOMATIC_RECT_H #include "position.h" +#include #include /* Put this inside a namespace as Apple put a Rect in the global namespace */ @@ -67,16 +69,24 @@ 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); + /* 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); - return Rect ( + 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 boost::optional > (); + } + + return r; } void extend (Rect const & other)