1 // Boost.Geometry (aka GGL, Generic Geometry Library)
2 // QuickBook Example
3 
4 // Copyright (c) 2011-2012 Barend Gehrels, Amsterdam, the Netherlands.
5 
6 // Use, modification and distribution is subject to the Boost Software License,
7 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
8 // http://www.boost.org/LICENSE_1_0.txt)
9 
10 //[comparable_distance
11 //` Shows how to efficiently get the closest point
12 
13 #include <iostream>
14 
15 #include <boost/geometry.hpp>
16 #include <boost/geometry/geometries/point_xy.hpp>
17 
18 #include <boost/numeric/conversion/bounds.hpp>
19 #include <boost/foreach.hpp>
20 
main()21 int main()
22 {
23     typedef boost::geometry::model::d2::point_xy<double> point_type;
24 
25     point_type p(1.4, 2.6);
26 
27     std::vector<point_type> v;
28     for (double x = 0.0; x <= 4.0; x++)
29     {
30         for (double y = 0.0; y <= 4.0; y++)
31         {
32             v.push_back(point_type(x, y));
33         }
34     }
35 
36     point_type min_p;
37     double min_d = boost::numeric::bounds<double>::highest();
38     BOOST_FOREACH(point_type const& pv, v)
39     {
40         double d = boost::geometry::comparable_distance(p, pv);
41         if (d < min_d)
42         {
43             min_d = d;
44             min_p = pv;
45         }
46     }
47 
48     std::cout
49         << "Closest: " << boost::geometry::dsv(min_p) << std::endl
50         << "At: " << boost::geometry::distance(p, min_p) << std::endl;
51 
52     return 0;
53 }
54 
55 //]
56 
57 
58 //[comparable_distance_output
59 /*`
60 Output:
61 [pre
62 Closest: (1, 3)
63 At: 0.565685
64 ]
65 */
66 //]
67 
68