首页 > 解决方案 > 如何使用Boost获得2D点的凸包面积?

问题描述

我是新手,我尝试的所有操作都会导致屏幕充满编译器错误。这是我最近的尝试:

#include <boost/geometry/algorithm/convex_hull.hpp>
#include <boost/geometry/algorithm/area.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/multi_point.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <boost/geometry/geometries/adapted/boost_tuple.hpp>

namespace bg = boost::geometry
BOOST_GEOMETRY_REGISTER_BOOST_TUPLE_CS(cs::cartesian)

double getHullArea(std::vector<double> x, std::vector<double> y) {
  typedef boost::tuple<double,double> point;
  typedef bg::model::multi_point<point> points;
  points p;
  for (size_t i=0; i<x.size(); i++) {
    bg::append(p, point(x[i],y[i]));
  }
  bg::model::polygon hull;
  bg::convex_hull(p, hull);
  return bg::area(hull);
}

我知道有一些聪明的事情可以避免多点,但我想了解我在使用这种基本方法时做错了什么。错误出现在convex_hull通话中,可能与我定义hull的方式有关。

编辑:添加了我在收到错误时使用的包含。

标签: c++boostboost-geometry

解决方案


问题在于包含。 <boost/geometry.hhp>必须包括在内。这有效:

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/multi_point.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <boost/geometry/geometries/adapted/boost_tuple.hpp>

namespace bg = boost::geometry
BOOST_GEOMETRY_REGISTER_BOOST_TUPLE_CS(cs::cartesian)

double getHullArea(std::vector<double> x, std::vector<double> y) {
  typedef boost::tuple<double,double> point;
  typedef bg::model::multi_point<point> points;
  points p;
  for (size_t i=0; i<x.size(); i++) {
    bg::append(p, point(x[i],y[i]));
  }
  bg::model::polygon hull;
  bg::convex_hull(p, hull);
  return bg::area(hull);
}

推荐阅读