Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/151.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.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++_Sorting_Coordinates - Fatal编程技术网

C++ 蛇形图案中的排序点坐标

C++ 蛇形图案中的排序点坐标,c++,sorting,coordinates,C++,Sorting,Coordinates,在我的代码中,我计算两条直线的交点。存在交点的位置将保存在向量中。现在对该向量进行排序,以形成蛇形结构。要在此处解释my函数的当前输出,请执行以下操作: -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

在我的代码中,我计算两条直线的交点。存在交点的位置将保存在向量中。现在对该向量进行排序,以形成蛇形结构。要在此处解释my函数的当前输出,请执行以下操作:

-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
实际输出如下所示:

-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;
}
bool升序_first=false;
auto firstInRange=相交。开始();
while(firstInRange!=intersects.end()){
自动结束范围=
相邻的查找(firstInRange,intersects.end(),[](常量std::array&a,常量std::array&b)
{返回a[1]!=b[1];});
如果(endInRange!=intersects.end())+endInRange;
std::sort(firstInRange,endInRange,[先升](常数std::array&a,常数std::array&b)
{先返回升序_?a[0]
此代码的问题在于,它仅在两个点具有相同的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;
}
用于(尺寸b=0;b
typedef对pff;
向量{
{-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}};
模板
结构xx_比_键
{
内联布尔运算符()(常数pff&a、常数pff&b)常数
{
返回T()(a.first,b.first);
}
};
空排序()
{
自动c=vp.begin();
int b=真;
对于(自动p=vp.begin();p秒!=c->秒)
{
如果(b)
{
排序(c,p,xx_-than_-key());
}
其他的
{
排序(c,p,xx_-than_-key());
}
b=!b;
c=p;
}
}
}

您能提供预期输出吗?(可以提供多个输出吗?)您的代码为样本提供了正确的输出(首先将
升序的初始值反转),因此您可以提供没有预期输出的示例。