00001
00002
00003 #ifdef HAVE_CONFIG_H
00004 # include "../config.h"
00005 #endif
00006
00007 #include "rect.hh"
00008
00009 namespace otk {
00010
00011 void Rect::setX(int x)
00012 {
00013 _x2 += x - _x1;
00014 _x1 = x;
00015 }
00016
00017
00018 void Rect::setY(int y)
00019 {
00020 _y2 += y - _y1;
00021 _y1 = y;
00022 }
00023
00024
00025 void Rect::setPos(const Point &location)
00026 {
00027 _x2 += location.x() - _x1;
00028 _x1 = location.x();
00029 _y2 += location.y() - _y1;
00030 _y1 = location.y();
00031 }
00032
00033
00034 void Rect::setPos(int x, int y)
00035 {
00036 _x2 += x - _x1;
00037 _x1 = x;
00038 _y2 += y - _y1;
00039 _y1 = y;
00040 }
00041
00042
00043 void Rect::setWidth(int w)
00044 {
00045 _x2 = w + _x1 - 1;
00046 }
00047
00048
00049 void Rect::setHeight(int h)
00050 {
00051 _y2 = h + _y1 - 1;
00052 }
00053
00054
00055 void Rect::setSize(int w, int h)
00056 {
00057 _x2 = w + _x1 - 1;
00058 _y2 = h + _y1 - 1;
00059 }
00060
00061
00062 void Rect::setSize(const Point &size)
00063 {
00064 _x2 = size.x() + _x1 - 1;
00065 _y2 = size.y() + _y1 - 1;
00066 }
00067
00068
00069 void Rect::setRect(int x, int y, int w, int h)
00070 {
00071 *this = Rect(x, y, w, h);
00072 }
00073
00074
00075 void Rect::setRect(const Point &location, const Point &size)
00076 {
00077 *this = Rect(location, size);
00078 }
00079
00080
00081 void Rect::setCoords(int l, int t, int r, int b)
00082 {
00083 _x1 = l;
00084 _y1 = t;
00085 _x2 = r;
00086 _y2 = b;
00087 }
00088
00089
00090 void Rect::setCoords(const Point &tl, const Point &br)
00091 {
00092 _x1 = tl.x();
00093 _y1 = tl.y();
00094 _x2 = br.x();
00095 _y2 = br.y();
00096 }
00097
00098
00099 Rect Rect::operator|(const Rect &a) const
00100 {
00101 Rect b;
00102
00103 b._x1 = std::min(_x1, a._x1);
00104 b._y1 = std::min(_y1, a._y1);
00105 b._x2 = std::max(_x2, a._x2);
00106 b._y2 = std::max(_y2, a._y2);
00107
00108 return b;
00109 }
00110
00111
00112 Rect Rect::operator&(const Rect &a) const
00113 {
00114 Rect b;
00115
00116 b._x1 = std::max(_x1, a._x1);
00117 b._y1 = std::max(_y1, a._y1);
00118 b._x2 = std::min(_x2, a._x2);
00119 b._y2 = std::min(_y2, a._y2);
00120
00121 return b;
00122 }
00123
00124
00125 bool Rect::intersects(const Rect &a) const
00126 {
00127 return std::max(_x1, a._x1) <= std::min(_x2, a._x2) &&
00128 std::max(_y1, a._y1) <= std::min(_y2, a._y2);
00129 }
00130
00131
00132 bool Rect::contains(int x, int y) const
00133 {
00134 return x >= _x1 && x <= _x2 &&
00135 y >= _y1 && y <= _y2;
00136 }
00137
00138
00139 bool Rect::contains(const Point &p) const
00140 {
00141 return contains(p.x(), p.y());
00142 }
00143
00144
00145 bool Rect::contains(const Rect& a) const
00146 {
00147 return a._x1 >= _x1 && a._x2 <= _x2 &&
00148 a._y1 >= _y1 && a._y2 <= _y2;
00149 }
00150
00151 }