00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056 #ifndef FIFE_VIDEO_RECT_H
00057 #define FIFE_VIDEO_RECT_H
00058
00059
00060 #include <iostream>
00061
00062
00063
00064
00065
00066
00067
00068 #include "point.h"
00069
00070 namespace FIFE {
00071
00079 template <typename T>
00080 class RectType {
00081 public:
00084 T x;
00087 T y;
00090 T w;
00093 T h;
00094
00099 explicit RectType(T x = 0, T y = 0, T w = 0, T h = 0) : x(x), y(y), w(w), h(h) {
00100 }
00101
00106 template<typename U>
00107 explicit RectType(const RectType<U>& r)
00108 : x(static_cast<T>(r.x)),
00109 y(static_cast<T>(r.y)),
00110 w(static_cast<T>(r.w)),
00111 h(static_cast<T>(r.h)) {
00112 }
00113
00116 T right() const;
00117
00120 T bottom() const;
00121
00127 bool operator==(const RectType<T>& rect ) const;
00128
00134 bool contains( const PointType2D<T>& point ) const;
00135
00143 bool intersects( const RectType<T>& rect ) const;
00144
00151 bool intersectInplace( const RectType<T>& rect );
00152 };
00153
00159 template<typename T>
00160 std::ostream& operator<<(std::ostream& os, const RectType<T>& r) {
00161 return
00162 os << "("<<r.x<<","<<r.y<<")-("<<r.w<<","<<r.h<<")";
00163 }
00164
00166
00167 template<typename T>
00168 inline T RectType<T>::right() const {
00169 return x + w;
00170 }
00171
00172 template<typename T>
00173 inline T RectType<T>::bottom() const {
00174 return y + h;
00175 }
00176
00177 template<typename T>
00178 inline bool RectType<T>::operator==(const RectType<T>& rect ) const {
00179 return
00180 x == rect.x && y == rect.y && w == rect.w && h == rect.h;
00181 }
00182
00183 template<typename T>
00184 inline bool RectType<T>::contains( const PointType2D<T>& point ) const {
00185 return
00186 (((point.x >= x) && (point.x <= x + w))
00187 && ((point.y >= y) && (point.y <= y + h)));
00188 }
00189
00190
00191 template<typename T>
00192 inline bool RectType<T>::intersectInplace( const RectType<T>& rectangle ) {
00193 x = x - rectangle.x;
00194 y = y - rectangle.y;
00195
00196
00197 if (x < 0) {
00198 w += x;
00199 x = 0;
00200 }
00201
00202 if (y < 0) {
00203 h += y;
00204 y = 0;
00205 }
00206
00207 if (x + w > rectangle.w) {
00208 w = rectangle.w - x;
00209 }
00210
00211 if (y + h > rectangle.h) {
00212 h = rectangle.h - y;
00213 }
00214
00215 x += rectangle.x;
00216 y += rectangle.y;
00217
00218 if (w <= 0 || h <= 0) {
00219 h = 0;
00220 w = 0;
00221 return false;
00222 }
00223 return true;
00224 }
00225
00226 template<typename T>
00227 inline bool RectType<T>::intersects( const RectType<T>& rectangle ) const {
00228 T _x = x - rectangle.x;
00229 T _y = y - rectangle.y;
00230 T _w = w;
00231 T _h = h;
00232
00233
00234 if (_x < 0) {
00235 _w += _x;
00236 _x = 0;
00237 }
00238
00239 if (_y < 0) {
00240 _h += _y;
00241 _y = 0;
00242 }
00243
00244 if (_x + _w > rectangle.w) {
00245 _w = rectangle.w - _x;
00246 }
00247
00248 if (_y + _h > rectangle.h) {
00249 _h = rectangle.h - _y;
00250 }
00251
00252 if (_w <= 0 || _h <= 0) {
00253 return false;
00254 }
00255 return true;
00256 }
00257
00258 typedef RectType<int32_t> Rect;
00259 typedef RectType<float> FloatRect;
00260 typedef RectType<double> DoubleRect;
00261
00262
00263 }
00264
00265 #endif