00001 #ifndef Impala_Core_Geometry_Rectangle_h
00002 #define Impala_Core_Geometry_Rectangle_h
00003
00004 #include <iostream>
00005
00006 namespace Impala
00007 {
00008 namespace Core
00009 {
00010 namespace Geometry
00011 {
00012
00013
00014 class Rectangle
00015 {
00016 public:
00017 int mLeft;
00018 int mTop;
00019 int mRight;
00020 int mBottom;
00021
00022 Rectangle()
00023 {
00024 mLeft = 0;
00025 mTop = 0;
00026 mRight = 0;
00027 mBottom = 0;
00028 }
00029
00030 Rectangle(int left, int top, int right, int bottom)
00031 {
00032 mLeft = left;
00033 mTop = top;
00034 mRight = right;
00035 mBottom = bottom;
00036 }
00037
00038 Rectangle(int left, int top, int right, int bottom, bool check)
00039 {
00040 mLeft = left;
00041 mTop = top;
00042 mRight = right;
00043 mBottom = bottom;
00044 if (check)
00045 {
00046 if(left > right)
00047 {
00048 mLeft = right;
00049 mRight = left;
00050 }
00051 if(top > bottom)
00052 {
00053 mTop = bottom;
00054 mBottom = top;
00055 }
00056 }
00057 }
00058
00059 Rectangle(const Rectangle& r)
00060 {
00061 mLeft = r.mLeft;
00062 mTop = r.mTop;
00063 mRight = r.mRight;
00064 mBottom = r.mBottom;
00065 }
00066
00067 int Width() const
00068 {
00069 return mRight - mLeft + 1;
00070 }
00071
00072 int Height() const
00073 {
00074 return mBottom - mTop + 1;
00075 }
00076
00077 int Size() const
00078 {
00079 return Width() * Height();
00080 }
00081
00082 bool Valid() const
00083 {
00084 return Size()>0;
00085 }
00086
00087 bool IsInside(int x, int y) const
00088 {
00089 if(x < mRight &&
00090 x > mLeft &&
00091 y > mTop &&
00092 y < mBottom)
00093 return true;
00094 return false;
00095 }
00096
00097
00098 bool Intersects(const Rectangle& r) const
00099 {
00100
00101
00102 return ! (r.mLeft > mRight ||
00103 r.mRight < mLeft ||
00104 r.mTop > mBottom ||
00105 r.mBottom < mTop );
00106 }
00107
00108
00109
00110 void CheckSize()
00111 {
00112 }
00113
00114 bool operator==(const Rectangle& r) const
00115 {
00116 return ((mLeft == r.mLeft) && (mTop == r.mTop) &&
00117 (mRight == r.mRight) && (mBottom == r.mBottom));
00118 }
00119
00120 };
00121
00122 Rectangle GrowBorder(const Rectangle& r, int growSize)
00123 {
00124 return Rectangle(r.mLeft - growSize, r.mTop - growSize,
00125 r.mRight + growSize, r.mBottom + growSize);
00126 }
00127
00128 std::ostream&
00129 operator<< (std::ostream& os, const Rectangle& r)
00130 {
00131 os << r.mLeft << " "
00132 << r.mTop << " "
00133 << r.mRight << " "
00134 << r.mBottom;
00135 return os;
00136 }
00137
00138 std::istream&
00139 operator>> (std::istream& is, Rectangle& r)
00140 {
00141 is >> r.mLeft >> r.mTop >> r.mRight >> r.mBottom;
00142 return is;
00143 }
00144
00145 Rectangle
00146 operator/ (Rectangle& r, double d)
00147 {
00148 return Rectangle((int)(r.mLeft/d), (int)(r.mTop/d), (int)(r.mRight/d),
00149 (int)(r.mBottom/d));
00150 }
00151
00152 Rectangle
00153 operator* (Rectangle& r, double d)
00154 {
00155 return Rectangle((int)(r.mLeft*d), (int)(r.mTop*d), (int)(r.mRight*d),
00156 (int)(r.mBottom*d));
00157 }
00158
00159 }
00160 }
00161 }
00162
00163 #endif