...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 length of a geometry.
The free function length calculates the length (the sum of distances between consecutive points) of a geometry. It uses the default strategy, based on the coordinate system of the geometry.
template<typename Geometry> default_length_result<Geometry>::type length(Geometry const & geometry)
Type |
Concept |
Name |
Description |
---|---|---|---|
Geometry const & |
Any type fulfilling a Geometry Concept |
geometry |
A model of the specified concept |
The calculated length
Either
#include <boost/geometry.hpp>
Or
#include <boost/geometry/algorithms/length.hpp>
The function length implements function Length from the OGC Simple Feature Specification.
Case |
Behavior |
---|---|
pointlike (e.g. point) |
Returns 0 |
linear (e.g. linestring) |
Returns the length |
areal (e.g. polygon) |
Returns 0 |
Linear
The following simple example shows the calculation of the length of a linestring containing three points
#include <iostream> #include <boost/geometry.hpp> #include <boost/geometry/geometries/linestring.hpp> #include <boost/geometry/geometries/point_xy.hpp> int main() { using namespace boost::geometry; model::linestring<model::d2::point_xy<double> > line; read_wkt("linestring(0 0,1 1,4 8,3 2)", line); std::cout << "linestring length is " << length(line) << " units" << std::endl; return 0; }
Output:
linestring length is 15.1127 units