c04_b_custom_triangle_example.cpp

This second custom triangle example shows an alternative implementation for a custom shape, showing a partial specialization for the area calculation.

    //Boost.Geometry(akaGGL,GenericGeometryLibrary)

    //Copyright(c)2007-2012BarendGehrels,Amsterdam,theNetherlands.
    //Copyright(c)2008-2012BrunoLalande,Paris,France.
    //Copyright(c)2009-2012MateuszLoskot,London,UK.

    //Use,modificationanddistributionissubjecttotheBoostSoftwareLicense,
    //Version1.0.(SeeaccompanyingfileLICENSE_1_0.txtorcopyat
    //http://www.boost.org/LICENSE_1_0.txt)
    //
    //CustomtriangletemplateExample

    #include<iostream>

    #include<boost/array.hpp>
    #include<boost/tuple/tuple.hpp>

    #include<boost/geometry/algorithms/area.hpp>
    #include<boost/geometry/algorithms/centroid.hpp>
    #include<boost/geometry/geometries/adapted/boost_tuple.hpp>
    #include<boost/geometry/geometries/register/ring.hpp>
    #include<boost/geometry/strategies/strategies.hpp>
    #include<boost/geometry/io/dsv/write.hpp>

    BOOST_GEOMETRY_REGISTER_BOOST_TUPLE_CS(cs::cartesian)

    template<typenameP>
    structtriangle:publicboost::array<P,3>
    {
    };

    //Registertriangle<P>asaring
    BOOST_GEOMETRY_REGISTER_RING_TEMPLATED(triangle)

    namespaceboost{namespacegeometry{namespacedispatch{

    //Specializationsofareadispatchstructure,implementalgorithm
    template<typenamePoint>
    structarea<triangle<Point>,ring_tag>
    {
    template<typenameStrategy>
    staticinlinedoubleapply(triangle<Point>const&t,Strategyconst&)
    {
    return0.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])));
    }
    };

    }}}//namespaceboost::geometry::dispatch

    intmain()
    {
    //triangle<boost::geometry::point_xy<double>>t;
    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;
    std::cout<<"Area:"<<boost::geometry::area(t)<<std::endl;

    //boost::geometry::point_xy<double>c;
    boost::tuple<double,double>c;
    boost::geometry::centroid(t,c);
    std::cout<<"Centroid:"<<boost::geometry::dsv(c)<<std::endl;

    return0;
    }