FIFE  6e1afdbeda11afe9ac53e6023a4be96ef88f1dc6
rect.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2017 by the FIFE team *
3  * http://www.fifengine.net *
4  * This file is part of FIFE. *
5  * *
6  * FIFE is free software; you can redistribute it and/or *
7  * modify it under the terms of the GNU Lesser General Public *
8  * License as published by the Free Software Foundation; either *
9  * version 2.1 of the License, or (at your option) any later version. *
10  * *
11  * This library 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 GNU *
14  * Lesser General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU Lesser General Public *
17  * License along with this library; if not, write to the *
18  * Free Software Foundation, Inc., *
19  * 51 Franklin Street, 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 
79  template <typename T>
80  class RectType {
81  public:
84  T x;
87  T y;
90  T w;
93  T h;
94 
99  explicit RectType(T x = 0, T y = 0, T w = 0, T h = 0) : x(x), y(y), w(w), h(h) {
100  }
101 
106  template<typename U>
107  explicit RectType(const RectType<U>& r)
108  : x(static_cast<T>(r.x)),
109  y(static_cast<T>(r.y)),
110  w(static_cast<T>(r.w)),
111  h(static_cast<T>(r.h)) {
112  }
113 
116  T right() const;
117 
120  T bottom() const;
121 
127  bool operator==(const RectType<T>& rect ) const;
128 
134  bool contains( const PointType2D<T>& point ) const;
135 
143  bool intersects( const RectType<T>& rect ) const;
144 
151  bool intersectInplace( const RectType<T>& rect );
152  };
153 
159  template<typename T>
160  std::ostream& operator<<(std::ostream& os, const RectType<T>& r) {
161  return
162  os << "("<<r.x<<","<<r.y<<")-("<<r.w<<","<<r.h<<")";
163  }
164 
166 
167  template<typename T>
168  inline T RectType<T>::right() const {
169  return x + w;
170  }
171 
172  template<typename T>
173  inline T RectType<T>::bottom() const {
174  return y + h;
175  }
176 
177  template<typename T>
178  inline bool RectType<T>::operator==(const RectType<T>& rect ) const {
179  return
180  x == rect.x && y == rect.y && w == rect.w && h == rect.h;
181  }
182 
183  template<typename T>
184  inline bool RectType<T>::contains( const PointType2D<T>& point ) const {
185  return
186  (((point.x >= x) && (point.x <= x + w))
187  && ((point.y >= y) && (point.y <= y + h)));
188  }
189 
190 
191  template<typename T>
192  inline bool RectType<T>::intersectInplace( const RectType<T>& rectangle ) {
193  x = x - rectangle.x;
194  y = y - rectangle.y;
195 
196 
197  if (x < 0) {
198  w += x;
199  x = 0;
200  }
201 
202  if (y < 0) {
203  h += y;
204  y = 0;
205  }
206 
207  if (x + w > rectangle.w) {
208  w = rectangle.w - x;
209  }
210 
211  if (y + h > rectangle.h) {
212  h = rectangle.h - y;
213  }
214 
215  x += rectangle.x;
216  y += rectangle.y;
217 
218  if (w <= 0 || h <= 0) {
219  h = 0;
220  w = 0;
221  return false;
222  }
223  return true;
224  }
225 
226  template<typename T>
227  inline bool RectType<T>::intersects( const RectType<T>& rectangle ) const {
228  T _x = x - rectangle.x;
229  T _y = y - rectangle.y;
230  T _w = w;
231  T _h = h;
232 
233 
234  if (_x < 0) {
235  _w += _x;
236  _x = 0;
237  }
238 
239  if (_y < 0) {
240  _h += _y;
241  _y = 0;
242  }
243 
244  if (_x + _w > rectangle.w) {
245  _w = rectangle.w - _x;
246  }
247 
248  if (_y + _h > rectangle.h) {
249  _h = rectangle.h - _y;
250  }
251 
252  if (_w <= 0 || _h <= 0) {
253  return false;
254  }
255  return true;
256  }
257 
261 
262 
263 }
264 
265 #endif
RectType< double > DoubleRect
Definition: rect.h:260
A Rectangle on screen.
Definition: rect.h:80
T h
Height of the rectangle.
Definition: rect.h:93
bool operator==(const RectType< T > &rect) const
Equivalence operator.
Definition: rect.h:178
T x
The X Coordinate.
Definition: rect.h:84
bool contains(const PointType2D< T > &point) const
Checks whether a rectangle contains a Point.
Definition: rect.h:184
bool intersectInplace(const RectType< T > &rect)
Calculate rectangle intersection in place.
Definition: rect.h:192
RectType(T x=0, T y=0, T w=0, T h=0)
Constructor.
Definition: rect.h:99
T bottom() const
The Y coordinate of the bottom edge.
Definition: rect.h:173
RectType(const RectType< U > &r)
Constructor.
Definition: rect.h:107
T y
The Y Coordinate.
Definition: rect.h:87
T right() const
The X coordinate of the right edge.
Definition: rect.h:168
bool intersects(const RectType< T > &rect) const
Check whether two rectangles share some area.
Definition: rect.h:227
A 2D Point.
Definition: point.h:48
RectType< int32_t > Rect
Definition: rect.h:258
RectType< float > FloatRect
Definition: rect.h:259
T w
Width of the rectangle.
Definition: rect.h:90