Go to the documentation of this file.00001 #include "wit.h"
00002 #include "geometry.h"
00003 #include <vector>
00004
00005 namespace wit {
00006
00028 Point::Point() : x(0), y(0)
00029 {
00030
00031 }
00032
00036 Point::Point(const Point& other) : x(other.x), y(other.y)
00037 {
00038
00039 }
00040
00044 Point::Point(float x, float y) : x(x), y(y)
00045 {
00046
00047 }
00048
00052 Point& Point::operator=(const Point& other)
00053 {
00054 x = other.x;
00055 y = other.y;
00056 return *this;
00057 }
00058
00092 Rect::Rect() : x1(0), y1(0), x2(0), y2(0)
00093 {
00094
00095 }
00096
00100 Rect::Rect(const Rect& other) : x1(other.x1), y1(other.y1), x2(other.x2), y2(other.y2)
00101 {
00102
00103 }
00104
00108 Rect::Rect(float x1, float y1, float x2, float y2) : x1(x1), y1(y1), x2(x2), y2(y2)
00109 {
00110
00111 }
00112
00116 Rect& Rect::operator=(const Rect& other)
00117 {
00118 x1 = other.x1;
00119 y1 = other.y1;
00120 x2 = other.x2;
00121 y2 = other.y2;
00122 return *this;
00123 }
00124
00130 bool Rect::contains(float x, float y)
00131 {
00132 return (x1 <= x && x2 >= x) && (y1 <= y && y2 >= y);
00133 }
00134
00140 bool Rect::intersects(const Rect& other)
00141 {
00142 return (x2 >= other.x1 && x1 <= other.x2) && (y2 >= other.y1 && y1 <= other.y2);
00143 }
00144
00150 float Rect::width() const
00151 {
00152 return x2 - x1;
00153 }
00154
00160 float Rect::height() const
00161 {
00162 return y2 - y1;
00163 }
00164
00171 struct PolygonPrivate {
00172 std::vector<Point> points;
00173 Rect bb;
00174 };
00175
00179 Polygon::Polygon() : d(new PolygonPrivate)
00180 {
00181
00182 }
00183
00187 Polygon::Polygon(const Polygon& other) : d(new PolygonPrivate)
00188 {
00189 this->operator=(other);
00190 }
00191
00195 Polygon::~Polygon()
00196 {
00197 delete d;
00198 }
00199
00203 Polygon& Polygon::operator=(const Polygon& other)
00204 {
00205 d->points = other.d->points;
00206 d->bb = other.d->bb;
00207 return *this;
00208 }
00209
00213 void Polygon::addPoint(const Point& p)
00214 {
00215 d->points.push_back(p);
00216 if(d->points.size() == 1) {
00217 d->bb.x1 = d->bb.x2 = p.x;
00218 d->bb.y1 = d->bb.y2 = p.y;
00219 } else {
00220 if(d->bb.x1 > p.x) d->bb.x1 = p.x;
00221 if(d->bb.x2 < p.x) d->bb.x2 = p.x;
00222 if(d->bb.y1 > p.y) d->bb.y1 = p.y;
00223 if(d->bb.y2 < p.y) d->bb.y2 = p.y;
00224 }
00225 }
00226
00232 void Polygon::addPoint(float x, float y)
00233 {
00234 addPoint(Point(x, y));
00235 }
00236
00240 int Polygon::count() const
00241 {
00242 return d->points.size();
00243 }
00244
00250 Point* Polygon::points() const
00251 {
00252 return &(d->points.front());
00253 }
00254
00261 Rect Polygon::boundingBox() const
00262 {
00263 return d->bb;
00264 }
00265
00273 bool Polygon::contains(float x, float y) const
00274 {
00275 bool inside = false;
00276 Point* p = points();
00277
00278 for(int i = count() - 2; i >= -1; --i) {
00279 Point p1 = p[i+1];
00280 Point p2 = (i == -1 ? p[count() - 1] : p[i]);
00281
00282 if((p2.x < x) != (x <= p1.x))
00283 continue;
00284
00285 if(p1.x < p2.x) {
00286 p2 = p1;
00287 p1 = (i == -1 ? p[count() - 1] : p[i]);
00288 }
00289
00290 if((y - p1.y) * (p2.x - p1.x) < (p2.y - p1.y) * (x - p1.x))
00291 inside = !inside;
00292 }
00293 return inside;
00294 }
00295
00296 }