01_point_example.cpp

In most cases the documentation gives small examples of how to use the algorithms or classes. The point example is a slightly larger example giving an idea of how to use different algorithms from the library, related to points. It shows

- the usage of include files

- how to declare points, using different coordinate types

- how to construct points, specifying coordinates, initializing to zero or to infinite

- how to compare points to each other

- how points can be streamed as OGC text

- calculating the distance from point to point

      //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)
      //
      //PointExample-showingdifferenttypeofpoints

      #include<iostream>

      #include<boost/geometry/geometry.hpp>
      #include<boost/geometry/geometries/point_xy.hpp>
      #include<boost/geometry/geometries/adapted/boost_tuple.hpp>
      #include<boost/geometry/geometries/adapted/c_array.hpp>
      #include<boost/geometry/geometries/adapted/boost_array.hpp>
      #include<boost/geometry/geometries/adapted/boost_polygon/point.hpp>

      BOOST_GEOMETRY_REGISTER_C_ARRAY_CS(cs::cartesian)
      BOOST_GEOMETRY_REGISTER_BOOST_ARRAY_CS(cs::cartesian)
      BOOST_GEOMETRY_REGISTER_BOOST_TUPLE_CS(cs::cartesian)

      intmain()
      {
      usingnamespaceboost::geometry;

      //Boost.Geometrycontainsseveralpointtypes:
      //1:itsowngenerictype
      model::point<double, 2, cs::cartesian>pt1;

      //2:itsowntypetargettedtoCartesian(x,y)coordinates
      model::d2::point_xy<double>pt2;

      //3:itsupportsBoosttuple's
      boost::tuple<double,double>pt3;

      //4:itsupportsnormalarrays
      doublept4[2];

      //5:itsupportsarrays-as-pointsfromBoost.Array
      boost::array<double,2>pt5;

      //6:itsupportspointsfromBoost.Polygon
      boost::polygon::point_data<double>pt6;

      //7:inthepasttherewasatypedefpoint_2d
      //Butusersarenowsuppostedtodothatthemselves:
      typedefmodel::d2::point_xy<double>point_2d;
      point_2dpt7;

      //7:therearemorevariants,andyoucancreateyourown.
      //(seethereforethecustom_pointexample)

      //Allthesetypesarehandledthesameway.Weshowhere
      //assigningthemandcalculatingdistances.
      assign_values(pt1,1,1);
      assign_values(pt2,2,2);
      assign_values(pt3,3,3);
      assign_values(pt4,4,4);
      assign_values(pt5,5,5);
      assign_values(pt6,6,6);
      assign_values(pt7,7,7);

      doubled1=distance(pt1,pt2);
      doubled2=distance(pt3,pt4);
      doubled3=distance(pt5,pt6);
      std::cout<<"Distances:"
      <<d1<<"and"<<d2<<"and"<<d3<<std::endl;

      //(incaseyoudidn'tnote,distancescanbecalculated
      //frompointswithdifferentpoint-types)

      //Severalwaysofconstructionandsettingpointvalues
      //1:default,emptyconstructor,causingnoinitializationatall
      model::d2::point_xy<double>p1;

      //2:asshownabove,assign_values
      model::d2::point_xy<double>p2;
      assign_values(p2,1,1);

      //3:using"set"function
      //setusestheconceptsbehind,suchthatitcanbeappliedfor
      //everypoint-type(likeassign_values)
      model::d2::point_xy<double>p3;
      set<0>(p3,1);
      set<1>(p3,1);
      //set<2>(p3,1);//willresultincompile-error

      //3:foranypointtype,andothergeometryobjects:
      //thereisthe"make"objectgenerator
      //(thisonerequirestospecifythepoint-type).
      model::d2::point_xy<double>p4=make<model::d2::point_xy<double>>(1,1);

      //5:forthed2::point_xy<...>typeonly:constructorwithtwovalues
      model::d2::point_xy<double>p5(1,1);

      //6:forboosttuplesyoucanofcourseusemake_tuple

      //Somewaysofgettingpointvalues

      //1:usingthe"get"functionfollowingtheconceptsbehind
      std::cout<<get<0>(p2)<<","<<get<1>(p2)<<std::endl;

      //2:forpoint_xyonly
      std::cout<<p2.x()<<","<<p2.y()<<std::endl;

      //3:usingboost-tuplesyouofcoursecanboost-tuple-methods
      std::cout<<pt3.get<0>()<<","<<pt3.get<1>()<<std::endl;

      //4:Boost.Geometrysupportsvariousoutputformats,e.g.DSV
      //(delimiterseparatedvalues)
      std::cout<<dsv(pt3)<<std::endl;

      //5.orwkt
      std::cout<<wkt(p4)<<(equals(p4,p5)?"equals":"don'tequals")<<wkt(p5)<<std::endl;

      //Thereare3-dimensionalpointstoo
      model::point<double, 3, cs::cartesian>d3a,d3b;
      assign_values(d3a,1,2,3);
      assign_values(d3b,4,5,6);
      d3=distance(d3a,d3b);

      //Otherexamplesshowothertypesofpoints,geometriesandmorealgorithms

      return0;
      }