c04_b_custom_triangle_example.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
  3. // Copyright (c) 2008-2012 Bruno Lalande, Paris, France.
  4. // Copyright (c) 2009-2012 Mateusz Loskot, London, UK.
  5. // Use, modification and distribution is subject to the Boost Software License,
  6. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //
  9. // Custom triangle template Example
  10. #include <iostream>
  11. #include <boost/array.hpp>
  12. #include <boost/tuple/tuple.hpp>
  13. #include <boost/geometry/algorithms/area.hpp>
  14. #include <boost/geometry/algorithms/centroid.hpp>
  15. #include <boost/geometry/geometries/adapted/boost_tuple.hpp>
  16. #include <boost/geometry/geometries/register/ring.hpp>
  17. #include <boost/geometry/strategies/strategies.hpp>
  18. #include <boost/geometry/io/dsv/write.hpp>
  19. BOOST_GEOMETRY_REGISTER_BOOST_TUPLE_CS(cs::cartesian)
  20. template <typename P>
  21. struct triangle : public boost::array<P, 3>
  22. {
  23. };
  24. // Register triangle<P> as a ring
  25. BOOST_GEOMETRY_REGISTER_RING_TEMPLATED(triangle)
  26. namespace boost { namespace geometry { namespace dispatch {
  27. // Specializations of area dispatch structure, implement algorithm
  28. template<typename Point>
  29. struct area<triangle<Point>, ring_tag>
  30. {
  31. template <typename Strategy>
  32. static inline double apply(triangle<Point> const& t, Strategy const&)
  33. {
  34. return 0.5 * ((get<0>(t[1]) - get<0>(t[0])) * (get<1>(t[2]) - get<1>(t[0]))
  35. - (get<0>(t[2]) - get<0>(t[0])) * (get<1>(t[1]) - get<1>(t[0])));
  36. }
  37. };
  38. }}} // namespace boost::geometry::dispatch
  39. int main()
  40. {
  41. //triangle<boost::geometry::point_xy<double> > t;
  42. triangle<boost::tuple<double, double> > t;
  43. t[0] = boost::make_tuple(0, 0);
  44. t[1] = boost::make_tuple(5, 0);
  45. t[2] = boost::make_tuple(2.5, 2.5);
  46. std::cout << "Triangle: " << boost::geometry::dsv(t) << std::endl;
  47. std::cout << "Area: " << boost::geometry::area(t) << std::endl;
  48. //boost::geometry::point_xy<double> c;
  49. boost::tuple<double, double> c;
  50. boost::geometry::centroid(t, c);
  51. std::cout << "Centroid: " << boost::geometry::dsv(c) << std::endl;
  52. return 0;
  53. }