...one of the most highly
regarded and expertly designed C++ library projects in the
world.

— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards

Calculate the comparable distance measurement of two geometries.

The free function comparable_distance does not necessarily calculate the distance, but it calculates a distance measure such that two distances are comparable to each other. For example: for the Cartesian coordinate system, Pythagoras is used but the square root is not taken, which makes it faster and the results of two point pairs can still be compared to each other.

template<typename Geometry1, typename Geometry2> default_distance_result<Geometry1, Geometry2>::type comparable_distance(Geometry1 const & geometry1, Geometry2 const & geometry2)

Type |
Concept |
Name |
Description |
---|---|---|---|

Geometry1 const & |
first geometry type |
geometry1 |
A model of the specified concept |

Geometry2 const & |
second geometry type |
geometry2 |
A model of the specified concept |

The calculated comparable distance

Either

`#include <boost/geometry/geometry.hpp>`

Or

`#include <boost/geometry/algorithms/comparable_distance.hpp>`

There is no (not yet) version with a strategy.

It depends on the coordinate system of the geometry's point type if there is a strategy available which can determine (more efficient than the standard strategy) a measure of comparable distance.

Linear

Shows how to efficiently get the closest point

#include <iostream> #include <boost/geometry.hpp> #include <boost/geometry/geometries/point_xy.hpp> #include <boost/numeric/conversion/bounds.hpp> #include <boost/foreach.hpp> int main() { typedef boost::geometry::model::d2::point_xy<double> point_type; point_type p(1.4, 2.6); std::vector<point_type> v; for (double x = 0.0; x <= 4.0; x++) { for (double y = 0.0; y <= 4.0; y++) { v.push_back(point_type(x, y)); } } point_type min_p; double min_d = boost::numeric::bounds<double>::highest(); BOOST_FOREACH(point_type const& pv, v) { double d = boost::geometry::comparable_distance(p, pv); if (d < min_d) { min_d = d; min_p = pv; } } std::cout << "Closest: " << boost::geometry::dsv(min_p) << std::endl << "At: " << boost::geometry::distance(p, min_p) << std::endl; return 0; }

Output:

Closest: (1, 3) At: 0.565685