projects
/
dcpomatic.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Make PlayerText comparable.
[dcpomatic.git]
/
src
/
lib
/
rect.h
diff --git
a/src/lib/rect.h
b/src/lib/rect.h
index 1feb8ad4fed460d19a9f83439e21bdd0abcc1ea0..4e4020073057980fd94b49821221b58f6037d867 100644
(file)
--- a/
src/lib/rect.h
+++ b/
src/lib/rect.h
@@
-1,19
+1,20
@@
/*
Copyright (C) 2013 Carl Hetherington <cth@carlh.net>
/*
Copyright (C) 2013 Carl Hetherington <cth@carlh.net>
- 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.
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
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 <http://www.gnu.org/licenses/>.
*/
*/
@@
-21,20
+22,22
@@
#define DCPOMATIC_RECT_H
#include "position.h"
#define DCPOMATIC_RECT_H
#include "position.h"
+#include <boost/optional.hpp>
+#include <algorithm>
/* Put this inside a namespace as Apple put a Rect in the global namespace */
namespace dcpomatic
{
/* Put this inside a namespace as Apple put a Rect in the global namespace */
namespace dcpomatic
{
-
+
/** @struct Rect
* @brief A rectangle.
*/
/** @struct Rect
* @brief A rectangle.
*/
-template <class T>
+template <class T>
class Rect
{
public:
class Rect
{
public:
-
+
Rect ()
: x (0)
, y (0)
Rect ()
: x (0)
, y (0)
@@
-66,30
+69,57
@@
public:
return Position<T> (x, y);
}
return Position<T> (x, y);
}
-
Rect<T
> intersection (Rect<T> const & other) const
+
boost::optional<Rect<T>
> intersection (Rect<T> 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,
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<Rect<T> > ();
+ }
+
+ return r;
}
void extend (Rect<T> const & other)
{
}
void extend (Rect<T> const & other)
{
+ T old_x = x;
+ T old_y = y;
x = std::min (x, other.x);
y = std::min (y, other.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<T> extended (T amount) const {
+ Rect<T> c = *this;
+ c.x -= amount;
+ c.y -= amount;
+ c.width += amount * 2;
+ c.height += amount * 2;
+ return c;
}
bool contains (Position<T> p) const
{
return (p.x >= x && p.x <= (x + width) && p.y >= y && p.y <= (y + height));
}
}
bool contains (Position<T> p) const
{
return (p.x >= x && p.x <= (x + width) && p.y >= y && p.y <= (y + height));
}
+
+ bool equal (Rect<T> const & other, T threshold) const
+ {
+ return std::abs(x - other.x) < threshold &&
+ std::abs(y - other.y) < threshold &&
+ std::abs(width - other.width) < threshold &&
+ std::abs(height - other.height) < threshold;
+ }
};
}
};
}