1 | /*
|
---|
2 | Copyright 2008 Intel 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 | int main() {
|
---|
14 | //lets declare ourselves a polygon set
|
---|
15 | using namespace gtl; //because of operators
|
---|
16 | typedef std::vector<polygon_data<int> > PolygonSet;
|
---|
17 | PolygonSet ps;
|
---|
18 |
|
---|
19 | //lets put some data in
|
---|
20 | ps += rectangle_data<int>(0, 0, 10, 10);
|
---|
21 |
|
---|
22 | //now lets do something interesting
|
---|
23 | PolygonSet ps2;
|
---|
24 | ps2 += rectangle_data<int>(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...interval...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 intersection 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<int> 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 | }
|
---|
52 |
|
---|
53 | //Now you know how to use the polygon set concept with library polygons
|
---|