Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) BoostGeometryExample3

 

BoostQt CreatorLubuntu

 

Boost.Geometry example 3: IsConvex: determine if a 2D polygon is convex or concave is a Boost.Geometry example.

 

IsConvex is maintained in CppGeometry. See CppGeometry for the heavily used, debugged and tested version of IsConvex

Technical facts

 

Application type(s)

Operating system(s) or programming environment(s)

IDE(s):

Project type:

C++ standard:

Compiler(s):

Libraries used:

 

 

 

 

 

Qt project file: ./CppBoostGeometryExample3/CppBoostGeometryExample3.pro

 

exists(../../ConsoleApplication.pri) {
  include(../../ConsoleApplication.pri)
}
!exists(../../ConsoleApplication.pri) {
  QT += core
  QT += gui
  greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
  CONFIG   += console
  CONFIG   -= app_bundle
  TEMPLATE = app
  CONFIG(release, debug|release) {
    DEFINES += NDEBUG NTRACE_BILDERBIKKEL
  }
  QMAKE_CXXFLAGS += -std=c++11 -Wall -Wextra -Weffc++
  unix {
    QMAKE_CXXFLAGS += -Werror
  }
}

exists(../../Libraries/Boost.pri) {
  include(../../Libraries/Boost.pri)
}
!exists(../../Libraries/Boost.pri) {
  win32 {
    INCLUDEPATH += \
      ../../Libraries/boost_1_55_0
  }
}

SOURCES += main.cpp

 

 

 

 

 

./CppBoostGeometryExample3/main.cpp

 

#include <cassert>
#include <iostream>

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
#pragma GCC diagnostic ignored "-Wunused-variable"
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#pragma GCC diagnostic pop

bool IsConvex(boost::geometry::model::polygon<boost::geometry::model::d2::point_xy<double>> polygon)
{
  boost::geometry::correct(polygon);
  boost::geometry::model::polygon<boost::geometry::model::d2::point_xy<double>> hull;
  boost::geometry::convex_hull(polygon, hull);
  return boost::geometry::area(hull) == boost::geometry::area(polygon);
}

int main()
{
  typedef boost::geometry::model::d2::point_xy<double> Coordinat2D;
  /* Polygons used:

  0123456789012    0123456789012
0+------------   0+------------
1|               1|
2| A---------B   2| A---------B
3| E        /    3|  \   D   /
4|  \      /     4|   \ / \ /
5|   D----C      5|    E   C
  |                |

     Convex           Concave

  */

  //Convex shape
  {
    const std::vector<Coordinat2D> points {
      { 2.0, 2.0}, //A
      {12.0, 2.0}, //B
      { 9.0, 5.0}, //C
      { 4.0, 5.0}, //D
      { 2.0, 3.0}  //E
    };

    boost::geometry::model::polygon<Coordinat2D> polygon;
    boost::geometry::append(polygon, points);
    assert(IsConvex(polygon));
  }
  //Concave shape
  {
    const std::vector<Coordinat2D> points {
      { 2.0, 2.0}, //A
      {12.0, 2.0}, //B
      { 9.0, 5.0}, //C
      { 7.0, 3.0}, //D
      { 5.0, 5.0}  //E
    };

    boost::geometry::model::polygon<Coordinat2D> polygon;
    boost::geometry::append(polygon, points);
    assert(!IsConvex(polygon));
  }
}

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict

This page has been created by the tool CodeToHtml