GG
PtRect.h
Go to the documentation of this file.
1 // -*- C++ -*-
2 /* GG is a GUI for SDL and OpenGL.
3  Copyright (C) 2003-2008 T. Zachary Laine
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public License
7  as published by the Free Software Foundation; either version 2.1
8  of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, write to the Free
17  Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18  02111-1307 USA
19 
20  If you do not wish to comply with the terms of the LGPL please
21  contact the author as other terms are available for a fee.
22 
23  Zach Laine
24  whatwasthataddress@gmail.com */
25 
28 #ifndef _GG_PtRect_h_
29 #define _GG_PtRect_h_
30 
31 #include <GG/Base.h>
32 #include <GG/StrongTypedef.h>
33 
34 
35 namespace GG {
36 
42 
48 
49 // some useful coordinate constants
50 extern GG_API const X X0;
51 extern GG_API const X X1;
52 extern GG_API const Y Y0;
53 extern GG_API const Y Y1;
54 
56 struct GG_API Pt
57 {
59  Pt();
60  Pt(X x_, Y y_);
61  Pt(X_d x_, Y y_);
62  Pt(X x_, Y_d y_);
63  Pt(X_d x_, Y_d y_);
64 
65 
67 
70  bool Less(const Pt& rhs) const
71  { return x < rhs.x ? true : (x == rhs.x ? (y < rhs.y ? true : false) : false); }
73 
75  void operator+=(const Pt& rhs) { x += rhs.x; y += rhs.y; }
76  void operator-=(const Pt& rhs) { x -= rhs.x; y -= rhs.y; }
77  Pt operator-() const { return Pt(-x, -y); }
78 
79 
80  X x;
81  Y y;
82 };
83 
87 struct GG_API Rect
88 {
90  Rect();
91  Rect(const Pt& pt1, const Pt& pt2);
92  Rect(X x1, Y y1, X x2, Y y2);
93 
94 
96  X Left() const { return ul.x; }
97  X Right() const { return lr.x; }
98  Y Top() const { return ul.y; }
99  Y Bottom() const { return lr.y; }
100  Pt UpperLeft() const { return ul; }
101  Pt LowerRight() const { return lr; }
102  X Width() const { return lr.x - ul.x; }
103  Y Height() const { return lr.y - ul.y; }
104 
105  bool Contains(const Pt& pt) const;
106 
107 
109  void operator+=(const Pt& pt) { ul += pt; lr += pt; }
110  void operator-=(const Pt& pt) { ul -= pt; lr -= pt; }
111 
112 
113  Pt ul;
114  Pt lr;
115 };
116 
117 GG_API inline bool operator==(const Pt& lhs, const Pt& rhs) { return lhs.x == rhs.x && lhs.y == rhs.y; }
118 GG_API inline bool operator!=(const Pt& lhs, const Pt& rhs) { return !(lhs == rhs); }
119 GG_API inline bool operator<(const Pt& lhs, const Pt& rhs) { return lhs.x < rhs.x && lhs.y < rhs.y; }
120 GG_API inline bool operator>(const Pt& lhs, const Pt& rhs) { return lhs.x > rhs.x && lhs.y > rhs.y; }
121 GG_API inline bool operator<=(const Pt& lhs, const Pt& rhs) { return lhs.x <= rhs.x && lhs.y <= rhs.y; }
122 GG_API inline bool operator>=(const Pt& lhs, const Pt& rhs) { return lhs.x >= rhs.x && lhs.y >= rhs.y; }
123 GG_API inline Pt operator+(const Pt& lhs, const Pt& rhs) { return Pt(lhs.x + rhs.x, lhs.y + rhs.y); }
124 GG_API inline Pt operator-(const Pt& lhs, const Pt& rhs) { return Pt(lhs.x - rhs.x, lhs.y - rhs.y); }
125 
126 GG_API std::ostream& operator<<(std::ostream& os, const Pt& pt);
127 
129 GG_API inline bool operator==(const Rect& lhs, const Rect& rhs) { return lhs.ul.x == rhs.ul.x && lhs.lr.x == rhs.lr.x && lhs.lr.y == rhs.lr.y && lhs.lr.y == rhs.lr.y; }
130 
132 GG_API inline bool operator!=(const Rect& lhs, const Rect& rhs) { return !(lhs == rhs); }
133 
134 GG_API inline Rect operator+(const Rect& rect, const Pt& pt) { return Rect(rect.ul + pt, rect.lr + pt); }
135 GG_API inline Rect operator-(const Rect& rect, const Pt& pt) { return Rect(rect.ul - pt, rect.lr - pt); }
136 GG_API inline Rect operator+(const Pt& pt, const Rect& rect) { return rect + pt; }
137 GG_API inline Rect operator-(const Pt& pt, const Rect& rect) { return rect - pt; }
138 
139 GG_API std::ostream& operator<<(std::ostream& os, const Rect& rect);
140 
141 } // namepace GG
142 
143 #endif