Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1 | /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved. |
| 2 | * |
| 3 | * This 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 2 of the License, or |
| 6 | * (at your option) any later version. |
| 7 | * |
| 8 | * This software 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 General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this software; if not, write to the Free Software |
| 15 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
| 16 | * USA. |
| 17 | */ |
| 18 | |
| 19 | // rfb::Rect and rfb::Point structures |
| 20 | |
| 21 | #ifndef __RFB_RECT_INCLUDED__ |
| 22 | #define __RFB_RECT_INCLUDED__ |
| 23 | |
| 24 | // Some platforms (e.g. Windows) include max() and min() macros in their |
| 25 | // standard headers, but they are also standard C++ template functions, so some |
| 26 | // C++ headers will undefine them. So we steer clear of the names min and max |
| 27 | // and define __rfbmin and __rfbmax instead. |
| 28 | |
| 29 | #ifndef __rfbmax |
| 30 | #define __rfbmax(a,b) (((a) > (b)) ? (a) : (b)) |
| 31 | #endif |
| 32 | #ifndef __rfbmin |
| 33 | #define __rfbmin(a,b) (((a) < (b)) ? (a) : (b)) |
| 34 | #endif |
| 35 | |
| 36 | namespace rfb { |
| 37 | |
| 38 | // rfb::Point |
| 39 | // |
| 40 | // Represents a point in 2D space, by X and Y coordinates. |
| 41 | // Can also be used to represent a delta, or offset, between |
| 42 | // two Points. |
| 43 | // Functions are provided to allow Points to be compared for |
| 44 | // equality and translated by a supplied offset. |
| 45 | // Functions are also provided to negate offset Points. |
| 46 | |
| 47 | struct Point { |
| 48 | Point() : x(0), y(0) {} |
| 49 | Point(int x_, int y_) : x(x_), y(y_) {} |
| 50 | inline Point negate() const {return Point(-x, -y);} |
| 51 | inline bool equals(const Point &p) const {return x==p.x && y==p.y;} |
| 52 | inline Point translate(const Point &p) const {return Point(x+p.x, y+p.y);} |
| 53 | inline Point subtract(const Point &p) const {return Point(x-p.x, y-p.y);} |
| 54 | int x, y; |
| 55 | }; |
| 56 | |
| 57 | // rfb::Rect |
| 58 | // |
| 59 | // Represents a rectangular region defined by its top-left (tl) |
| 60 | // and bottom-right (br) Points. |
| 61 | // Rects may be compared for equality, checked to determine whether |
| 62 | // or not they are empty, cleared (made empty), or intersected with |
| 63 | // one another. The bounding rectangle of two existing Rects |
| 64 | // may be calculated, as may the area of a Rect. |
| 65 | // Rects may also be translated, in the same way as Points, by |
| 66 | // an offset specified in a Point structure. |
| 67 | |
| 68 | struct Rect { |
| 69 | Rect() {} |
| 70 | Rect(Point tl_, Point br_) : tl(tl_), br(br_) {} |
| 71 | Rect(int x1, int y1, int x2, int y2) : tl(x1, y1), br(x2, y2) {} |
| 72 | inline void setXYWH(int x, int y, int w, int h) { |
| 73 | tl.x = x; tl.y = y; br.x = x+w; br.y = y+h; |
| 74 | } |
| 75 | inline Rect intersect(const Rect &r) const { |
| 76 | Rect result; |
| 77 | result.tl.x = __rfbmax(tl.x, r.tl.x); |
| 78 | result.tl.y = __rfbmax(tl.y, r.tl.y); |
| 79 | result.br.x = __rfbmax(__rfbmin(br.x, r.br.x), result.tl.x); |
| 80 | result.br.y = __rfbmax(__rfbmin(br.y, r.br.y), result.tl.y); |
| 81 | return result; |
| 82 | } |
| 83 | inline Rect union_boundary(const Rect &r) const { |
| 84 | if (r.is_empty()) return *this; |
| 85 | if (is_empty()) return r; |
| 86 | Rect result; |
| 87 | result.tl.x = __rfbmin(tl.x, r.tl.x); |
| 88 | result.tl.y = __rfbmin(tl.y, r.tl.y); |
| 89 | result.br.x = __rfbmax(br.x, r.br.x); |
| 90 | result.br.y = __rfbmax(br.y, r.br.y); |
| 91 | return result; |
| 92 | } |
| 93 | inline Rect translate(const Point &p) const { |
| 94 | return Rect(tl.translate(p), br.translate(p)); |
| 95 | } |
| 96 | inline bool equals(const Rect &r) const {return r.tl.equals(tl) && r.br.equals(br);} |
| 97 | inline bool is_empty() const {return (tl.x >= br.x) || (tl.y >= br.y);} |
| 98 | inline void clear() {tl = Point(); br = Point();} |
| 99 | inline bool enclosed_by(const Rect &r) const { |
| 100 | return (tl.x>=r.tl.x) && (tl.y>=r.tl.y) && (br.x<=r.br.x) && (br.y<=r.br.y); |
| 101 | } |
| 102 | inline bool overlaps(const Rect &r) const { |
| 103 | return tl.x < r.br.x && tl.y < r.br.y && br.x > r.tl.x && br.y > r.tl.y; |
| 104 | } |
Pierre Ossman | 615a0f5 | 2015-03-03 16:35:28 +0100 | [diff] [blame] | 105 | inline int area() const {return is_empty() ? 0 : (br.x-tl.x)*(br.y-tl.y);} |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 106 | inline Point dimensions() const {return Point(width(), height());} |
| 107 | inline int width() const {return br.x-tl.x;} |
| 108 | inline int height() const {return br.y-tl.y;} |
| 109 | inline bool contains(const Point &p) const { |
| 110 | return (tl.x<=p.x) && (tl.y<=p.y) && (br.x>p.x) && (br.y>p.y); |
| 111 | } |
| 112 | Point tl; |
| 113 | Point br; |
| 114 | }; |
| 115 | } |
| 116 | #endif // __RFB_RECT_INCLUDED__ |