1 // Copyright (C) 2007 Ole Laursen
3 // This program is free software; you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation; either version 3 of the License, or
6 // (at your option) any later version.
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 Library General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25 Rectangle() : x(pos.x), y(pos.y), w(dim.x), h(dim.y) {}
27 Rectangle(int x_, int y_, int w_, int h_)
28 : pos(x_, y_), dim(w_, h_), x(pos.x), y(pos.y), w(dim.x), h(dim.y) {}
30 Rectangle(Vector<int> pos_, Vector<int> dim_)
31 : pos(pos_), dim(dim_), x(pos.x), y(pos.y), w(dim.x), h(dim.y) {}
33 Rectangle(const Rectangle &other)
34 : pos(other.pos), dim(other.dim), x(pos.x), y(pos.y), w(dim.x), h(dim.y) {}
36 const Rectangle &operator=(const Rectangle &other)
43 Vector<int> pos, dim; // position and dimensions
45 // accessors - sometimes it's easier with .x instead of .pos.x
49 inline bool operator==(const Rectangle &lhs, const Rectangle &rhs)
51 return lhs.pos == rhs.pos && lhs.dim == rhs.dim;
54 inline bool operator!=(const Rectangle &lhs, const Rectangle &rhs)
59 inline bool is_inside(const Rectangle &r, Vector<int> v)
61 return r.x <= v.x && v.x < r.x + r.w
62 && r.y <= v.y && v.y < r.y + r.h;
65 inline bool is_overlapping(const Rectangle &r1, const Rectangle &r2)
67 // find the leftmost rectangle
68 Rectangle const *l, *r;
80 // leftmost is too far to the left
81 if (l->x + l->w <= r->x)
84 // find the upper rectangle
85 Rectangle const *u, *d;
97 // upper is too high up
98 if (u->y + u->h <= d->y)