2
* Copyright © 2013 Canonical Ltd.
4
* This program is free software: you can redistribute it and/or modify it
5
* under the terms of the GNU Lesser General Public License version 3,
6
* as published by the Free Software Foundation.
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU Lesser General Public License for more details.
13
* You should have received a copy of the GNU Lesser General Public License
14
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16
* Authored by: Alexandros Frantzis <alexandros.frantzis@canonical.com>
19
#include "mir/geometry/rectangle.h"
23
namespace geom = mir::geometry;
25
geom::Point geom::Rectangle::bottom_right() const
27
return {top_left.x.as_int() + size.width.as_int(),
28
top_left.y.as_int() + size.height.as_int()};
31
geom::Point geom::Rectangle::top_right() const
33
return top_left + DeltaX{size.width.as_int()};
36
geom::Point geom::Rectangle::bottom_left() const
38
return top_left + DeltaY{size.height.as_int()};
41
bool geom::Rectangle::contains(Rectangle const& r) const
43
return r.top_left.x >= top_left.x &&
44
r.top_left.x.as_int() + r.size.width.as_int() <=
45
top_left.x.as_int() + size.width.as_int() &&
46
r.top_left.y >= top_left.y &&
47
r.top_left.y.as_int() + r.size.height.as_int() <=
48
top_left.y.as_int() + size.height.as_int();
51
bool geom::Rectangle::contains(Point const& p) const
53
if (size.width == geom::Width{0} || size.height == geom::Height{0})
56
auto br = bottom_right();
57
return p.x >= top_left.x && p.x < br.x &&
58
p.y >= top_left.y && p.y < br.y;
61
bool geom::Rectangle::overlaps(Rectangle const& r) const
63
if (size.width > geom::Width{0} && size.height > geom::Height{0} &&
64
r.size.width > geom::Width{0} && r.size.height > geom::Height{0})
67
auto br1 = bottom_right();
68
auto tl2 = r.top_left;
69
auto br2 = r.bottom_right();
71
return !(tl2.x >= br1.x || br2.x <= tl1.x ||
72
tl2.y >= br1.y || br2.y <= tl1.y);
80
geom::Rectangle geom::Rectangle::intersection_with(Rectangle const& r) const
82
int const a = std::max(top_left.x.as_int(), r.top_left.x.as_int());
83
int const b = std::min(bottom_right().x.as_int(), r.bottom_right().x.as_int());
84
int const c = std::max(top_left.y.as_int(), r.top_left.y.as_int());
85
int const d = std::min(bottom_right().y.as_int(), r.bottom_right().y.as_int());
88
return {{a, c}, {b - a, d - c}};
90
return geom::Rectangle();