以蛇形图案排序点坐标

Sort point coordinates in serpentine pattern

本文关键字:排序 坐标      更新时间:2023-10-16

在我的代码中,我计算了两条直线的交点。相交的点,如果它们存在,被保存在一个向量中。这个向量现在要排序,这样就形成了一个蛇形的结构。在这里解释一下我的函数的当前输出:

-2.8    -3.5
-2.6    -3.5
 3.1    -3.5
-3.8    -2.5
-3.6    -2.5
 3.8    -2.5
 4      -2.5
-4.3    -1.4
 4.4    -1.4
-4.5    -0.5
 4.6    -0.5
 4.7    -0.5
-4.5     0.5
 4.6     0.5
 4.7     0.5
-4.2     1.5
 4.4     1.5
-3.8     2.5
-3.6     2.5
 3.8     2.5
 3.9     2.5
 4       2.5
 2.9     3.5
 3.1     3.5
-2.8     3.5

第二列可能已经按升序排序,但现在必须对第一列进行排序,以便它们再次按升序和降序排序。预期的输出应该如下所示:

-2.8    -3.5
-2.6    -3.5
 3.1    -3.5
 4      -2.5
 3.8    -2.5
-3.6    -2.5
-3.8    -2.5
-4.3    -1.4
 4.4    -1.4
 4.7    -0.5
 4.6    -0.5
-4.5    -0.5
-4.5     0.5
 4.6     0.5
 4.7     0.5
 4.4     1.5
-4.2     1.5
-3.8     2.5
-3.6     2.5
 3.8     2.5
 3.9     2.5
 4       2.5
 3.1     3.5
 2.9     3.5
-2.8     3.5

实际输出如下所示:

 3.1    -3.5
-2.6    -3.5
-2.8    -3.5
-3.8    -2.5
-3.6    -2.5
 3.8    -2.5
 4      -2.5
 4.4    -1.4
-4.3    -1.4
-4.5    -0.5
 4.6    -0.5
 4.7    -0.5
 4.7     0.5
 4.6     0.5
-4.5     0.5
-4.2     1.5
 4.4     1.5
 4       2.5
 3.9     2.5
 3.8     2.5
-3.6     2.5
-3.8     2.5
 2.9     3.5
 3.1     3.5
-2.8     3.5

我的第一次尝试是这样的

bool ascending_first = false;
auto firstInRange = intersects.begin();
while(firstInRange != intersects.end()) {
    auto endInRange = 
    adjacent_find(firstInRange, intersects.end(), [](const std::array<double, 3>& a, const std::array<double, 3>& b) 
        {return a[1] != b[1]; });
    if (endInRange != intersects.end()) ++endInRange;
    std::sort(firstInRange, endInRange, [ascending_first](const std::array<double, 3>& a, const std::array<double, 3>& b)
        {return ascending_first ? a[0] < b[0] : b[0] < a[0] ;});
    ascending_first = ! ascending_first;
    firstInRange = endInRange;
}

这段代码的问题是,只有当两个点具有相同的y坐标时,它才有效。如果在相同的y坐标上找到更多的交点,则蛇形图案不能正确创建。谁能告诉我怎样才能达到预期的效果?

编辑:我刚刚又检查了我的代码。我发现排序函数不是问题所在。由于只有一个小数点后的数字,所以将双精度值四舍五入。我用了底函数。

for(size_t b=0; b<intersects.size(); b++)
{
    intersects[b][0]=intersects[b][0]*10;
    intersects[b][1]=intersects[b][1]*10;
    intersects[b][2]=intersects[b][2]*10;
    intersects[b][0]=floor(intersects[b][0]);
    intersects[b][1]=floor(intersects[b][1]);
    intersects[b][2]=floor(intersects[b][2]);
    intersects[b][0]=intersects[b][0]/10;
    intersects[b][1]=intersects[b][1]/10;
    intersects[b][2]=intersects[b][2]/10;
}
然而,在这里,小数似乎不完全匹配。我对这个提取尸体的方法是用排序函数来扩展一个范围的可能性。它将排序函数扩展到一个±0.1。但是我不知道如何处理我的代码
typedef pair<float, float> pff;
std::vector<pff> vp{
    {-2.6, -3.5}, {-2.8, -3.5}, {3.1, -3.5},  {-3.8, -2.5}, {-3.6, -2.5},
    {3.8, -2.5},  {4, -2.5},    {-4.3, -1.4}, {4.4, -1.4},  {-4.5, -0.5},
    {4.6, -0.5},  {4.7, -0.5},  {-4.5, 0.5},  {4.6, 0.5},   {4.7, 0.5},
    {-4.2, 1.5},  {4.4, 1.5},   {-3.8, 2.5},  {-3.6, 2.5},  {3.8, 2.5},
    {3.9, 2.5},   {4, 2.5},     {2.9, 3.5},   {3.1, 3.5},   {-2.8, 3.5}};
template <class T>
struct xx_than_key
{
    inline bool operator() (const pff &a, const pff &b) const
    {
        return T()(a.first, b.first);
    }
};
void sort()
{
    auto c = vp.begin();
    int b = true;
    for (auto p = vp.begin(); p < vp.end(); ++p)
    {
        if (p->second != c->second)
        {
            if (b)
            {
                std::sort(c, p, xx_than_key<std::less<float>>());
            }
            else
            {
                std::sort(c, p, xx_than_key<std::greater<float>>());
            }
            b = !b;
            c = p;
        }
    }
}