rect.h
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
00104 T right() const;
00105
00108 T bottom() const;
00109
00115 bool operator==(const RectType<T>& rect ) const;
00116
00122 bool contains( const PointType2D<T>& point ) const;
00123
00131 bool intersects( const RectType<T>& rect ) const;
00132
00139 bool intersectInplace( const RectType<T>& rect );
00140
00141 };
00142
00148 template<typename T>
00149 std::ostream& operator<<(std::ostream& os, const RectType<T>& r) {
00150 return
00151 os << "("<<r.x<<","<<r.y<<")-("<<r.w<<","<<r.h<<")";
00152 }
00153
00155
00156 template<typename T>
00157 inline T RectType<T>::right() const {
00158 return x + w;
00159 }
00160
00161 template<typename T>
00162 inline T RectType<T>::bottom() const {
00163 return y + h;
00164 }
00165
00166 template<typename T>
00167 inline bool RectType<T>::operator==(const RectType<T>& rect ) const {
00168 return
00169 x == rect.x && y == rect.y && w == rect.w && h == rect.h;
00170 }
00171
00172 template<typename T>
00173 inline bool RectType<T>::contains( const PointType2D<T>& point ) const {
00174 return
00175 (((point.x >= x) && (point.x <= x + w))
00176 && ((point.y >= y) && (point.y <= y + h)));
00177 }
00178
00179
00180 template<typename T>
00181 inline bool RectType<T>::intersectInplace( const RectType<T>& rectangle ) {
00182 x = x - rectangle.x;
00183 y = y - rectangle.y;
00184
00185
00186 if (x < 0) {
00187 w += x;
00188 x = 0;
00189 }
00190
00191 if (y < 0) {
00192 h += y;
00193 y = 0;
00194 }
00195
00196 if (x + w > rectangle.w) {
00197 w = rectangle.w - x;
00198 }
00199
00200 if (y + h > rectangle.h) {
00201 h = rectangle.h - y;
00202 }
00203
00204 x += rectangle.x;
00205 y += rectangle.y;
00206
00207 if (w <= 0 || h <= 0) {
00208 h = 0;
00209 w = 0;
00210 return false;
00211 }
00212 return true;
00213 }
00214
00215 template<typename T>
00216 inline bool RectType<T>::intersects( const RectType<T>& rectangle ) const {
00217 T _x = x - rectangle.x;
00218 T _y = y - rectangle.y;
00219 T _w = w;
00220 T _h = h;
00221
00222
00223 if (_x < 0) {
00224 _w += _x;
00225 _x = 0;
00226 }
00227
00228 if (_y < 0) {
00229 _h += _y;
00230 _y = 0;
00231 }
00232
00233 if (_x + _w > rectangle.w) {
00234 _w = rectangle.w - _x;
00235 }
00236
00237 if (_y + _h > rectangle.h) {
00238 _h = rectangle.h - _y;
00239 }
00240
00241 if (_w <= 0 || _h <= 0) {
00242 return false;
00243 }
00244 return true;
00245 }
00246
00247 typedef RectType<int> Rect;
00248 typedef RectType<float> FloatRect;
00249 typedef RectType<double> DoubleRect;
00250
00251
00252 }
00253
00254 #endif