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