1 // Boost.Geometry (aka GGL, Generic Geometry Library)
2 // Quickbook Examples, for main page
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.
8 // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
9 // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
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)
18 // We deliberately mix float/double's here so turn off warning
19 //#pragma warning( disable : 4244 )
20 #endif // defined(_MSC_VER)
26 #include <boost/geometry.hpp>
27 #include <boost/geometry/geometries/point_xy.hpp>
28 #include <boost/geometry/geometries/polygon.hpp>
30 using namespace boost::geometry;
33 #include <boost/geometry/geometries/register/point.hpp>
36 //[quickstart_register_c_array
37 #include <boost/geometry/geometries/adapted/c_array.hpp>
39 BOOST_GEOMETRY_REGISTER_C_ARRAY_CS(cs::cartesian)
42 //[quickstart_register_boost_tuple
43 #include <boost/geometry/geometries/adapted/boost_tuple.hpp>
45 BOOST_GEOMETRY_REGISTER_BOOST_TUPLE_CS(cs::cartesian)
48 // Small QRect simulations following http://doc.trolltech.com/4.4/qrect.html
49 // Todo: once work the traits out further, would be nice if there is a real example of this.
50 // However for the example it makes no difference, it will work any way.
54 // In Qt these are methods but for example below it makes no difference
59 int x, y, width, height;
60 QRect(int _x, int _y, int w, int h)
61 : x(_x), y(_y), width(w), height(h)
63 // In Qt these are methods but that will work as well, requires changing traits below
67 // Would be get/set with x(),y(),setX(),setY()
68 BOOST_GEOMETRY_REGISTER_POINT_2D(QPoint, int, cs::cartesian, x, y)
71 // Register the QT rectangle. The macro(s) does not offer (yet) enough flexibility to do this in one line,
72 // but the traits classes do their job perfectly.
73 namespace boost { namespace geometry { namespace traits
76 template <> struct tag<QRect> { typedef box_tag type; };
77 template <> struct point_type<QRect> { typedef QPoint type; };
79 template <size_t C, size_t D>
80 struct indexed_access<QRect, C, D>
82 static inline int get(const QRect& qr)
84 // Would be: x(), y(), width(), height()
85 return C == min_corner && D == 0 ? qr.x
86 : C == min_corner && D == 1 ? qr.y
87 : C == max_corner && D == 0 ? qr.x + qr.width
88 : C == max_corner && D == 1 ? qr.y + qr.height
92 static inline void set(QRect& qr, const int& value)
94 // Would be: setX, setY, setWidth, setHeight
95 if (C == min_corner && D == 0) qr.x = value;
96 else if (C == min_corner && D == 1) qr.y = value;
97 else if (C == max_corner && D == 0) qr.width = value - qr.x;
98 else if (C == max_corner && D == 1) qr.height = value - qr.y;
108 //[quickstart_distance
109 model::d2::point_xy<int> p1(1, 1), p2(2, 2);
110 std::cout << "Distance p1-p2 is: " << distance(p1, p2) << std::endl;
113 //[quickstart_distance_c_array
116 double d = distance(a, b);
117 std::cout << "Distance a-b is: " << d << std::endl;
120 //[quickstart_point_in_polygon
121 double points[][2] = {{2.0, 1.3}, {4.1, 3.0}, {5.3, 2.6}, {2.9, 0.7}, {2.0, 1.3}};
122 model::polygon<model::d2::point_xy<double> > poly;
123 append(poly, points);
124 boost::tuple<double, double> p = boost::make_tuple(3.7, 2.0);
125 std::cout << "Point p is in polygon? " << std::boolalpha << within(p, poly) << std::endl;
129 std::cout << "Area: " << area(poly) << std::endl;
132 //[quickstart_distance_mixed
133 double d2 = distance(a, p);
134 std::cout << "Distance a-p is: " << d2 << std::endl;
137 //[quick_start_spherical
138 typedef boost::geometry::model::point
140 double, 2, boost::geometry::cs::spherical_equatorial<boost::geometry::degree>
143 spherical_point amsterdam(4.90, 52.37);
144 spherical_point paris(2.35, 48.86);
146 double const earth_radius = 3959; // miles
147 std::cout << "Distance in miles: " << distance(amsterdam, paris) * earth_radius << std::endl;
152 point_ll_deg amsterdam, paris;
153 parse(amsterdam, "52 22 23 N", "4 53 32 E");
154 parse(paris, "48 52 0 N", "2 19 59 E");
155 std::cout << "Distance A'dam-Paris: " << distance(amsterdam, paris) / 1000.0 << " kilometers " << std::endl;
159 QRect r1(100, 200, 15, 15);
160 QRect r2(110, 210, 20, 20);
161 if (overlaps(r1, r2))
163 assign_values(r2, 200, 300, 220, 320);