x01_qt_example.cpp

This sample demonstrates that by usage of concepts, external geometries can be handled by GGL, just calling by a one-line registration macro. In this case for the Qt Widget Library.

The example, code shown below, results in this window-output:

    //Boost.Geometry(akaGGL,GenericGeometryLibrary)
    //
    //Copyright(c)2007-2012BarendGehrels,Amsterdam,theNetherlands.
    //Use,modificationanddistributionissubjecttotheBoostSoftwareLicense,
    //Version1.0.(SeeaccompanyingfileLICENSE_1_0.txtorcopyat
    //http://www.boost.org/LICENSE_1_0.txt)

    //QtExample

    //Qtisawell-knownandoftenusedplatformindependentwindowslibrary

    //Tobuildandrunthisexample:
    //1)download(fromhttp://qt.nokia.com),configureandmakeQT
    //2)ifnecessary,adaptQtclauseinincludepath(notethereisaQtpropertysheet)

    #include<sstream>

    #include<QtGui>

    #include<boost/geometry/geometry.hpp>
    #include<boost/geometry/geometries/register/point.hpp>
    #include<boost/geometry/geometries/register/ring.hpp>

    //AdaptaQPointFsuchthatitcanbehandledbyBoost.Geometry
    BOOST_GEOMETRY_REGISTER_POINT_2D_GET_SET(QPointF,double,cs::cartesian,x,y,setX,setY)

    //AdaptaQPolygonFaswell.
    //AQPolygonFhasnoholes(interiors)soitissimilartoaBoost.Geometryring
    BOOST_GEOMETRY_REGISTER_RING(QPolygonF)

    intmain(intargc,char*argv[])
    {
    //ThisusageQApplicationandQLabelisadaptedfrom
    //http://en.wikipedia.org/wiki/Qt_(toolkit)#Qt_hello_world
    QApplicationapp(argc,argv);

    //DeclareaQtpolygon.TheQtPolygoncanbeused
    //inBoost.Geometry,justbyitsonelineregistrationabove.
    QPolygonFpolygon;

    //UseQttoaddpointstopolygon
    polygon
    <<QPointF(10,20)<<QPointF(20,30)
    <<QPointF(30,20)<<QPointF(20,10)
    <<QPointF(10,20);

    //UseBoost.Geometrye.g.tocalculatearea
    std::ostringstreamout;
    out<<"Boost.Geometryarea:"<<boost::geometry::area(polygon)<<std::endl;

    //SomefunctionalityisdefinedinbothQtandBoost.Geometry
    QPointFp(20,20);
    out<<"Qtcontains:"
    <<(polygon.containsPoint(p,Qt::WindingFill)?"yes":"no")
    <<std::endl
    <<"Boost.Geometrywithin:"
    <<(boost::geometry::within(p,polygon)?"yes":"no")
    <<std::endl;
    //Detail:ifpointisONboundary,Qtsaysyes,Boost.Geometrysaysno.

    //Qtdefinesaniterator
    //(whichisrequiredforoftheBoost.Geometryring-concept)
    //suchthatBoost.Geometrycanusethepointsofthispolygon
    QPolygonF::const_iteratorit;
    for(it=polygon.begin();it!=polygon.end();++it)
    {
    //StreamDelimiter-Separated,justtoshowsomethingBoost.Geometrycando
    out<<boost::geometry::dsv(*it)<<std::endl;
    }

    //Streamthepolygonaswell
    out<<boost::geometry::dsv(polygon)<<std::endl;

    //Justshowwhatwedidinalabel
    QLabellabel(out.str().c_str());
    label.show();
    returnapp.exec();
    }