C ++中的多边形

时间:2016-09-06 12:18:25

标签: c++ boost

假设我在2D空间中有多个多边形,如下图所示:

enter image description here

每个多边形用x-y坐标列表表示。例如,Polygon A可以写成PolygonA:{(3,4),(7,8),...,(3,4)}。在生成所有多边形之后,我们希望对它们执行一些操作。例如,我们想要知道矩形框内的所有多边形。或者对于某个多边形,我们想知道它的相邻多边形。这是我的问题:如何组织数据结构,以便对多边形的操作变得更加容易和有效。最直接的方法是将所有多边形放在矢量容器中。但对它们的操作效率低下。关于适当的数据结构或库的任何想法?

2 个答案:

答案 0 :(得分:0)

现在这只是一个提示! 我会以不同的方式做到这一点。它(正如你已经说过的) way 使用整个多边形操作太不可思议了。经常使用的帮助是将多边形分成更小(更容易计算)的形状。

我们假设您有一个多边形。enter image description here

再次,每个坐标操作..没办法。 让我们假设你有一个算法可以根据强度因子"返回多种形状。它可能看起来像这样。

std::vector<Shape> fill_with_shapes(const Polygon &a, float intensity); //example

强度越大,细节越多。 enter image description here

和(如果你想的话)甚至更多。enter image description here

使用矩形和三角形计算比使用整个多边形更简单。现在,我没有这样一个&#39; fill_with_shapes&#39;的代码。功能,再一次,这个答案更多是之前的建议你做任何操作..我希望这个想法有所帮助!

答案 1 :(得分:0)

我考虑使用boost :: geometry库中的r-tree,这似乎是一个很好的解决方案。我发布的代码如下:

#include<stdio.h>

#include <boost/geometry.hpp>
#include <boost/geometry/extensions/index/rtree/rtree.hpp>
#include <vector>


#include <boost/geometry.hpp>
#include <boost/geometry/extensions/index/rtree/rtree.hpp>

#include <random>

#include <iostream>
#include <exception>
#include <boost/foreach.hpp>

int main(void)
{
    namespace bg = boost::geometry;
    namespace bgi = boost::geometry::index;

    typedef bg::model::point<float,2,bg::cs::cartesian> point;
    typedef bg::model::box<point> box;

    typedef bgi::rtree<box,unsigned > rtreeType;

    rtreeType rtree(6,3);

    for(unsigned i=0; i<10; i++)
    {
        box b(point(i+0.1f,i+0.1f),point(i+0.5f,i+0.5f));
        rtree.insert(b,i);
    }

    box query_box(point(0,0),point(5,5));

    rtree.print();

    std::cout<<"number of elements: ";
    std::cout<<rtree.elements()<<std::endl;


    // application 1: search for boxes that are within query_box
    std::deque<unsigned> &boxValue=rtree.find(query_box);





    return 0;
}