| Home | Libraries | People | FAQ | More

##### comparable_distance

Calculate the comparable distance measurement of two geometries.

###### Description

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.

###### Synopsis

    template<typename Geometry1, typename Geometry2>
    auto comparable_distance(Geometry1 const & geometry1, Geometry2 const & geometry2)

###### Parameters

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

###### Returns

The calculated comparable distance

###### Header

Either

`#include <boost/geometry.hpp>`

Or

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

###### Conformance

The function comparable_distance is not defined by OGC.

###### Behaviour

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.

###### Supported geometries

| Point | Segment | Box | Linestring | Ring | Polygon | MultiPoint | MultiLinestring | MultiPolygon | Variant
--- | --- | --- | --- | --- | --- | --- | --- | --- | --- | ---
Point |  |  |  |  |  |  |  |  |  |
Segment |  |  |  |  |  |  |  |  |  |
Box |  |  |  |  |  |  |  |  |  |
Linestring |  |  |  |  |  |  |  |  |  |
Ring |  |  |  |  |  |  |  |  |  |
Polygon |  |  |  |  |  |  |  |  |  |
MultiPoint |  |  |  |  |  |  |  |  |  |
MultiLinestring |  |  |  |  |  |  |  |  |  |
MultiPolygon |  |  |  |  |  |  |  |  |  |
Variant |  |  |  |  |  |  |  |  |  |

###### Complexity

For point to geometry: linear

For multi-point to ring/polygon/multi-polygon: currently quadratic

For all other geometry combinations: linearithmic

###### Example

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>

    int main()
    {
        using point_type = boost::geometry::model::d2::point_xy<double>;

        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();
        for (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