以蛇形图案排序点坐标
Sort point coordinates in serpentine pattern
在我的代码中,我计算了两条直线的交点。相交的点,如果它们存在,被保存在一个向量中。这个向量现在要排序,这样就形成了一个蛇形的结构。在这里解释一下我的函数的当前输出:
-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;
}
}
}
相关文章:
- 二叉排序树无法编译
- QGraphicsPolygonItem在拖动时未更新QPolygonF坐标
- 仅使用绝对值对数组进行排序,并在C++中显示实际值
- 使用C++中的链表对与原点的距离、x 和 y 坐标进行排序
- 对坐标结构的向量进行排序
- 如何按坐标 x 或坐标 y 对 QGraphicsitem 的 QList 进行排序
- 在 c++ 中对插值的坐标进行排序和搜索
- 对坐标 c++ 的向量进行排序
- 根据点的坐标对点进行排序
- 如何按距点的距离(x1,y1)升序对一组坐标进行排序
- 对抛物线的坐标 (x,y) 进行排序 y=ax^2+bx+c x=x1,x2,x3, x4.根据 y 坐标
- 坐标的排序向量C++
- C++对指针向量进行排序,其中对象表示矩阵坐标
- 坐标的排序
- Make_heap和排序x和y坐标
- 基于 x 和 y 坐标进行排序
- 排序坐标从左上到右下
- 以蛇形图案排序点坐标
- 如何使用std::sort按x坐标对结构进行排序
- 排序屏幕坐标