3. Quick start
This quick start shows boost Some features of geometry are in the form of relatively simple code snippets with comments.
The following code assumes that boost / geometry HPP, and use the header file boost::geometry. Boost.Geometry is an end file, so it only contains header files. There is no need to link to any library.
#include <boost/geometry.hpp> #include <boost/geometry/geometries/point_xy.hpp> #include <boost/geometry/geometries/polygon.hpp> using namespace boost::geometry;
3.1 Descartes
You can use only a small part of the library. For example, the distance between two points is a common use case. Promoting geometry can calculate it from various types. Use one of its own types:
model::d2::point_xy<int> p1(1, 1), p2(2, 2); std::cout << "Distance p1-p2 is: " << distance(p1, p2) << std::endl;
If the right title is included and the type is bound to the coordinate system, various other types can be used as points: ordinary C array, Boost array, Boost Tuple's, Boost Fusion, imported structure, your own class...
Register and use C arrays:
#include <boost/geometry/geometries/adapted/c_array.hpp> BOOST_GEOMETRY_REGISTER_C_ARRAY_CS(cs::cartesian)
int a[2] = {1,1}; int b[2] = {2,3}; double d = distance(a, b); std::cout << "Distance a-b is: " << d << std::endl;
Another commonly used algorithm is point in polygon, which is used in boost Geometry is implemented by the within function. We show his usage here and check a boost Whether tuple (as a point) is in a polygon filled with C array point pairs.
But first register a boost Tuple, just like registering C array:
#include <boost/geometry/geometries/adapted/boost_tuple.hpp> BOOST_GEOMETRY_REGISTER_BOOST_TUPLE_CS(cs::cartesian)
double points[][2] = {{2.0, 1.3}, {4.1, 3.0}, {5.3, 2.6}, {2.9, 0.7}, {2.0, 1.3}}; model::polygon<model::d2::point_xy<double> > poly; append(poly, points); boost::tuple<double, double> p = boost::make_tuple(3.7, 2.0); std::cout << "Point p is in polygon? " << std::boolalpha << within(p, poly) << std::endl;
We can calculate the area of a polygon:
std::cout << "Area: " << area(poly) << std::endl;
Depending on the nature of the template library, you can mix point types. We calculate the distance again. Now we use a C array point and a Boost tuple point:
double d2 = distance(a, p); std::cout << "Distance a-p is: " << d2 << std::endl;
The code snippets listed above produce the following output:
Distance p1-p2 is: 1.41421 Distance a-b is: 2.23607 Point p is in polygon? true Area: 3.015 Distance a-p is: 2.87924
3.2 non Cartesian
You can also use non Cartesian points. For example: a point on a sphere. When using algorithms such as distance, the library "checks" whether it is dealing with spherical points and calculates the distance on the sphere instead of applying Pythagorean theorem.
The promotion geometry supports a geographic coordinate system, but it is in an extension and is not published in the current version of Boost.
We approximate the earth to a sphere and calculate the distance between Amsterdam and Paris:
typedef boost::geometry::model::point < double, 2, boost::geometry::cs::spherical_equatorial<boost::geometry::degree> > spherical_point; spherical_point amsterdam(4.90, 52.37); spherical_point paris(2.35, 48.86); double const earth_radius = 3959; // miles std::cout << "Distance in miles: " << distance(amsterdam, paris) * earth_radius << std::endl;
3.3 adaptive structure
Finally, there is an example of a completely different area: developing window based applications, such as using QtWidgets. As long as the Qt class is registered in Boost. We can use them. For example, we can check whether two rectangles overlap. If so, move the second rectangle to another position:
QRect r1(100, 200, 15, 15); QRect r2(110, 210, 20, 20); if (overlaps(r1, r2)) { assign_values(r2, 200, 300, 220, 320); }
How to register QT class in Boost
3.4 more
More examples can be found in the reference section.