06_a_transformation_example.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
  3. // Copyright (c) 2008-2012 Bruno Lalande, Paris, France.
  4. // Copyright (c) 2009-2012 Mateusz Loskot, London, UK.
  5. // Use, modification and distribution is subject to the Boost Software License,
  6. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //
  9. // Transformation Example
  10. #include <iostream>
  11. #include <boost/geometry/geometry.hpp>
  12. #include <boost/geometry/geometries/point_xy.hpp>
  13. #include <boost/geometry/geometries/polygon.hpp>
  14. #include <boost/geometry/geometries/adapted/c_array.hpp>
  15. BOOST_GEOMETRY_REGISTER_C_ARRAY_CS(cs::cartesian)
  16. int main()
  17. {
  18. using namespace boost::geometry;
  19. typedef model::d2::point_xy<double> point_2d;
  20. point_2d p(1, 1);
  21. point_2d p2;
  22. // Example: translate a point over (5,5)
  23. strategy::transform::translate_transformer<double, 2, 2> translate(5, 5);
  24. transform(p, p2, translate);
  25. std::cout << "transformed point " << boost::geometry::dsv(p2) << std::endl;
  26. // Transform a polygon
  27. model::polygon<point_2d> poly, poly2;
  28. const double coor[][2] = { {0, 0}, {0, 7}, {2, 2}, {2, 0}, {0, 0} };
  29. // note that for this syntax you have to include the two
  30. // include files above (c_array.hpp)
  31. assign_points(poly, coor);
  32. //read_wkt("POLYGON((0 0,0 7,4 2,2 0,0 0))", poly);
  33. transform(poly, poly2, translate);
  34. std::cout << "source polygon " << boost::geometry::dsv(poly) << std::endl;
  35. std::cout << "transformed polygon " << boost::geometry::dsv(poly2) << std::endl;
  36. // Many more transformations are possible:
  37. // - from Cartesian to Spherical coordinate systems and back
  38. // - from Cartesian to Cartesian (mapping, affine transformations) and back (inverse)
  39. // - Map Projections
  40. // - from Degree to Radian and back in spherical_equatorial or geographic coordinate systems
  41. return 0;
  42. }