comparison engine/core/util/structures/rect.h @ 0:4a0efb7baf70

* Datasets becomes the new trunk and retires after that :-)
author mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
date Sun, 29 Jun 2008 18:44:17 +0000
parents
children 90005975cdbb
comparison
equal deleted inserted replaced
-1:000000000000 0:4a0efb7baf70
1 /***************************************************************************
2 * Copyright (C) 2005-2008 by the FIFE team *
3 * http://www.fifengine.de *
4 * This file is part of FIFE. *
5 * *
6 * FIFE is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the *
18 * Free Software Foundation, Inc., *
19 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
20 ***************************************************************************/
21
22 /***************************************************************************
23
24 Rectangle intersection code copied and modified from the guichan 0.4
25 source, which is released under the BSD license.
26
27 Copyright (c) 2004, 2005, 2006 Olof Naessén and Per Larsson All rights reserved.
28
29 * Redistribution and use in source and binary forms, with or without modification,
30 are permitted provided that the following conditions are met:
31 Redistributions of source code must retain the above copyright notice,
32 this list of conditions and the following disclaimer.
33
34 * Redistributions in binary form must reproduce the above copyright notice,
35 this list of conditions and the following disclaimer in the documentation
36 and/or other materials provided with the distribution.
37
38 * Neither the name of the Guichan nor the names of its contributors may be used
39 to endorse or promote products derived from this software without specific
40 prior written permission.
41
42 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
43 OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
44 AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
45 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
46 OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
47 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
48 AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
50 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
51
52 For more Information about guichan see: http://guichan.sourceforge.net
53
54 ****************************************************************************/
55
56 #ifndef FIFE_VIDEO_RECT_H
57 #define FIFE_VIDEO_RECT_H
58
59 // Standard C++ library includes
60 #include <iostream>
61
62 // 3rd party library includes
63
64 // FIFE includes
65 // These includes are split up in two parts, separated by one empty line
66 // First block: files included from the FIFE root src directory
67 // Second block: files included from the same folder
68 #include "point.h"
69
70 namespace FIFE {
71
72 /** A Rectangle on screen.
73 *
74 * This is a small helper class used for screen coordinate arithmetics.
75 * The same thoughts reasong using @b int as value type as in Point apply.
76 *
77 * @see Point
78 */
79 class Rect {
80 public:
81 /** The X Coordinate.
82 */
83 int x;
84 /** The Y Coordinate.
85 */
86 int y;
87 /** Width of the rectangle.
88 */
89 int w;
90 /** Height of the rectangle.
91 */
92 int h;
93
94 /** Constructor.
95 *
96 * Creates a new Rect with the values defaulting to 0.
97 */
98 explicit Rect(int x = 0, int y = 0, unsigned int width = 0, unsigned int height = 0);
99
100 /** The X coordinate of the right edge.
101 */
102 int right() const;
103
104 /** The Y coordinate of the bottom edge.
105 */
106 int bottom() const;
107
108 /** Equivalence operator.
109 *
110 * @param rect The rectangle to which this is compared.
111 * @return True only if both rectangle values are all equal.
112 */
113 bool operator==(const Rect& rect ) const;
114
115 /** Checks whether a rectangle contains a Point.
116 *
117 * @param p The point that is checked.
118 * @return True if the point lies inside the rectangle or on one of its borders.
119 */
120 bool contains( const Point& point ) const;
121
122 /** Check whether two rectangles share some area.
123 *
124 * @param rect The other rectangle that is checked.
125 * @return True, if and only if both rectangles have some covered area in common.
126 * This includes edges that cover each other.
127 * @note This operation is commutative.
128 */
129 bool intersects( const Rect& rect ) const;
130
131 /** Calculate rectangle intersection in place
132 *
133 * @param rect The other rectangle that is checked.
134 * @return True, if and only if both rectangles have some covered area in common.
135 * This includes edges that cover each other.
136 */
137 bool intersectInplace( const Rect& rect );
138
139 };
140
141 /** Stream output operator.
142 *
143 * Useful for debugging purposes, this will output the coordinates
144 * of the rectangle to the stream.
145 */
146 std::ostream& operator<<(std::ostream&, const Rect&);
147
148
149 //////////////// INLINE FUNCTIONS /////////////////////////
150
151 inline
152 int Rect::right() const {
153 return x + w;
154 }
155
156 inline
157 int Rect::bottom() const {
158 return y + h;
159 }
160
161
162 inline
163 bool Rect::operator==(const Rect& rect ) const {
164 return
165 x == rect.x && y == rect.y && w == rect.w && h == rect.h;
166 }
167
168
169 inline
170 bool Rect::contains( const Point& point ) const {
171 return
172 (((point.x >= x) && (point.x <= x + w))
173 && ((point.y >= y) && (point.y <= y + h)));
174 }
175
176
177 inline
178 bool Rect::intersectInplace( const Rect& rectangle ) {
179 x = x - rectangle.x;
180 y = y - rectangle.y;
181
182
183 if (x < 0) {
184 w += x;
185 x = 0;
186 }
187
188 if (y < 0) {
189 h += y;
190 y = 0;
191 }
192
193 if (x + w > rectangle.w) {
194 w = rectangle.w - x;
195 }
196
197 if (y + h > rectangle.h) {
198 h = rectangle.h - y;
199 }
200
201 x += rectangle.x;
202 y += rectangle.y;
203
204 if (w <= 0 || h <= 0) {
205 h = 0;
206 w = 0;
207 return false;
208 }
209 return true;
210 }
211
212
213 inline
214 bool Rect::intersects( const Rect& rectangle ) const {
215 int _x = x - rectangle.x;
216 int _y = y - rectangle.y;
217 int _w = w;
218 int _h = h;
219
220
221 if (_x < 0) {
222 _w += _x;
223 _x = 0;
224 }
225
226 if (_y < 0) {
227 _h += _y;
228 _y = 0;
229 }
230
231 if (_x + _w > rectangle.w) {
232 _w = rectangle.w - _x;
233 }
234
235 if (_y + _h > rectangle.h) {
236 _h = rectangle.h - _y;
237 }
238
239 if (_w <= 0 || _h <= 0) {
240 return false;
241 }
242 return true;
243 }
244
245 }
246
247 #endif