2016-08-22 6 views
0

私のコードでは、2つの直線の交点を計算します。それらが存在する交点はベクトルに保存されます。このベクトルは、蛇行状の構造が形成されるようにソートする必要があります。私の機能の現在の出力いったんここで説明するには、次の蛇行パターンのソートポイント座標

-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 

acutual出力は次のようになります。

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; 
} 

のように見えました同じ2つの点に同じy座標が与えられている場合にのみ機能します。同じy座標を持つ交差点がさらに見つかった場合、蛇行パターンは正しく作成されません。 誰も私はまだ希望の結果を得ることができる方法についてのヒントを与えることができますか?

編集:私のコードをもう一度チェックしました。ソート機能が問題ではないことが分かりました。私は小数点以下1桁の数字しか持っていないので、二重の値を丸めます。このため私は床機能を使用しました。

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; 
} 

ただし、10進数が正確に一致しない場合があります。この私のアプローチは、範囲の可能性を広げるソート機能となるだろう。拡張するには、ソート機能を±0.1にします。私は自分のコードをどうすればいいのか分かりません。

+0

期待する出力を提供できますか? (複数の出力が可能ですか?) – Jarod42

+0

あなたのコードは、サンプルの正しい出力を提供します( 'ascending_first'の初期値を反転してください)。[Demo](http://ideone.com/zyR72X)出力が期待されています。 – Jarod42

答えて

0
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; 
     } 
    } 
}