...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
Calculates the number of points of a geometry.
The free function num_points calculates the number of points of a geometry.
template<typename Geometry> std::size_t num_points(Geometry const & geometry, bool add_for_open = false)
Type |
Concept |
Name |
Description |
---|---|---|---|
Geometry const & |
Any type fulfilling a Geometry Concept |
geometry |
A model of the specified concept |
bool |
add_for_open |
add one for open geometries (i.e. polygon types which are not closed) |
The calculated number of points
Either
#include <boost/geometry.hpp>
Or
#include <boost/geometry/algorithms/num_points.hpp>
The function num_points implements function NumPoints from the OGC Simple Feature Specification.
Note | |
---|---|
num_points can be called for any geometry and not just linestrings (as the standard describes) |
Case |
Behavior |
---|---|
Point |
Returns 1 |
Segment |
Returns 2 |
Box |
Returns 2^d, where d is the dimension of the box |
Rangelike (linestring, ring) |
Returns boost::size(geometry) |
Other geometries |
Returns the sum of the number of points of its elements |
Open geometries |
Returns the sum of the number of points of its elements, it adds one for open (per ring) if specified |
Closed geometries |
Returns the sum of the number of points of its elements |
Constant or Linear
Get the number of points in a geometry
#include <iostream> #include <boost/geometry.hpp> #include <boost/geometry/geometries/point_xy.hpp> #include <boost/geometry/geometries/polygon.hpp> #include <boost/geometry/geometries/multi_polygon.hpp> int main() { boost::geometry::model::multi_polygon < boost::geometry::model::polygon < boost::geometry::model::d2::point_xy<double> > > mp; boost::geometry::read_wkt("MULTIPOLYGON(((0 0,0 10,10 0,0 0),(1 1,1 9,9 1,1 1)),((10 10,10 7,7 10,10 10)))", mp); std::cout << "Number of points: " << boost::geometry::num_points(mp) << std::endl; return 0; }
Output:
Number of points: 12