| 1 | #include <boost/polygon/polygon.hpp>
|
|---|
| 2 |
|
|---|
| 3 | #include <iostream>
|
|---|
| 4 | #include <cstdlib>
|
|---|
| 5 |
|
|---|
| 6 | namespace bp = boost::polygon;
|
|---|
| 7 | using namespace bp::operators;
|
|---|
| 8 |
|
|---|
| 9 | typedef bp::polygon_set_data<int> PolygonSet;
|
|---|
| 10 | typedef bp::polygon_data<PolygonSet::coordinate_type> Polygon;
|
|---|
| 11 | typedef bp::polygon_traits<Polygon>::point_type Point;
|
|---|
| 12 |
|
|---|
| 13 | int
|
|---|
| 14 | main(int argc, char *argv[])
|
|---|
| 15 | {
|
|---|
| 16 | // Small figure with a little hole. Area should be 32.
|
|---|
| 17 | const Point points[] = {
|
|---|
| 18 | Point(0,0),
|
|---|
| 19 | Point(6,0),
|
|---|
| 20 | Point(6,4),
|
|---|
| 21 | Point(4,6),
|
|---|
| 22 | Point(0,6),
|
|---|
| 23 | Point(0,0),
|
|---|
| 24 | Point(4,4),
|
|---|
| 25 | Point(5,4)
|
|---|
| 26 | };
|
|---|
| 27 |
|
|---|
| 28 | Polygon poly1(&points[0], &points[sizeof(points)/sizeof(points[0])]);
|
|---|
| 29 | PolygonSet ps1;
|
|---|
| 30 | ps1 += poly1;
|
|---|
| 31 |
|
|---|
| 32 | // Boost.Polygon says the area is 30. That is because its internal
|
|---|
| 33 | // representation is wrong, as you can confirm by manually
|
|---|
| 34 | // examining the result of get() or get_trapezoids().
|
|---|
| 35 | std::cout << "poly1 area == " << area(poly1) << "\n";
|
|---|
| 36 | std::cout << "ps1 area == " << area(ps1) << "\n";
|
|---|
| 37 |
|
|---|
| 38 | // Doubling the coordinates avoids the bug.
|
|---|
| 39 | Polygon poly2(poly1);
|
|---|
| 40 | scale_up(poly2, 2);
|
|---|
| 41 | PolygonSet ps2;
|
|---|
| 42 | ps2 += poly2;
|
|---|
| 43 | std::cout << "poly2 area == " << area(poly2) << "\n";
|
|---|
| 44 | std::cout << "ps2 area == " << area(ps2) << "\n";
|
|---|
| 45 |
|
|---|
| 46 | assert(area(poly2) == area(ps2));
|
|---|
| 47 | assert(area(poly1) == area(ps1));
|
|---|
| 48 | }
|
|---|