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