计算c ++

时间:2015-06-26 15:57:04

标签: c++11 vector point

我有一个存储坐标点列表的向量。我想计算每个点在定义的容差范围内的出现次数。 假设X和Y值的公差极限定义为10。并且向量包含{(100,200),(110,205),(115,215),(120,220),(100,200),(150,160)}。现在我想要的输出是 (100,200)3 (110,205)4 (115,215)3 (120,220)2 (150,160)1

第一个坐标计数3包括(100,200),(110,205){原因是值在100 + - 10和200 + - 10}范围内和(100,200) 效率是高优先级

1 个答案:

答案 0 :(得分:0)

我使用boost发布此问题的解决方案。我在这里发布代码,以防将来有人需要类似的解决方案。

    #include<set>
    #include <iostream>
    #include <boost/geometry.hpp>
    #include <boost/geometry/geometries/point.hpp>
    #include <boost/geometry/index/rtree.hpp>

    using namespace std;
    namespace bg = boost::geometry;
    namespace bgi = boost::geometry::index;

    typedef bg::model::point<int, 2, bg::cs::cartesian> point;
    typedef std::pair<point, unsigned> value;

  struct ltstr
  {
bool operator()(const point &p1, const point &p2) const
{
    return (p1.get < 0 >() < p2.get < 0 >() || p1.get < 1 >() < p2.get < 1 >());
}
};


void main()
  {
vector<point> candidatePoints{ point(457, 184), point(457, 184), point(457, 184), point(457, 184), point(457, 184),
    point(456, 184), point(456, 184), point(456, 184), point(456, 184), point(456, 184),
    point(456, 184), point(457, 184), point(457, 184), point(457, 184), point(458, 184), point(459, 185) };

bgi::rtree< value, bgi::quadratic<16> > rtree;

set<point, ltstr> uniqueCandidatePoints;

for (int i = 0; i < candidatePoints.size(); ++i)
{
    int x = candidatePoints[i].get < 0 >();
    int y = candidatePoints[i].get < 1 >();
    uniqueCandidatePoints.insert(point(x, y));
    rtree.insert(make_pair(candidatePoints[i], i));
}

for (auto it = uniqueCandidatePoints.begin(); it != uniqueCandidatePoints.end(); ++it)
{
    std::vector<value> returnedValues;
    point currentItem = *it;
    rtree.query(bgi::satisfies([&](value const& v) {return bg::distance(v.first, currentItem) < 5; }),
        std::back_inserter(returnedValues));

    cout << "Current Item: " << currentItem.get < 0 >() << "," << currentItem.get < 1 >() << "Count: " << returnedValues.size() << endl;
} 

getchar();

}