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