ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

rect.h

Go to the documentation of this file.
00001 /* ScummVM - Graphic Adventure Engine
00002  *
00003  * ScummVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the COPYRIGHT
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #ifndef COMMON_RECT_H
00024 #define COMMON_RECT_H
00025 
00026 #include "common/scummsys.h"
00027 #include "common/util.h"
00028 #include "common/debug.h"
00029 
00030 #define PRINT_RECT(x) (x).left,(x).top,(x).right,(x).bottom
00031 
00032 namespace Common {
00033 
00037 struct Point {
00038     int16 x;    
00039     int16 y;    
00040 
00041     Point() : x(0), y(0) {}
00042     Point(int16 x1, int16 y1) : x(x1), y(y1) {}
00043     bool  operator==(const Point &p)    const { return x == p.x && y == p.y; }
00044     bool  operator!=(const Point &p)    const { return x != p.x || y != p.y; }
00045     Point operator+(const Point &delta) const { return Point(x + delta.x, y + delta.y); }
00046     Point operator-(const Point &delta) const { return Point(x - delta.x, y - delta.y); }
00047 
00048     void operator+=(const Point &delta) {
00049         x += delta.x;
00050         y += delta.y;
00051     }
00052 
00053     void operator-=(const Point &delta) {
00054         x -= delta.x;
00055         y -= delta.y;
00056     }
00057 
00064     uint sqrDist(const Point &p) const {
00065         int diffx = ABS(p.x - x);
00066         if (diffx >= 0x1000)
00067             return 0xFFFFFF;
00068 
00069         int diffy = ABS(p.y - y);
00070         if (diffy >= 0x1000)
00071             return 0xFFFFFF;
00072 
00073         return uint(diffx * diffx + diffy * diffy);
00074     }
00075 };
00076 
00095 struct Rect {
00096     int16 top, left;        
00097     int16 bottom, right;    
00098 
00099     Rect() : top(0), left(0), bottom(0), right(0) {}
00100     Rect(int16 w, int16 h) : top(0), left(0), bottom(h), right(w) {}
00101     Rect(int16 x1, int16 y1, int16 x2, int16 y2) : top(y1), left(x1), bottom(y2), right(x2) {
00102         assert(isValidRect());
00103     }
00104     bool operator==(const Rect &rhs) const { return equals(rhs); }
00105     bool operator!=(const Rect &rhs) const { return !equals(rhs); }
00106 
00107     int16 width() const { return right - left; }
00108     int16 height() const { return bottom - top; }
00109 
00110     void setWidth(int16 aWidth) {
00111         right = left + aWidth;
00112     }
00113 
00114     void setHeight(int16 aHeight) {
00115         bottom = top + aHeight;
00116     }
00117 
00126     bool contains(int16 x, int16 y) const {
00127         return (left <= x) && (x < right) && (top <= y) && (y < bottom);
00128     }
00129 
00137     bool contains(const Point &p) const {
00138         return contains(p.x, p.y);
00139     }
00140 
00148     bool contains(const Rect &r) const {
00149         return (left <= r.left) && (r.right <= right) && (top <= r.top) && (r.bottom <= bottom);
00150     }
00151 
00159     bool equals(const Rect &r) const {
00160         return (left == r.left) && (right == r.right) && (top == r.top) && (bottom == r.bottom);
00161     }
00162 
00171     bool intersects(const Rect &r) const {
00172         return (left < r.right) && (r.left < right) && (top < r.bottom) && (r.top < bottom);
00173     }
00174 
00182     Rect findIntersectingRect(const Rect &r) const {
00183         if (!intersects(r))
00184             return Rect();
00185 
00186         return Rect(MAX(r.left, left), MAX(r.top, top), MIN(r.right, right), MIN(r.bottom, bottom));
00187     }
00188 
00194     void extend(const Rect &r) {
00195         left = MIN(left, r.left);
00196         right = MAX(right, r.right);
00197         top = MIN(top, r.top);
00198         bottom = MAX(bottom, r.bottom);
00199     }
00200 
00206     void grow(int16 offset) {
00207         top -= offset;
00208         left -= offset;
00209         bottom += offset;
00210         right += offset;
00211     }
00212 
00213     void clip(const Rect &r) {
00214         assert(isValidRect());
00215         assert(r.isValidRect());
00216 
00217         if (top < r.top) top = r.top;
00218         else if (top > r.bottom) top = r.bottom;
00219 
00220         if (left < r.left) left = r.left;
00221         else if (left > r.right) left = r.right;
00222 
00223         if (bottom > r.bottom) bottom = r.bottom;
00224         else if (bottom < r.top) bottom = r.top;
00225 
00226         if (right > r.right) right = r.right;
00227         else if (right < r.left) right = r.left;
00228     }
00229 
00230     void clip(int16 maxw, int16 maxh) {
00231         clip(Rect(0, 0, maxw, maxh));
00232     }
00233 
00234     bool isEmpty() const {
00235         return (left >= right || top >= bottom);
00236     }
00237 
00238     bool isValidRect() const {
00239         return (left <= right && top <= bottom);
00240     }
00241 
00242     void moveTo(int16 x, int16 y) {
00243         bottom += y - top;
00244         right += x - left;
00245         top = y;
00246         left = x;
00247     }
00248 
00249     void translate(int16 dx, int16 dy) {
00250         left += dx; right += dx;
00251         top += dy; bottom += dy;
00252     }
00253 
00254     void moveTo(const Point &p) {
00255         moveTo(p.x, p.y);
00256     }
00257 
00258     void debugPrint(int debuglevel = 0, const char *caption = "Rect:") const {
00259         debug(debuglevel, "%s %d, %d, %d, %d", caption, left, top, right, bottom);
00260     }
00261 
00266     static Rect center(int16 cx, int16 cy, int16 w, int16 h) {
00267         int x = cx - w / 2, y = cy - h / 2;
00268         return Rect(x, y, x + w, y + h);
00269     }
00270 };
00271 
00272 } // End of namespace Common
00273 
00274 #endif


Generated on Sat Sep 14 2019 05:01:05 for ResidualVM by doxygen 1.7.1
curved edge   curved edge