fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <cstdlib>
  4. #include <algorithm>
  5. #include <map>
  6.  
  7. using namespace std;
  8.  
  9. struct POINTS
  10. {
  11. float xStart;
  12. float yStart;
  13. float zStart;
  14.  
  15. float xEnd;
  16. float yEnd;
  17. float zEnd;
  18. };
  19.  
  20. vector<POINTS> generate_lines(int n)
  21. {
  22. vector<POINTS> lines;
  23.  
  24. for (int i = 0; i < n; i++)
  25. {
  26. POINTS p;
  27.  
  28. p.xStart = i ? lines.rbegin()->xEnd : rand()/static_cast<float>(RAND_MAX);
  29. p.yStart = i ? lines.rbegin()->yEnd : rand()/static_cast<float>(RAND_MAX);
  30. p.zStart = i ? lines.rbegin()->zEnd : rand()/static_cast<float>(RAND_MAX);
  31.  
  32. p.xEnd = i == n-1 ? lines.begin()->xStart : rand()/static_cast<float>(RAND_MAX);
  33. p.yEnd = i == n-1 ? lines.begin()->yStart : rand()/static_cast<float>(RAND_MAX);
  34. p.zEnd = i == n-1 ? lines.begin()->zStart : rand()/static_cast<float>(RAND_MAX);
  35.  
  36. lines.push_back(p);
  37. }
  38.  
  39. random_shuffle(lines.begin(), lines.end());
  40.  
  41. for (auto &p : lines)
  42. if (rand()%2)
  43. {
  44. swap(p.xStart, p.xEnd);
  45. swap(p.yStart, p.yEnd);
  46. swap(p.zStart, p.zEnd);
  47. }
  48.  
  49. return lines;
  50. }
  51.  
  52. int main()
  53. {
  54. vector<POINTS> lines = generate_lines(10);
  55. multimap<tuple<double, double, double>, pair<double, double> > M;
  56.  
  57. for (int i = 0; i < lines.size(); i++)
  58. {
  59. // add start point
  60. M.insert(make_pair(make_tuple(lines[i].xStart, lines[i].yStart, lines[i].zStart),
  61. make_pair(i, 0) ) );
  62.  
  63. M.insert(make_pair(make_tuple(lines[i].xEnd, lines[i].yEnd, lines[i].zEnd),
  64. make_pair(i, 1) ) );
  65. }
  66.  
  67. auto coords1 = M.begin()->first;
  68. auto pos1 = M.begin()->second;
  69.  
  70. vector<bool> used(lines.size(), false);
  71.  
  72. for (int i = 0; i < lines.size(); i++)
  73. {
  74. std::tuple<double, double, double> coords2 = pos1.second ? make_tuple(lines[pos1.first].xStart, lines[pos1.first].yStart, lines[pos1.first].zStart) :
  75. make_tuple(lines[pos1.first].xEnd, lines[pos1.first].yEnd, lines[pos1.first].zEnd);
  76.  
  77. cout << pos1.first << ": " << get<0>(coords1) << " " << get<1>(coords1) << " " << get<2>(coords1) << " - "
  78. << get<0>(coords2) << " " << get<1>(coords2) << " " << get<2>(coords2) << endl;
  79.  
  80. used[pos1.first] = true;
  81.  
  82. pair<int, int> nextPos(-1, -1);
  83.  
  84. auto r = M.equal_range(coords2);
  85. for (auto it = r.first; it != r.second; it++)
  86. if (!used[it->second.first])
  87. {
  88. nextPos = it->second;
  89. break;
  90. }
  91.  
  92. if (nextPos.first == -1 && i != lines.size()-1)
  93. {
  94. cout << "something bad happens\n";
  95. return 1;
  96. }
  97.  
  98. pos1 = nextPos;
  99. coords1 = coords2;
  100. }
  101.  
  102. return 0;
  103. }
  104.  
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
1: 0.108809 0.998924 0.218257 - 0.840188 0.394383 0.783099
4: 0.840188 0.394383 0.783099 - 0.79844 0.911647 0.197551
8: 0.79844 0.911647 0.197551 - 0.335223 0.76823 0.277775
0: 0.335223 0.76823 0.277775 - 0.55397 0.477397 0.628871
6: 0.55397 0.477397 0.628871 - 0.364784 0.513401 0.95223
3: 0.364784 0.513401 0.95223 - 0.916195 0.635712 0.717297
5: 0.916195 0.635712 0.717297 - 0.141603 0.606969 0.0163006
2: 0.141603 0.606969 0.0163006 - 0.242887 0.137232 0.804177
9: 0.242887 0.137232 0.804177 - 0.156679 0.400944 0.12979
7: 0.156679 0.400944 0.12979 - 0.108809 0.998924 0.218257