~ubuntu-branches/ubuntu/natty/lordsawar/natty

« back to all changes in this revision

Viewing changes to src/rectangle.h

  • Committer: Bazaar Package Importer
  • Author(s): Barry deFreese
  • Date: 2007-09-27 21:27:07 UTC
  • Revision ID: james.westby@ubuntu.com-20070927212707-w1qzmylq1rx8ofod
Tags: upstream-0.0.3
ImportĀ upstreamĀ versionĀ 0.0.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//  This program is free software; you can redistribute it and/or modify
 
2
//  it under the terms of the GNU General Public License as published by
 
3
//  the Free Software Foundation; either version 2 of the License, or
 
4
//  (at your option) any later version.
 
5
//
 
6
//  This program is distributed in the hope that it will be useful,
 
7
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
8
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
9
//  GNU Library General Public License for more details.
 
10
//
 
11
//  You should have received a copy of the GNU General Public License
 
12
//  along with this program; if not, write to the Free Software
 
13
//  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
14
 
 
15
#ifndef RECTANGLE_H
 
16
#define RECTANGLE_H
 
17
 
 
18
#include "vector.h"
 
19
 
 
20
struct Rectangle
 
21
{
 
22
    Rectangle() : x(pos.x), y(pos.y), w(dim.x), h(dim.y) {}
 
23
 
 
24
    Rectangle(int x_, int y_, int w_, int h_)
 
25
        : pos(x_, y_), dim(w_, h_), x(pos.x), y(pos.y), w(dim.x), h(dim.y) {}
 
26
 
 
27
    Rectangle(Vector<int> pos_, Vector<int> dim_)
 
28
        : pos(pos_), dim(dim_), x(pos.x), y(pos.y), w(dim.x), h(dim.y) {}
 
29
    
 
30
    Rectangle(const Rectangle &other)
 
31
        : pos(other.pos), dim(other.dim), x(pos.x), y(pos.y), w(dim.x), h(dim.y) {}
 
32
 
 
33
    const Rectangle &operator=(const Rectangle &other)
 
34
    {
 
35
        pos = other.pos;
 
36
        dim = other.dim;
 
37
        return *this;
 
38
    }
 
39
 
 
40
    Vector<int> pos, dim; // position and dimensions
 
41
 
 
42
    // accessors - sometimes it's easier with .x instead of .pos.x
 
43
    int &x, &y, &w, &h;
 
44
};
 
45
 
 
46
inline bool operator==(const Rectangle &lhs, const Rectangle &rhs)
 
47
{
 
48
    return lhs.pos == rhs.pos && lhs.dim == rhs.dim;
 
49
}
 
50
 
 
51
inline bool operator!=(const Rectangle &lhs, const Rectangle &rhs)
 
52
{
 
53
    return !(lhs == rhs);
 
54
}
 
55
 
 
56
inline bool is_inside(const Rectangle &r, Vector<int> v)
 
57
{
 
58
    return r.x <= v.x && v.x < r.x + r.w
 
59
        && r.y <= v.y && v.y < r.y + r.h;
 
60
}
 
61
 
 
62
inline bool is_overlapping(const Rectangle &r1, const Rectangle &r2)
 
63
{
 
64
    // find the leftmost rectangle
 
65
    Rectangle const *l, *r;
 
66
    if (r1.x <= r2.x)
 
67
    {
 
68
        l = &r1;
 
69
        r = &r2;
 
70
    }
 
71
    else
 
72
    {
 
73
        l = &r2;
 
74
        r = &r1;
 
75
    }
 
76
    
 
77
    // leftmost is too far to the left
 
78
    if (l->x + l->w <= r->x)
 
79
        return false;
 
80
 
 
81
    // find the upper rectangle
 
82
    Rectangle const *u, *d;
 
83
    if (r1.y <= r2.y)
 
84
    {
 
85
        u = &r1;
 
86
        d = &r2;
 
87
    }
 
88
    else
 
89
    {
 
90
        u = &r2;
 
91
        d = &r1;
 
92
    }
 
93
 
 
94
    // upper is too high up
 
95
    if (u->y + u->h <= d->y)
 
96
        return false;
 
97
 
 
98
    return true;
 
99
}
 
100
 
 
101
#endif