00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef RECT_HH_
00018 # define RECT_HH_
00019
00020 # include "misc/Point.hh"
00021
00028 class Rect
00029 {
00030 public:
00031 Rect();
00032 Rect(int x, int y, int w, int h);
00033 Rect(const Point& pos, const Point& size);
00034
00035 bool inside(const Point& pos) const;
00036 Point getPos() const;
00037 Point getSize() const;
00038
00039 friend bool operator== (const Rect& lhs, const Rect& rhs);
00040 friend bool operator!= (const Rect& lhs, const Rect& rhs);
00041 Rect& operator+= (const Rect& rhs);
00042 friend Rect operator+ (const Rect& lhs, const Rect& rhs);
00043 Rect& operator+= (const Point& rhs);
00044 friend Rect operator+ (const Rect& lhs, const Point& rhs);
00045 Rect& operator-= (const Point& rhs);
00046 friend Rect operator- (const Rect& lhs, const Point& rhs);
00047 friend Rect operator* (const Rect& lhs, double factor);
00048 Rect& operator|= (const Rect& rhs);
00049 Rect& operator&= (const Rect& rhs);
00050 friend Rect operator| (const Rect& lhs, const Rect& rhs);
00051 friend Rect operator& (const Rect& lhs, const Rect& rhs);
00052 friend std::ostream& operator<< (std::ostream& os, const Rect& p);
00053
00054 public:
00055 int x;
00056 int y;
00057 int w;
00058 int h;
00059 };
00060
00061 inline Rect::Rect()
00062 : x(0),
00063 y(0),
00064 w(0),
00065 h(0)
00066 {
00067 }
00068
00069 inline Rect::Rect(int x, int y, int w, int h)
00070 : x(x),
00071 y(y),
00072 w(w),
00073 h(h)
00074 {
00075 }
00076
00077 inline Rect::Rect(const Point& pos, const Point& size)
00078 : x(pos.x),
00079 y(pos.y),
00080 w(size.x),
00081 h(size.y)
00082 {
00083 }
00084
00085
00086 inline bool Rect::inside(const Point& pos) const
00087 {
00088 return pos.x >= x && pos.x <= x + w && pos.y >= y && pos.y <= y + h;
00089 }
00090
00091 inline Point Rect::getPos() const
00092 {
00093 return Point(x, y);
00094 }
00095
00096 inline Point Rect::getSize() const
00097 {
00098 return Point(w, h);
00099 }
00100
00101 inline bool operator== (const Rect& lhs, const Rect& rhs)
00102 {
00103 return (lhs.x == rhs.x && lhs.y == rhs.y
00104 && lhs.w == rhs.w && lhs.h == rhs.h);
00105 }
00106
00107 inline bool operator!= (const Rect& lhs, const Rect& rhs)
00108 {
00109 return !(lhs.x == rhs.x && lhs.y == rhs.y
00110 && lhs.w == rhs.w && lhs.h == rhs.h);
00111
00112 }
00113
00114 inline Rect& Rect::operator+= (const Rect& rhs)
00115 {
00116 x += rhs.x;
00117 y += rhs.y;
00118 w += rhs.w;
00119 h += rhs.h;
00120 return *this;
00121 }
00122
00123 inline Rect operator+ (const Rect& lhs, const Rect& rhs)
00124 {
00125 return Rect(lhs.x + rhs.x, lhs.y + rhs.y, lhs.w + rhs.w, lhs.h + rhs.w);
00126 }
00127
00128 inline Rect& Rect::operator+= (const Point& rhs)
00129 {
00130 x += rhs.x;
00131 y += rhs.y;
00132 return *this;
00133 }
00134
00135 inline Rect operator+ (const Rect& lhs, const Point& rhs)
00136 {
00137 return Rect(lhs.x + rhs.x, lhs.y + rhs.y, lhs.w, lhs.h);
00138 }
00139
00140 inline Rect& Rect::operator-= (const Point& rhs)
00141 {
00142 x -= rhs.x;
00143 y -= rhs.y;
00144 return *this;
00145 }
00146
00147 inline Rect operator- (const Rect& lhs, const Point& rhs)
00148 {
00149 return Rect(lhs.x - rhs.x, lhs.y - rhs.y, lhs.w, lhs.h);
00150 }
00151
00152 inline Rect& Rect::operator|= (const Rect& rhs)
00153 {
00154 int x2 = std::max(x + w, rhs.x + rhs.w);
00155 int y2 = std::max(y + h, rhs.y + rhs.h);
00156 x = std::min(x, rhs.x);
00157 y = std::min(y, rhs.y);
00158 w = x2 - x;
00159 h = y2 - y;
00160 return *this;
00161 }
00162
00163 inline Rect operator* (const Rect& lhs, double factor)
00164 {
00165 return Rect((int)(lhs.x * factor), (int)(lhs.y * factor),
00166 (int)(lhs.w * factor), (int)(lhs.h * factor));
00167 }
00168
00169 inline Rect& Rect::operator&= (const Rect& rhs)
00170 {
00171 int x2 = std::min(x + w, rhs.x + rhs.w);
00172 int y2 = std::min(y + h, rhs.y + rhs.h);
00173 x = std::max(x, rhs.x);
00174 y = std::max(y, rhs.y);
00175 w = std::max(x2 - x, 0);
00176 h = std::max(y2 - y, 0);
00177 return *this;
00178 }
00179
00180 inline Rect operator| (const Rect& lhs, const Rect& rhs)
00181 {
00182 return Rect(lhs) |= rhs;
00183 }
00184
00185 inline Rect operator& (const Rect& lhs, const Rect& rhs)
00186 {
00187 return Rect(lhs) &= rhs;
00188 }
00189
00190 inline std::ostream& operator<< (std::ostream& os, const Rect& p)
00191 {
00192 os << "[x:" << p.x << ", y:" << p.y << ", w:" << p.w << ", h:" << p.h << "]";
00193 return os;
00194 }
00195
00196 #endif