| 10 | namespace dlib |
| 11 | { |
| 12 | class polygon |
| 13 | { |
| 14 | public: |
| 15 | using size_type = std::vector<point>::size_type; |
| 16 | |
| 17 | polygon(std::vector<point> points) : points(std::move(points)) {} |
| 18 | |
| 19 | size_type size() const { return points.size(); } |
| 20 | |
| 21 | point& operator[](const size_type idx) { return points[idx]; } |
| 22 | const point& operator[](const size_type idx) const { return points[idx]; } |
| 23 | const point& at(const size_type idx) const { return points.at(idx); } |
| 24 | |
| 25 | std::vector<point>::iterator begin() { return points.begin(); } |
| 26 | std::vector<point>::iterator end() { return points.end(); } |
| 27 | const std::vector<point>::const_iterator begin() const { return points.begin(); } |
| 28 | const std::vector<point>::const_iterator end() const { return points.end(); } |
| 29 | |
| 30 | rectangle get_rect() const |
| 31 | { |
| 32 | rectangle rect; |
| 33 | for (const auto& p : points) |
| 34 | rect += p; |
| 35 | return rect; |
| 36 | } |
| 37 | |
| 38 | double area() const { return polygon_area(points); } |
| 39 | |
| 40 | template <typename alloc> |
| 41 | void get_left_and_right_bounds ( |
| 42 | const long top, |
| 43 | const long bottom, |
| 44 | std::vector<double, alloc>& left_boundary, |
| 45 | std::vector<double, alloc>& right_boundary |
| 46 | ) const |
| 47 | { |
| 48 | using std::min; |
| 49 | using std::max; |
| 50 | |
| 51 | left_boundary.assign(bottom-top+1, std::numeric_limits<double>::infinity()); |
| 52 | right_boundary.assign(bottom-top+1, -std::numeric_limits<double>::infinity()); |
| 53 | |
| 54 | // trace out the points along the edge of the polynomial and record them |
| 55 | for (unsigned long i = 0; i < points.size(); ++i) |
| 56 | { |
| 57 | const point p1 = points[i]; |
| 58 | const point p2 = points[(i+1)%points.size()]; |
| 59 | |
| 60 | if (p1.y() == p2.y()) |
| 61 | { |
| 62 | if (top <= p1.y() && p1.y() <= bottom) |
| 63 | { |
| 64 | const long y = p1.y() - top; |
| 65 | const double xmin = min(p1.x(), p2.x()); |
| 66 | const double xmax = min(p1.x(), p2.x()); |
| 67 | left_boundary[y] = min(left_boundary[y], xmin); |
| 68 | right_boundary[y] = max(right_boundary[y], xmax); |
| 69 | } |