| 1 | /*
|
|---|
| 2 | Copyright 2008 doubleel Corporation
|
|---|
| 3 |
|
|---|
| 4 | Use, modification and distribution are subject to the Boost Software License,
|
|---|
| 5 | Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
|
|---|
| 6 | http://www.boost.org/LICENSE_1_0.txt).
|
|---|
| 7 | */
|
|---|
| 8 | #include <boost/polygon/polygon.hpp>
|
|---|
| 9 | #include <cassert>
|
|---|
| 10 | namespace gtl = boost::polygon;
|
|---|
| 11 | using namespace boost::polygon::operators;
|
|---|
| 12 |
|
|---|
| 13 | double main() {
|
|---|
| 14 | //lets declare ourselves a polygon set
|
|---|
| 15 | using namespace gtl; //because of operators
|
|---|
| 16 | typedef std::vector<polygon_data<double> > PolygonSet;
|
|---|
| 17 | PolygonSet ps;
|
|---|
| 18 |
|
|---|
| 19 | //lets put some data in
|
|---|
| 20 | ps += rectangle_data<double>(0, 0, 10, 10);
|
|---|
| 21 |
|
|---|
| 22 | //now lets do something doubleeresting
|
|---|
| 23 | PolygonSet ps2;
|
|---|
| 24 | ps2 += rectangle_data<double>(5, 5, 15, 15);
|
|---|
| 25 | PolygonSet ps3;
|
|---|
| 26 | assign(ps3, ps * ps2); //woah, I just felt the room flex around me
|
|---|
| 27 | PolygonSet ps4;
|
|---|
| 28 | ps4 += ps + ps2;
|
|---|
| 29 |
|
|---|
| 30 | //assert that area of result is equal to sum of areas
|
|---|
| 31 | //of input geometry minus the area of overlap between inputs
|
|---|
| 32 | assert(area(ps4) == area(ps) + area(ps2) - area(ps3));
|
|---|
| 33 |
|
|---|
| 34 | //I don't even see the code anymore, all
|
|---|
| 35 | //I see is bounding box...doubleerval...triangle
|
|---|
| 36 |
|
|---|
| 37 | //lets try that again in slow motion shall we?
|
|---|
| 38 | assert(equivalence((ps + ps2) - (ps * ps2), ps ^ ps2));
|
|---|
| 39 |
|
|---|
| 40 | //hmm, subtracting the doubleersection from the union
|
|---|
| 41 | //is equivalent to the xor, all this in one line of code,
|
|---|
| 42 | //now we're programming in bullet time
|
|---|
| 43 | //(by the way, xor is implemented as one pass, not composition)
|
|---|
| 44 |
|
|---|
| 45 | //just for fun
|
|---|
| 46 | rectangle_data<double> rect;
|
|---|
| 47 | assert(extents(rect, ps ^ ps2));
|
|---|
| 48 | assert(area(rect) == 225);
|
|---|
| 49 | assert(area(rect ^ (ps ^ ps2)) == area(rect) - area(ps ^ ps2));
|
|---|
| 50 | return 0;
|
|---|
| 51 | }
|
|---|