This second custom triangle example shows an alternative implementation for a custom shape, showing a partial specialization for the area calculation.
#include <iostream>
#include <boost/array.hpp>
#include <boost/tuple/tuple.hpp>
#include <boost/geometry/io/dsv/write.hpp>
template <typename P>
struct triangle : public boost::array<P, 3>
{
};
namespace boost { namespace geometry { namespace dispatch {
template<typename Point>
struct area<triangle<Point>, ring_tag>
{
template <typename Strategy>
static inline double apply(triangle<Point> const& t, Strategy const&)
{
return 0.5 * ((get<0>(t[1]) - get<0>(t[0])) * (get<1>(t[2]) - get<1>(t[0]))
- (get<0>(t[2]) - get<0>(t[0])) * (get<1>(t[1]) - get<1>(t[0])));
}
};
}}}
int main()
{
triangle<boost::tuple<double, double> > t;
t[0] = boost::make_tuple(0, 0);
t[1] = boost::make_tuple(5, 0);
t[2] = boost::make_tuple(2.5, 2.5);
std::cout << "Triangle: " << boost::geometry::dsv(t) << std::endl;
boost::tuple<double, double> c;
std::cout << "Centroid: " << boost::geometry::dsv(c) << std::endl;
return 0;
}