~ubuntu-branches/ubuntu/wily/mir/wily-proposed

« back to all changes in this revision

Viewing changes to src/common/geometry/rectangle.cpp

  • Committer: Package Import Robot
  • Author(s): Ubuntu daily release
  • Date: 2014-10-10 14:01:26 UTC
  • mto: This revision was merged to the branch mainline in revision 84.
  • Revision ID: package-import@ubuntu.com-20141010140126-n1czko8na1kuz4ll
Tags: upstream-0.8.0+14.10.20141010
Import upstream version 0.8.0+14.10.20141010

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright © 2013 Canonical Ltd.
 
3
 *
 
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.
 
7
 *
 
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.
 
12
 *
 
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/>.
 
15
 *
 
16
 * Authored by: Alexandros Frantzis <alexandros.frantzis@canonical.com>
 
17
 */
 
18
 
 
19
#include "mir/geometry/rectangle.h"
 
20
 
 
21
#include <algorithm>
 
22
 
 
23
namespace geom = mir::geometry;
 
24
 
 
25
geom::Point geom::Rectangle::bottom_right() const
 
26
{
 
27
    return {top_left.x.as_int() + size.width.as_int(),
 
28
            top_left.y.as_int() + size.height.as_int()};
 
29
}
 
30
 
 
31
geom::Point geom::Rectangle::top_right() const
 
32
{
 
33
    return top_left + DeltaX{size.width.as_int()};
 
34
}
 
35
 
 
36
geom::Point geom::Rectangle::bottom_left() const
 
37
{
 
38
    return top_left + DeltaY{size.height.as_int()};
 
39
}
 
40
 
 
41
bool geom::Rectangle::contains(Rectangle const& r) const
 
42
{
 
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();
 
49
}
 
50
 
 
51
bool geom::Rectangle::contains(Point const& p) const
 
52
{
 
53
    if (size.width == geom::Width{0} || size.height == geom::Height{0})
 
54
        return false;
 
55
 
 
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;
 
59
}
 
60
 
 
61
bool geom::Rectangle::overlaps(Rectangle const& r) const
 
62
{
 
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})
 
65
    {
 
66
        auto tl1 = top_left;
 
67
        auto br1 = bottom_right();
 
68
        auto tl2 = r.top_left;
 
69
        auto br2 = r.bottom_right();
 
70
 
 
71
        return !(tl2.x >= br1.x || br2.x <= tl1.x ||
 
72
                 tl2.y >= br1.y || br2.y <= tl1.y);
 
73
    }
 
74
    else
 
75
    {
 
76
        return false;
 
77
    }
 
78
}
 
79
 
 
80
geom::Rectangle geom::Rectangle::intersection_with(Rectangle const& r) const
 
81
{
 
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());
 
86
 
 
87
    if (a < b && c < d)
 
88
        return {{a, c}, {b - a, d - c}};
 
89
    else
 
90
        return geom::Rectangle();
 
91
}