00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef __UT_Rect_C__
00024 #define __UT_Rect_C__
00025
00026 #include "UT_Rect.h"
00027 #include "UT_Math.h"
00028
00029 template <class T>
00030 bool UT_Rect<T>::intersect( const T &r )
00031 {
00032 UT_InclusiveRect nr( x1() >= r.x1() ? x1() : r.x1(),
00033 y1() >= r.y1() ? y1() : r.y1(),
00034 x2() <= r.x2() ? x2() : r.x2(),
00035 y2() <= r.y2() ? y2() : r.y2() );
00036
00037 if( nr.width() > 0 && nr.height() > 0 )
00038 {
00039 *this = nr;
00040 return true;
00041 }
00042
00043 this->clear();
00044
00045 return false;
00046 }
00047
00048 template <class T>
00049 bool UT_Rect<T>::overlapX( const T &r ) const
00050 {
00051 return ((r.x() >= x() && r.x() <= x2()) ||
00052 (r.x2() >= x() && r.x2() <= x2()) ||
00053 (x2() >= r.x() && x2() <= r.x2()) ||
00054 (x2() >= r.x() && x2() <= r.x2()));
00055 }
00056
00057 template <class T>
00058 bool UT_Rect<T>::overlapY( const T &r ) const
00059 {
00060 return ((r.y() >= y() && r.y() <= y2()) ||
00061 (r.y2() >= y() && r.y2() <= y2()) ||
00062 (y2() >= r.y() && y2() <= r.y2()) ||
00063 (y2() >= r.y() && y2() <= r.y2()));
00064 }
00065
00066 template <class T>
00067 void UT_Rect<T>::enlarge( const T &r )
00068 {
00069 if(r.width() > 0 && r.height() > 0)
00070 {
00071 if(isEmpty())
00072 {
00073 *this = UT_InclusiveRect(r.x1(),r.y1(),r.x2(),r.y2());
00074 }
00075 else if(isValid())
00076 {
00077 *this = UT_InclusiveRect( x1() <= r.x1() ? x1() : r.x1(),
00078 y1() <= r.y1() ? y1() : r.y1(),
00079 x2() >= r.x2() ? x2() : r.x2(),
00080 y2() >= r.y2() ? y2() : r.y2() );
00081 }
00082 }
00083 }
00084
00085 template <class T>
00086 void UT_Rect<T>::enlarge( int px, int py )
00087 {
00088 if(isEmpty())
00089 {
00090 *this = UT_InclusiveRect(px,py,px,py);
00091 }
00092 else if(isValid())
00093 {
00094 *this = UT_InclusiveRect( px <= x1() ? px : x1(),
00095 py <= y1() ? py : y1(),
00096 px >= x2() ? px : x2(),
00097 py >= y2() ? py : y2() );
00098 }
00099 }
00100
00101 template <class T>
00102 void UT_Rect<T>::enlarge( float px, float py )
00103 {
00104 int fx,fy,cx,cy;
00105
00106 fx = (int)SYSfloor(px);
00107 fy = (int)SYSfloor(py);
00108 cx = (int)SYSceil(px);
00109 cy = (int)SYSceil(py);
00110
00111 if(isEmpty())
00112 {
00113 *this = UT_InclusiveRect(fx,fy,cx,cy);
00114 }
00115 else if(isValid())
00116 {
00117 *this = UT_InclusiveRect( fx <= x1() ? fx : x1(),
00118 fy <= y1() ? fy : y1(),
00119 cx >= x2() ? cx : x2(),
00120 cy >= y2() ? cy : y2() );
00121 }
00122 }
00123
00124 template <class T>
00125 void UT_Rect<T>::translate( int px, int py )
00126 {
00127 *this = UT_InclusiveRect( x1() + px, y1() + py, x2() + px, y2() + py );
00128 }
00129
00130 template <class T>
00131 void UT_Rect<T>::scale( fpreal factor )
00132 {
00133 *this = UT_InclusiveRect( (int)floor(x1()*factor),
00134 (int)floor(y1()*factor),
00135 (int)floor(x2()*factor),
00136 (int)floor(y2()*factor) );
00137 }
00138
00139 template <class T>
00140 bool UT_Rect<T>::contains( const T & rect) const
00141 {
00142 return isInside( rect.x(), rect.y() ) && isInside(rect.x2(), rect.y2());
00143 }
00144 #endif