PageRenderTime 6ms CodeModel.GetById 2ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/Src/Dependencies/Boost/libs/geometry/doc/src/examples/algorithms/comparable_distance.cpp

http://hadesmem.googlecode.com/
C++ | 67 lines | 33 code | 16 blank | 18 comment | 3 complexity | d743a50517ce90ec238e6daa13df2dbf MD5 | raw file
 1// Boost.Geometry (aka GGL, Generic Geometry Library)
 2// QuickBook Example
 3
 4// Copyright (c) 2011 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
21int 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/*`
60Output:
61[pre
62Closest: (1, 3)
63At: 0.565685
64]
65*/
66//]
67