c++ - 如何使用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的方式有关。
编辑:添加了我在收到错误时使用的包含。
解决方案
问题在于包含。 <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);
}
推荐阅读
- javascript - 如何让我的排序函数调用比较函数
- python - EasyEnsembleClassifier 的高召回率、低精度
- python - 在寻求加速 Python 时,有没有更好的方法来使用 cython?
- python - 使用系列填充数据框的值
- c++ - 带有继承的前向声明并包含 | 不完整的类型或预期的类名
- python - 用 Python 抓取网站,我怎么知道在 html 中引用的位置?
- qt - Qt 5.11.3 - QML - treeView - 根据 itemDelegate 属性调整行高
- typescript - 递归/循环类型的交集
- php - PHP ImageMagick 返回不正确的内容类型
- javascript - 如何获得按键IE8-的符号?