]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // Boost.Geometry (aka GGL, Generic Geometry Library) |
2 | // QuickBook Example | |
3 | ||
4 | // Copyright (c) 2011-2012 Barend Gehrels, Amsterdam, the Netherlands. | |
5 | ||
6 | // Use, modification and distribution is subject to the Boost Software License, | |
7 | // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at | |
8 | // http://www.boost.org/LICENSE_1_0.txt) | |
9 | ||
10 | //[for_each_point | |
11 | //` Convenient usage of for_each_point, rounding all points of a geometry | |
12 | ||
13 | #include <iostream> | |
14 | ||
15 | #include <boost/geometry.hpp> | |
16 | #include <boost/geometry/geometries/point_xy.hpp> | |
17 | #include <boost/geometry/geometries/polygon.hpp> | |
18 | ||
19 | ||
20 | ||
21 | template <typename Point> | |
22 | class round_coordinates | |
23 | { | |
24 | private : | |
25 | typedef typename boost::geometry::coordinate_type<Point>::type coordinate_type; | |
26 | coordinate_type factor; | |
27 | ||
28 | inline coordinate_type round(coordinate_type value) | |
29 | { | |
30 | return floor(0.5 + (value / factor)) * factor; | |
31 | } | |
32 | ||
33 | public : | |
34 | round_coordinates(coordinate_type f) | |
35 | : factor(f) | |
36 | {} | |
37 | ||
38 | inline void operator()(Point& p) | |
39 | { | |
40 | using boost::geometry::get; | |
41 | using boost::geometry::set; | |
42 | set<0>(p, round(get<0>(p))); | |
43 | set<1>(p, round(get<1>(p))); | |
44 | } | |
45 | }; | |
46 | ||
47 | ||
48 | int main() | |
49 | { | |
50 | typedef boost::geometry::model::d2::point_xy<double> point; | |
51 | boost::geometry::model::polygon<point> poly; | |
52 | boost::geometry::read_wkt("POLYGON((0 0,1.123 9.987,8.876 2.234,0 0),(3.345 4.456,7.654 8.765,9.123 5.432,3.345 4.456))", poly); | |
53 | boost::geometry::for_each_point(poly, round_coordinates<point>(0.1)); | |
54 | std::cout << "Rounded: " << boost::geometry::wkt(poly) << std::endl; | |
55 | return 0; | |
56 | } | |
57 | ||
58 | //] | |
59 | ||
60 | ||
61 | //[for_each_point_output | |
62 | /*` | |
63 | Output: | |
64 | [pre | |
65 | Rounded: POLYGON((0 0,1.1 10,8.9 2.2,0 0),(3.3 4.5,7.7 8.8,9.1 5.4,3.3 4.5)) | |
66 | ] | |
67 | */ | |
68 | //] |