c01_custom_point_example.cpp

This sample demonstrates that custom points can be made as well. This sample contains many points, derived from boost::tuple, created from scratch, read only points, legacy points, etc.

    //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)
    //
    //CustompointExample

    #include<iostream>

    #include<boost/geometry/algorithms/distance.hpp>
    #include<boost/geometry/algorithms/make.hpp>
    #include<boost/geometry/geometries/register/point.hpp>
    #include<boost/geometry/strategies/strategies.hpp>
    #include<boost/geometry/io/dsv/write.hpp>

    //Samplepoint,definingthreecolorvalues
    structmy_color_point
    {
    doublered,green,blue;
    };

    //Samplepoint,havinganintarraydefined
    structmy_array_point
    {
    intc[3];
    };

    //Samplepoint,havingx/y
    structmy_2d
    {
    floatx,y;
    };

    //Sampleclass,protectedandconstruction-time-onlyx/y,
    //Can(ofcourse)onlyusedinalgorithmswhichtakeconst&points
    classmy_class_ro
    {
    public:
    my_class_ro(doublex,doubley):m_x(x),m_y(y){}
    doublex()const{returnm_x;}
    doubley()const{returnm_y;}
    private:
    doublem_x,m_y;
    };

    //Sampleclassusingreferencesforread/write
    classmy_class_rw
    {
    public:
    constdouble&x()const{returnm_x;}
    constdouble&y()const{returnm_y;}
    double&x(){returnm_x;}
    double&y(){returnm_y;}
    private:
    doublem_x,m_y;
    };

    //Sampleclassusinggetters/setters
    classmy_class_gs
    {
    public:
    constdoubleget_x()const{returnm_x;}
    constdoubleget_y()const{returnm_y;}
    voidset_x(doublev){m_x=v;}
    voidset_y(doublev){m_y=v;}
    private:
    doublem_x,m_y;
    };

    //Samplepointwithinanamespace
    namespacemy
    {
    structmy_namespaced_point
    {
    doublex,y;
    };
    }

    BOOST_GEOMETRY_REGISTER_POINT_3D(my_color_point,double,cs::cartesian,red,green,blue)
    BOOST_GEOMETRY_REGISTER_POINT_3D(my_array_point,int,cs::cartesian,c[0],c[1],c[2])
    BOOST_GEOMETRY_REGISTER_POINT_2D(my_2d,float,cs::cartesian,x,y)
    BOOST_GEOMETRY_REGISTER_POINT_2D_CONST(my_class_ro,double,cs::cartesian,x(),y())
    BOOST_GEOMETRY_REGISTER_POINT_2D(my_class_rw,double,cs::cartesian,x(),y())
    BOOST_GEOMETRY_REGISTER_POINT_2D_GET_SET(my_class_gs,double,cs::cartesian,get_x,get_y,set_x,set_y)

    BOOST_GEOMETRY_REGISTER_POINT_2D(my::my_namespaced_point,double,cs::cartesian,x,y)

    intmain()
    {
    //Create2instancesofourcustomcolorpoint
    my_color_pointc1=boost::geometry::make<my_color_point>(255,3,233);
    my_color_pointc2=boost::geometry::make<my_color_point>(0,50,200);

    //Thedistancebetweenthemcanbecalculatedusingthecartesianmethod(=pythagoras)
    //providedwiththelibrary,configuredbythecoordinate_systemtypeofthepoint
    std::cout<<"colordistance"
    <<boost::geometry::dsv(c1)<<"to"
    <<boost::geometry::dsv(c2)<<"is"
    <<boost::geometry::distance(c1,c2)<<std::endl;

    my_array_pointa1={{0}};
    my_array_pointa2={{0}};
    boost::geometry::assign_values(a1,1,2,3);
    boost::geometry::assign_values(a2,3,2,1);

    std::cout<<"colordistance"
    <<boost::geometry::dsv(a1)<<"to"
    <<boost::geometry::dsv(a2)<<"is"
    <<boost::geometry::distance(a1,a2)<<std::endl;

    my_2dp1={1,5};
    my_2dp2={3,4};
    std::cout<<"floatdistance"
    <<boost::geometry::dsv(p1)<<"to"
    <<boost::geometry::dsv(p2)<<"is"
    <<boost::geometry::distance(p1,p2)<<std::endl;

    my_class_rocro1(1,2);
    my_class_rocro2(3,4);
    std::cout<<"classrodistance"
    <<boost::geometry::dsv(cro1)<<"to"
    <<boost::geometry::dsv(cro2)<<"is"
    <<boost::geometry::distance(cro1,cro2)<<std::endl;

    my_class_rwcrw1;
    my_class_rwcrw2;
    boost::geometry::assign_values(crw1,1,2);
    boost::geometry::assign_values(crw2,3,4);
    std::cout<<"classr/wdistance"
    <<boost::geometry::dsv(crw1)<<"to"
    <<boost::geometry::dsv(crw2)<<"is"
    <<boost::geometry::distance(crw1,crw2)<<std::endl;

    my_class_gscgs1;
    my_class_gscgs2;
    boost::geometry::assign_values(cgs1,1,2);
    boost::geometry::assign_values(cgs2,3,4);
    std::cout<<"classg/sdistance"
    <<boost::geometry::dsv(crw1)<<"to"
    <<boost::geometry::dsv(crw2)<<"is"
    <<boost::geometry::distance(cgs1,cgs2)<<std::endl;

    my::my_namespaced_pointnsp1=boost::geometry::make<my::my_namespaced_point>(1,2);
    my::my_namespaced_pointnsp2=boost::geometry::make<my::my_namespaced_point>(3,4);
    std::cout<<"namespaceddistance"
    <<boost::geometry::dsv(nsp1)<<"to"
    <<boost::geometry::dsv(nsp2)<<"is"
    <<boost::geometry::distance(nsp1,nsp2)<<std::endl;

    return0;
    }