summaryrefslogtreecommitdiff
path: root/src/RectangleUtil.hh
blob: 9725e63c88e675a8f8adaa0ac191bc056e033add (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#ifndef RECTANGLEUTIL_HH
#define RECTANGLEUTIL_HH

namespace RectangleUtil {


/*
 * Determines if a point is inside a rectangle-like objects border.
 * @param rect A rectangle-like object that has accessors for x, y, width, and
 *        height.
 * @param x
 * @param y 
 * @param border_width The size of the border.
 * @returns true if point is inside the rectangle-like object.
*/
template <typename RectangleLike>
bool insideBorder(const RectangleLike& rect,
                  int x, int y,
                  int border_width) {
    return
        x >= rect.x() + border_width &&
        x < rect.x() + (int)rect.width() + border_width &&
        y >= rect.y() + border_width &&
        y < rect.y() + (int)rect.height() + border_width;
}



/*
 * Determines if rectangle 'a' overlaps rectangle 'b'
 * @returns true if 'a' overlaps 'b'
 *
 *    outside              overlap situations
 *
 *  a----a            a----a         a--------a  b--------b
 *  |    | b----b     |  b-+--b      | b----b |  | a----a |
 *  a----a |    |     a--+-a  |      | |    | |  | |    | |
 *         b----b        b----b      | b----b |  | a----a |
 *                                   a--------a  b--------b
 *
 */

inline bool overlapRectangles(
        int ax, int ay, int awidth, int aheight,
        int bx, int by, int bwidth, int bheight) {

    bool do_not_overlap =
         ax > (bx + bwidth)
      || bx > (ax + awidth)
      || ay > (by + bheight)
      || by > (ay + aheight);

    return !do_not_overlap;
}


/*
 * Determines if rectangle 'a' overlaps rectangle 'b'
 * @param a - rectangle a
 * @param b - rectangle b
 * @returns true if 'a' overlaps 'b'
 */
template <typename RectangleLikeA, typename RectangleLikeB>
bool overlapRectangles(const RectangleLikeA& a, const RectangleLikeB& b) {

    return overlapRectangles(
            a.x(), a.y(), a.width(), a.height(),
            b.x(), b.y(), b.width(), b.height());
}

} // namespace RectangleUtil


#endif // RECTANGLEUTIL_HH