00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef _RECTANGLE_
00019 #define _RECTANGLE_
00020
00021 #include <iostream>
00022
00023 class Rect
00024 {
00025 public:
00026 float _x;
00027 float _y;
00028 float _w;
00029 float _h;
00030
00031 Rect()
00032 :_x(0), _y(0), _w(0), _h(0)
00033 {
00034 }
00035
00036 Rect(const float x, const float y, const float w, const float h)
00037 :_x(x), _y(y), _w(w), _h(h)
00038 {
00039 }
00040
00041 Rect(const Rect& r)
00042 :_x(r._x), _y(r._y), _w(r._w), _h(r._h)
00043 {
00044 }
00045
00046 Rect & SetXY(const float x, const float y)
00047 {
00048 _x = x;
00049 _y = y;
00050
00051 return *this;
00052 }
00053
00054 Rect & SetWH(const float w, const float h)
00055 {
00056 _w = w;
00057 _h = h;
00058
00059 return *this;
00060 }
00061
00062 Rect & operator=(const Rect& r)
00063 {
00064 _x = r._x;
00065 _y = r._y;
00066 _w = r._w;
00067 _h = r._h;
00068
00069 return *this;
00070 }
00071
00072 Rect & Clip(const Rect& r)
00073 {
00074 if(_x < r._x)
00075 _x = r._x;
00076
00077 if(_y < r._y)
00078 _y = r._y;
00079
00080 if(Right() > r.Right())
00081 _w = r.Right() - _x;
00082
00083 if(Bottom() > r.Bottom())
00084 _h = r.Bottom() - _y;
00085
00086 return *this;
00087 }
00088
00089
00090 Rect GetClipped(const Rect& r) const
00091 {
00092 Rect temp(*this);
00093
00094 return temp.Clip(r);
00095 }
00096
00097 bool Intersects(const Rect& r) const
00098 {
00099 if(Right() < r.Left() || Left() > r.Right() || Top() > r.Bottom() || Bottom() < r.Top())
00100 return true;
00101
00102 return false;
00103 }
00104
00105 bool PointInRect(const float x, const float y) const
00106 {
00107 if(x < Left() || x > Right() || y < Top() || y > Bottom())
00108 return false;
00109
00110 return true;
00111 }
00112
00113 float Left() const
00114 {
00115 return _x;
00116 }
00117
00118 float Top() const
00119 {
00120 return _y;
00121 }
00122
00123 float Right() const
00124 {
00125 return _x + _w;
00126 }
00127
00128 float Bottom() const
00129 {
00130 return _y + _h;
00131 }
00132
00133 };
00134
00135 inline std::ostream & operator<<(std::ostream & os, const Rect & rect)
00136 {
00137 os << rect._x << " " << rect._y << " " << rect._w << " " << rect._h << " ";
00138
00139 return os;
00140 }
00141
00142 inline std::istream & operator>>(std::istream & is, Rect & rect)
00143 {
00144 is >> rect._x >> rect._y >> rect._w >> rect._h;
00145
00146 return is;
00147 }
00148
00149 #endif