...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
Reverses the points within a geometry.
Generic function to reverse a geometry. It resembles the std::reverse functionality, but it takes the geometry type into account. Only for a ring or for a linestring it is the same as the std::reverse.
template<typename Geometry> void reverse(Geometry & geometry)
Type |
Concept |
Name |
Description |
---|---|---|---|
Geometry & |
Any type fulfilling a Geometry Concept |
geometry |
A model of the specified concept which will be reversed |
Either
#include <boost/geometry.hpp>
Or
#include <boost/geometry/algorithms/reverse.hpp>
The function reverse is not defined by OGC.
The function reverse conforms to the std::reverse function of the C++ std-library.
Case |
Behavior |
---|---|
Point |
Nothing happens, geometry is unchanged |
Segment |
Not yet supported in this version |
Box |
Nothing happens, geometry is unchanged |
Linestring |
Reverses the Linestring |
Ring |
Reverses the Ring |
Polygon |
Reverses the exterior ring and all interior rings in the polygon |
Multi Point |
Nothing happens, geometry is unchanged |
Multi Linestring |
Reverses all contained linestrings individually |
Multi Polygon |
Reverses all contained polygons individually |
Note | |
---|---|
The reverse of a (multi)polygon or ring might make a valid geometry invalid because the (counter)clockwise orientation reverses. |
Linear
Shows how to reverse a ring or polygon
#include <iostream> #include <boost/geometry.hpp> #include <boost/geometry/geometries/polygon.hpp> #include <boost/geometry/geometries/ring.hpp> #include <boost/geometry/geometries/adapted/boost_tuple.hpp> BOOST_GEOMETRY_REGISTER_BOOST_TUPLE_CS(cs::cartesian) #include <boost/assign.hpp> int main() { using boost::assign::tuple_list_of; typedef boost::tuple<int, int> point; typedef boost::geometry::model::polygon<point> polygon; typedef boost::geometry::model::ring<point> ring; polygon poly; boost::geometry::exterior_ring(poly) = tuple_list_of(0, 0)(0, 9)(10, 10)(0, 0); boost::geometry::interior_rings(poly).push_back(tuple_list_of(1, 2)(4, 6)(2, 8)(1, 2)); double area_before = boost::geometry::area(poly); boost::geometry::reverse(poly); double area_after = boost::geometry::area(poly); std::cout << boost::geometry::dsv(poly) << std::endl; std::cout << area_before << " -> " << area_after << std::endl; ring r = tuple_list_of(0, 0)(0, 9)(8, 8)(0, 0); area_before = boost::geometry::area(r); boost::geometry::reverse(r); area_after = boost::geometry::area(r); std::cout << boost::geometry::dsv(r) << std::endl; std::cout << area_before << " -> " << area_after << std::endl; return 0; }
Output:
(((0, 0), (10, 10), (0, 9), (0, 0)), ((1, 2), (2, 8), (4, 6), (1, 2))) 38 -> -38 ((0, 0), (8, 8), (0, 9), (0, 0)) 36 -> -36