Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame] | 1 | /* Copyright (C) 2002-2003 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 | |
Peter Åstrand | 7cc6c94 | 2005-01-17 15:09:46 +0000 | [diff] [blame^] | 24 | #ifndef vncmin |
| 25 | #define vncmin(a,b) (((a) < (b)) ? (a) : (b)) |
| 26 | #endif |
| 27 | #ifndef vncmax |
| 28 | #define vncmax(a,b) (((a) > (b)) ? (a) : (b)) |
| 29 | #endif |
Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame] | 30 | |
| 31 | namespace rfb { |
| 32 | |
| 33 | // rfb::Point |
| 34 | // |
| 35 | // Represents a point in 2D space, by X and Y coordinates. |
| 36 | // Can also be used to represent a delta, or offset, between |
| 37 | // two Points. |
| 38 | // Functions are provided to allow Points to be compared for |
| 39 | // equality and translated by a supplied offset. |
| 40 | // Functions are also provided to negate offset Points. |
| 41 | |
| 42 | struct Point { |
| 43 | Point() : x(0), y(0) {} |
| 44 | Point(int x_, int y_) : x(x_), y(y_) {} |
| 45 | inline Point negate() const {return Point(-x, -y);} |
| 46 | inline bool equals(const Point &p) const {return x==p.x && y==p.y;} |
| 47 | inline Point translate(const Point &p) const {return Point(x+p.x, y+p.y);} |
| 48 | inline Point subtract(const Point &p) const {return Point(x-p.x, y-p.y);} |
| 49 | int x, y; |
| 50 | }; |
| 51 | |
| 52 | // rfb::Rect |
| 53 | // |
| 54 | // Represents a rectangular region defined by its top-left (tl) |
| 55 | // and bottom-right (br) Points. |
| 56 | // Rects may be compared for equality, checked to determine whether |
| 57 | // or not they are empty, cleared (made empty), or intersected with |
| 58 | // one another. The bounding rectangle of two existing Rects |
| 59 | // may be calculated, as may the area of a Rect. |
| 60 | // Rects may also be translated, in the same way as Points, by |
| 61 | // an offset specified in a Point structure. |
| 62 | |
| 63 | struct Rect { |
| 64 | Rect() {} |
| 65 | Rect(Point tl_, Point br_) : tl(tl_), br(br_) {} |
| 66 | Rect(int x1, int y1, int x2, int y2) : tl(x1, y1), br(x2, y2) {} |
| 67 | inline void setXYWH(int x, int y, int w, int h) { |
| 68 | tl.x = x; tl.y = y; br.x = x+w; br.y = y+h; |
| 69 | } |
| 70 | inline Rect intersect(const Rect &r) const { |
| 71 | Rect result; |
Peter Åstrand | 0f49e22 | 2005-01-13 22:11:30 +0000 | [diff] [blame] | 72 | result.tl.x = vncmax(tl.x, r.tl.x); |
| 73 | result.tl.y = vncmax(tl.y, r.tl.y); |
| 74 | result.br.x = vncmax(vncmin(br.x, r.br.x), result.tl.x); |
| 75 | result.br.y = vncmax(vncmin(br.y, r.br.y), result.tl.y); |
Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame] | 76 | return result; |
| 77 | } |
| 78 | inline Rect union_boundary(const Rect &r) const { |
| 79 | if (r.is_empty()) return *this; |
| 80 | if (is_empty()) return r; |
| 81 | Rect result; |
Peter Åstrand | 0f49e22 | 2005-01-13 22:11:30 +0000 | [diff] [blame] | 82 | result.tl.x = vncmin(tl.x, r.tl.x); |
| 83 | result.tl.y = vncmin(tl.y, r.tl.y); |
| 84 | result.br.x = vncmax(br.x, r.br.x); |
| 85 | result.br.y = vncmax(br.y, r.br.y); |
Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame] | 86 | return result; |
| 87 | } |
| 88 | inline Rect translate(const Point &p) const { |
| 89 | return Rect(tl.translate(p), br.translate(p)); |
| 90 | } |
| 91 | inline bool equals(const Rect &r) const {return r.tl.equals(tl) && r.br.equals(br);} |
| 92 | inline bool is_empty() const {return (tl.x >= br.x) || (tl.y >= br.y);} |
| 93 | inline void clear() {tl = Point(); br = Point();} |
| 94 | inline bool enclosed_by(const Rect &r) const { |
| 95 | return (tl.x>=r.tl.x) && (tl.y>=r.tl.y) && (br.x<=r.br.x) && (br.y<=r.br.y); |
| 96 | } |
| 97 | inline bool overlaps(const Rect &r) const { |
| 98 | return tl.x < r.br.x && tl.y < r.br.y && br.x > r.tl.x && br.y > r.tl.y; |
| 99 | } |
| 100 | inline unsigned int area() const {return is_empty() ? 0 : (br.x-tl.x)*(br.y-tl.y);} |
| 101 | inline Point dimensions() const {return Point(width(), height());} |
| 102 | inline int width() const {return br.x-tl.x;} |
| 103 | inline int height() const {return br.y-tl.y;} |
| 104 | inline bool contains(const Point &p) const { |
| 105 | return (tl.x<=p.x) && (tl.y<=p.y) && (br.x>p.x) && (br.y>p.y); |
| 106 | } |
| 107 | Point tl; |
| 108 | Point br; |
| 109 | }; |
| 110 | } |
| 111 | #endif // __RFB_RECT_INCLUDED__ |