]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // Boost.Geometry (aka GGL, Generic Geometry Library) |
2 | // Quickbook Examples, for main page | |
3 | ||
4 | // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands. | |
5 | // Copyright (c) 2008-2012 Bruno Lalande, Paris, France. | |
6 | // Copyright (c) 2009-2012 Mateusz Loskot, London, UK. | |
7 | ||
8 | // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library | |
9 | // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands. | |
10 | ||
11 | // Use, modification and distribution is subject to the Boost Software License, | |
12 | // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at | |
13 | // http://www.boost.org/LICENSE_1_0.txt) | |
14 | // | |
15 | ||
16 | ||
17 | #if defined(_MSC_VER) | |
18 | // We deliberately mix float/double's here so turn off warning | |
19 | //#pragma warning( disable : 4244 ) | |
20 | #endif // defined(_MSC_VER) | |
21 | ||
22 | //[quickstart_include | |
23 | ||
24 | #include <boost/geometry.hpp> | |
25 | #include <boost/geometry/geometries/point_xy.hpp> | |
26 | #include <boost/geometry/geometries/polygon.hpp> | |
27 | ||
28 | using namespace boost::geometry; | |
29 | //] | |
30 | ||
31 | #include <boost/geometry/geometries/register/point.hpp> | |
32 | ||
33 | ||
34 | //[quickstart_register_c_array | |
35 | #include <boost/geometry/geometries/adapted/c_array.hpp> | |
36 | ||
37 | BOOST_GEOMETRY_REGISTER_C_ARRAY_CS(cs::cartesian) | |
38 | //] | |
39 | ||
40 | //[quickstart_register_boost_tuple | |
41 | #include <boost/geometry/geometries/adapted/boost_tuple.hpp> | |
42 | ||
43 | BOOST_GEOMETRY_REGISTER_BOOST_TUPLE_CS(cs::cartesian) | |
44 | //] | |
45 | ||
46 | // Small QRect simulations following http://doc.trolltech.com/4.4/qrect.html | |
47 | // Todo: once work the traits out further, would be nice if there is a real example of this. | |
48 | // However for the example it makes no difference, it will work any way. | |
49 | struct QPoint | |
50 | { | |
51 | int x, y; | |
52 | // In Qt these are methods but for example below it makes no difference | |
53 | }; | |
54 | ||
55 | struct QRect | |
56 | { | |
57 | int x, y, width, height; | |
58 | QRect(int _x, int _y, int w, int h) | |
59 | : x(_x), y(_y), width(w), height(h) | |
60 | {} | |
61 | // In Qt these are methods but that will work as well, requires changing traits below | |
62 | }; | |
63 | ||
64 | ||
65 | // Would be get/set with x(),y(),setX(),setY() | |
66 | BOOST_GEOMETRY_REGISTER_POINT_2D(QPoint, int, cs::cartesian, x, y) | |
67 | ||
68 | ||
69 | // Register the QT rectangle. The macro(s) does not offer (yet) enough flexibility to do this in one line, | |
70 | // but the traits classes do their job perfectly. | |
71 | namespace boost { namespace geometry { namespace traits | |
72 | { | |
73 | ||
74 | template <> struct tag<QRect> { typedef box_tag type; }; | |
75 | template <> struct point_type<QRect> { typedef QPoint type; }; | |
76 | ||
77 | template <size_t C, size_t D> | |
78 | struct indexed_access<QRect, C, D> | |
79 | { | |
80 | static inline int get(const QRect& qr) | |
81 | { | |
82 | // Would be: x(), y(), width(), height() | |
83 | return C == min_corner && D == 0 ? qr.x | |
84 | : C == min_corner && D == 1 ? qr.y | |
85 | : C == max_corner && D == 0 ? qr.x + qr.width | |
86 | : C == max_corner && D == 1 ? qr.y + qr.height | |
87 | : 0; | |
88 | } | |
89 | ||
90 | static inline void set(QRect& qr, const int& value) | |
91 | { | |
92 | // Would be: setX, setY, setWidth, setHeight | |
93 | if (C == min_corner && D == 0) qr.x = value; | |
94 | else if (C == min_corner && D == 1) qr.y = value; | |
95 | else if (C == max_corner && D == 0) qr.width = value - qr.x; | |
96 | else if (C == max_corner && D == 1) qr.height = value - qr.y; | |
97 | } | |
98 | }; | |
99 | ||
100 | ||
101 | }}} | |
102 | ||
103 | ||
104 | int main(void) | |
105 | { | |
106 | //[quickstart_distance | |
107 | model::d2::point_xy<int> p1(1, 1), p2(2, 2); | |
108 | std::cout << "Distance p1-p2 is: " << distance(p1, p2) << std::endl; | |
109 | //] | |
110 | ||
111 | //[quickstart_distance_c_array | |
112 | int a[2] = {1,1}; | |
113 | int b[2] = {2,3}; | |
114 | double d = distance(a, b); | |
115 | std::cout << "Distance a-b is: " << d << std::endl; | |
116 | //] | |
117 | ||
118 | //[quickstart_point_in_polygon | |
119 | double points[][2] = {{2.0, 1.3}, {4.1, 3.0}, {5.3, 2.6}, {2.9, 0.7}, {2.0, 1.3}}; | |
120 | model::polygon<model::d2::point_xy<double> > poly; | |
121 | append(poly, points); | |
122 | boost::tuple<double, double> p = boost::make_tuple(3.7, 2.0); | |
123 | std::cout << "Point p is in polygon? " << std::boolalpha << within(p, poly) << std::endl; | |
124 | //] | |
125 | ||
126 | //[quickstart_area | |
127 | std::cout << "Area: " << area(poly) << std::endl; | |
128 | //] | |
129 | ||
130 | //[quickstart_distance_mixed | |
131 | double d2 = distance(a, p); | |
132 | std::cout << "Distance a-p is: " << d2 << std::endl; | |
133 | //] | |
134 | ||
135 | //[quick_start_spherical | |
136 | typedef boost::geometry::model::point | |
137 | < | |
138 | double, 2, boost::geometry::cs::spherical_equatorial<boost::geometry::degree> | |
139 | > spherical_point; | |
140 | ||
141 | spherical_point amsterdam(4.90, 52.37); | |
142 | spherical_point paris(2.35, 48.86); | |
143 | ||
144 | double const earth_radius = 3959; // miles | |
145 | std::cout << "Distance in miles: " << distance(amsterdam, paris) * earth_radius << std::endl; | |
146 | //] | |
147 | ||
148 | /*** | |
149 | Now extension | |
150 | point_ll_deg amsterdam, paris; | |
151 | parse(amsterdam, "52 22 23 N", "4 53 32 E"); | |
152 | parse(paris, "48 52 0 N", "2 19 59 E"); | |
153 | std::cout << "Distance A'dam-Paris: " << distance(amsterdam, paris) / 1000.0 << " kilometers " << std::endl; | |
154 | ***/ | |
155 | ||
156 | //[quickstart_qt | |
157 | QRect r1(100, 200, 15, 15); | |
158 | QRect r2(110, 210, 20, 20); | |
159 | if (overlaps(r1, r2)) | |
160 | { | |
161 | assign_values(r2, 200, 300, 220, 320); | |
162 | } | |
163 | //] | |
164 | ||
165 | return 0; | |
166 | } | |
167 |