Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/139.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 用对向量提升几何区域?_C++_Boost_Boost Geometry - Fatal编程技术网

C++ 用对向量提升几何区域?

C++ 用对向量提升几何区域?,c++,boost,boost-geometry,C++,Boost,Boost Geometry,有没有办法让boost::geometry::area使用成对向量?它在这里说,它应该与多点。因此,我调用了BOOST\u GEOMETRY\u REGISTER\u MULTI\u POINT——它适用于凸面外壳,但不适用于区域。或者我必须创建一个多边形并将点附加到它,如图所示: 这是我的代码: #include <iostream> #include <cstdlib> #include <boost/geometry.hpp> #include &l

有没有办法让
boost::geometry::area
使用成对向量?它在这里说,它应该与多点。因此,我调用了
BOOST\u GEOMETRY\u REGISTER\u MULTI\u POINT
——它适用于
凸面外壳
,但不适用于
区域
。或者我必须创建一个多边形并将点附加到它,如图所示:

这是我的代码:

#include <iostream>
#include <cstdlib>

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

BOOST_GEOMETRY_REGISTER_POINT_2D(decltype(std::pair<double, double>{}), double, cs::cartesian, first, second)
BOOST_GEOMETRY_REGISTER_MULTI_POINT(decltype(std::vector<std::pair<double, double>>{}))

using MultiPoint = std::vector<std::pair<double, double>>;
MultiPoint getHull(const MultiPoint& points)
{
  MultiPoint hull{};
  boost::geometry::convex_hull(points, hull);
  return hull; // This returns a vector of pairs of points respresenting a hull
}
double getHullArea(const MultiPoint& points)
{
  return boost::geometry::area(points); // This always return 0
}

int main()
{
    // Unit square
    auto points = MultiPoint{{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0,0}};
    auto hull = getHull(points); // This works as expected
    auto area = getHullArea(hull); // This always return 0
}
#包括
#包括
#包括
#包括
#包括
BOOST\u GEOMETRY\u REGISTER\u POINT\u 2D(decltype(std::pair{})、double、cs::cartesian、first、second)
BOOST\u几何\u寄存器\u多点(decltype(std::vector{}))
使用MultiPoint=std::vector;
多点getHull(常数多点和点)
{
多点外壳{};
几何体:凸面外壳(点,外壳);
return hull;//返回表示外壳的点对向量
}
双getHullArea(常数多点和点)
{
返回boost::geometry::area(points);//此值始终返回0
}
int main()
{
//单位平方
自动点=多点{{0,0},{0,1},{1,1},{1,0},{0,0};
auto-hull=getHull(points);//这可以正常工作
auto area=getHullArea(外壳);//此值始终返回0
}

是,它支持多点。你会得到记录在案的:

所以,你得到的面积是0,就像你应该期待的那样

显然,您希望将其调整为面积几何体。让我们假设你们的配对是a组

如您所见,我添加了一个更有趣的示例:


非常感谢您!这是有道理的。谢谢你的耐心,sehe:)
using Point = std::pair<double, double>;
using Ring  = std::vector<Point>;

BOOST_GEOMETRY_REGISTER_POINT_2D(Point, double, cs::cartesian, first, second)
BOOST_GEOMETRY_REGISTER_RING(Ring)
#include <iostream>
#include <cstdlib>

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/register/point.hpp>
#include <boost/geometry/geometries/register/ring.hpp>
namespace bg = boost::geometry;

using Point = std::pair<double, double>;
using Ring  = std::vector<Point>;

BOOST_GEOMETRY_REGISTER_POINT_2D(Point, double, cs::cartesian, first, second)
BOOST_GEOMETRY_REGISTER_RING(Ring)

Ring getHull(const Ring& points) {
    Ring hull;
    bg::convex_hull(points, hull);
    return hull;
}

double getArea(const Ring& points) {
    return bg::area(points);
}

int main() {
    for (Ring points : {
             Ring{{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}},
             Ring{{0, 0}, {0, 2}, {3, 2}, {3, 1}, {1, 1}, {1, 0}, {0, 0}},
         })
    {
        std::cout << "-----\nInput:\t" << bg::wkt(points) << "\n";
        if (std::string reason; !bg::is_valid(points)) {
            std::cout << "Input:\t" << reason << "\n";
            bg::correct(points);
            std::cout << bg::wkt(points) << "\n";
        }

        std::cout << "Hull:\t"      << bg::wkt(getHull(points)) << "\n";
        std::cout << "Area:\t"      << getArea(points)          << "\n";
        std::cout << "Hull Area:\t" << getArea(getHull(points)) << "\n";
    }
}
-----
Input:  POLYGON((0 0,0 1,1 1,1 0,0 0))
Hull:   POLYGON((0 0,0 1,1 1,1 0,0 0))
Area:   1
Hull Area:      1
-----
Input:  POLYGON((0 0,0 2,3 2,3 1,1 1,1 0,0 0))
Hull:   POLYGON((0 0,0 2,3 2,3 1,1 0,0 0))
Area:   4
Hull Area:      5