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.
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.
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.
22
Rectangle() : x(pos.x), y(pos.y), w(dim.x), h(dim.y) {}
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) {}
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) {}
30
Rectangle(const Rectangle &other)
31
: pos(other.pos), dim(other.dim), x(pos.x), y(pos.y), w(dim.x), h(dim.y) {}
33
const Rectangle &operator=(const Rectangle &other)
40
Vector<int> pos, dim; // position and dimensions
42
// accessors - sometimes it's easier with .x instead of .pos.x
46
inline bool operator==(const Rectangle &lhs, const Rectangle &rhs)
48
return lhs.pos == rhs.pos && lhs.dim == rhs.dim;
51
inline bool operator!=(const Rectangle &lhs, const Rectangle &rhs)
56
inline bool is_inside(const Rectangle &r, Vector<int> v)
58
return r.x <= v.x && v.x < r.x + r.w
59
&& r.y <= v.y && v.y < r.y + r.h;
62
inline bool is_overlapping(const Rectangle &r1, const Rectangle &r2)
64
// find the leftmost rectangle
65
Rectangle const *l, *r;
77
// leftmost is too far to the left
78
if (l->x + l->w <= r->x)
81
// find the upper rectangle
82
Rectangle const *u, *d;
94
// upper is too high up
95
if (u->y + u->h <= d->y)