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
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069 #ifndef IO_MATH_INCLUDED
00070 #define IO_MATH_INCLUDED
00071
00072 #include "../util/glutil.h"
00073 #include "../util/gltypes.h"
00074 #include "../util/glfixed.h"
00075
00076
00077
00078 struct KrVector2
00079 {
00080 int x;
00081 int y;
00082
00083 void operator+=( const KrVector2& rhs ) { x += rhs.x; y += rhs.y; }
00084 void Set( int _x = 0, int _y = 0 ) { x = _x; y = _y; }
00085 };
00086
00087
00088 template <class T>
00089 struct KrVector2T
00090 {
00091 T x;
00092 T y;
00093
00094 void operator+=( const KrVector2T& rhs ) { x += rhs.x; y += rhs.y; }
00095 void Set( T _x, T _y ) { x = _x; y = _y; }
00096 };
00097
00098
00103 class KrMatrix2
00104 {
00105 public:
00106 GlFixed x, y;
00107 GlFixed xScale, yScale;
00108
00110 void Composite( const KrMatrix2& other )
00111 {
00112
00113
00114
00115 if ( other.xScale.v == GlFixed_1
00116 && other.yScale.v == GlFixed_1 )
00117 {
00118 x.v = other.x.v + x.v;
00119 y.v = other.y.v + y.v;
00120 }
00121 else
00122 {
00123 x = other.x + other.xScale * x;
00124 y = other.y + other.yScale * y;
00125 xScale = other.xScale * xScale;
00126 yScale = other.yScale * yScale;
00127 }
00128 }
00129
00130 void Set( GlFixed _x = 0, GlFixed _y = 0, GlFixed _xScale = 1, GlFixed _yScale = 1 )
00131 {
00132 x.v = _x.v;
00133 y.v = _y.v;
00134 xScale.v = _xScale.v;
00135 yScale.v = _yScale.v;
00136 }
00137
00138
00139 bool IsScaled() const { return ( xScale.v != GlFixed_1 ) || ( yScale.v != GlFixed_1 ); }
00140
00141 inline friend bool operator == (const KrMatrix2& a, const KrMatrix2& b) { return (a.x.v == b.x.v && a.y.v == b.y.v && a.xScale.v == b.xScale.v && a.yScale.v == b.yScale.v ); }
00142 inline friend bool operator != (const KrMatrix2& a, const KrMatrix2& b) { return (a.x.v != b.x.v || a.y.v != b.y.v || a.xScale.v != b.xScale.v || a.yScale.v != b.yScale.v ); }
00143 };
00144
00145
00148 struct KrRect
00149 {
00150 int xmin;
00151 int ymin;
00152 int xmax;
00153 int ymax;
00154
00155 int Width() const { return xmax - xmin + 1; }
00156 int Height() const { return ymax - ymin + 1; }
00157 int Area() const { return ( xmax - xmin + 1 ) * ( ymax - ymin + 1 ); }
00158
00160 void Set( int _xmin, int _ymin, int _xmax, int _ymax ) { xmin = _xmin; ymin = _ymin; xmax = _xmax; ymax = _ymax; }
00161
00163 void SetInvalid() { xmin = -1; xmax = -2; ymin = -1; ymax = -2; }
00164
00166 void Zero()
00167 {
00168 xmin = ymin = xmax = ymax = 0;
00169 }
00170
00172 bool IsValid() const
00173 {
00174 return ( xmin <= xmax ) && ( ymin <= ymax );
00175 }
00176
00180 void FromPair( int x0, int y0, int x1, int y1 );
00181
00183 bool Intersect( const KrRect& rect ) const
00184 {
00185 if ( rect.xmax < xmin
00186 || rect.xmin > xmax
00187 || rect.ymax < ymin
00188 || rect.ymin > ymax )
00189 {
00190 return false;
00191 }
00192 return true;
00193 }
00194
00195 bool Intersect( const KrVector2& point ) const
00196 {
00197 if ( point.x < xmin
00198 || point.x > xmax
00199 || point.y < ymin
00200 || point.y > ymax )
00201 {
00202 return false;
00203 }
00204 return true;
00205 }
00206
00207 bool Intersect( int x, int y ) const
00208 {
00209 if ( x < xmin
00210 || x > xmax
00211 || y < ymin
00212 || y > ymax )
00213 {
00214 return false;
00215 }
00216 return true;
00217 }
00218
00219
00221 bool HasInside( const KrRect& rect ) const
00222 {
00223 if ( rect.xmin >= xmin
00224 && rect.xmax <= xmax
00225 && rect.ymin >= ymin
00226 && rect.ymax <= ymax )
00227 {
00228 return true;
00229 }
00230 return false;
00231 }
00232
00234 bool HasInside( const KrVector2& point ) const
00235 {
00236 if ( point.x >= xmin
00237 && point.x <= xmax
00238 && point.y >= ymin
00239 && point.y <= ymax )
00240 {
00241 return true;
00242 }
00243 return false;
00244 }
00245
00246
00248 void DoUnion( const KrRect& rect )
00249 {
00250 xmin = GlMin( xmin, rect.xmin );
00251 xmax = GlMax( xmax, rect.xmax );
00252 ymin = GlMin( ymin, rect.ymin );
00253 ymax = GlMax( ymax, rect.ymax );
00254 }
00255
00257 void DoIntersection( const KrRect& rect )
00258 {
00259 xmin = GlMax( xmin, rect.xmin );
00260 xmax = GlMin( xmax, rect.xmax );
00261 ymin = GlMax( ymin, rect.ymin );
00262 ymax = GlMin( ymax, rect.ymax );
00263 }
00264
00266 void DoClip( const KrRect& rect )
00267 {
00268 xmin = rect.xmin > xmin ? rect.xmin : xmin;
00269 xmax = rect.xmax < xmax ? rect.xmax : xmax;
00270 ymin = rect.ymin > ymin ? rect.ymin : ymin;
00271 ymax = rect.ymax < ymax ? rect.ymax : ymax;
00272 }
00273
00274
00276 void Scale( GlFixed x, GlFixed y )
00277 {
00278 xmin = ( x * xmin ).ToInt();
00279 ymin = ( y * ymin ).ToInt();
00280 xmax = ( x * xmax ).ToInt();
00281 ymax = ( y * ymax ).ToInt();
00282 }
00283
00284
00285 void ScaleTranslate( const KrMatrix2& matrix )
00286 {
00287 Scale( matrix.xScale, matrix.yScale );
00288 xmin += matrix.x.ToInt();
00289 xmax += matrix.x.ToInt();
00290 ymin += matrix.y.ToInt();
00291 ymax += matrix.y.ToInt();
00292 }
00293
00295 void EdgeAdd( int i )
00296 {
00297 xmin -= i;
00298 xmax += i;
00299 ymin -= i;
00300 ymax += i;
00301 }
00302
00303 bool operator==( const KrRect& that ) const { return ( xmin == that.xmin )
00304 && ( xmax == that.xmax )
00305 && ( ymin == that.ymin )
00306 && ( ymax == that.ymax ); }
00307 bool operator!=( const KrRect& that ) const { return ( xmin != that.xmin )
00308 || ( xmax != that.xmax )
00309 || ( ymin != that.ymin )
00310 || ( ymax != that.ymax ); }
00311 };
00312
00313
00314
00315
00316 template <class T>
00317 struct KrTRect
00318 {
00319 T xmin;
00320 T ymin;
00321 T xmax;
00322 T ymax;
00323
00324 T Width() const { return xmax - xmin + 1; }
00325 T Height() const { return ymax - ymin + 1; }
00326 T Area() const { return ( xmax - xmin + 1 ) * ( ymax - ymin + 1 ); }
00327
00329 void Set( T _xmin, T _ymin, T _xmax, T _ymax ) { xmin = _xmin; ymin = _ymin; xmax = _xmax; ymax = _ymax; }
00330
00332 bool IsValid() const
00333 {
00334 return ( xmin <= xmax ) && ( ymin <= ymax );
00335 }
00336
00338 bool Intersect( const KrTRect<T>& rect ) const
00339 {
00340 if ( rect.xmax < xmin
00341 || rect.xmin > xmax
00342 || rect.ymax < ymin
00343 || rect.ymin > ymax )
00344 {
00345 return false;
00346 }
00347 return true;
00348 }
00349
00350 bool Intersect( T x, T y ) const
00351 {
00352 if ( x < xmin
00353 || x > xmax
00354 || y < ymin
00355 || y > ymax )
00356 {
00357 return false;
00358 }
00359 return true;
00360 }
00361
00362
00364 bool HasInside( const KrTRect<T>& rect ) const
00365 {
00366 if ( rect.xmin >= xmin
00367 && rect.xmax <= xmax
00368 && rect.ymin >= ymin
00369 && rect.ymax <= ymax )
00370 {
00371 return true;
00372 }
00373 return false;
00374 }
00375
00377 void DoUnion( const KrTRect<T>& rect )
00378 {
00379 xmin = GlMin( xmin, rect.xmin );
00380 xmax = GlMax( xmax, rect.xmax );
00381 ymin = GlMin( ymin, rect.ymin );
00382 ymax = GlMax( ymax, rect.ymax );
00383 }
00384
00386 void DoIntersection( const KrTRect<T>& rect )
00387 {
00388 xmin = GlMax( xmin, rect.xmin );
00389 xmax = GlMin( xmax, rect.xmax );
00390 ymin = GlMax( ymin, rect.ymin );
00391 ymax = GlMin( ymax, rect.ymax );
00392 }
00393 };
00394
00395
00396 #endif