fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <iterator>
  5. using namespace std;
  6.  
  7. struct Point2D {
  8. int x,y;
  9. bool operator==(const Point2D& rhs) const { return x==rhs.x && y==rhs.y; }
  10. };
  11. using Color = int;
  12.  
  13. struct Polygon{
  14. vector<Point2D> vertex;
  15. Color color;
  16. };
  17.  
  18. ostream& operator<< (ostream& os, const Point2D& p) {
  19. return os<<"("<<p.x<<","<<p.y<<")";
  20. }
  21.  
  22. bool compare1(const vector<Point2D> &x, const vector<Point2D> &y ) {
  23. return x==y;
  24. }
  25. bool compare2(const vector<Point2D> &x, const vector<Point2D> &y ) {
  26. if (x.size() != y.size())
  27. return false;
  28. auto offset = find (y.cbegin(), y.cend(), x[0]); // asumes at least 1 point
  29. if (offset==y.cend())
  30. return false;
  31. return equal(offset, y.cend(), x.cbegin())
  32. && equal(y.cbegin(), offset, x.cbegin()+(y.cend()-offset));
  33. }
  34. bool compare3(const vector<Point2D> &x, const vector<Point2D> &y ) {
  35. if (x.size() != y.size())
  36. return false;
  37. auto offset = find (y.cbegin(), y.cend(), x[0]); // asumes at least 1 point
  38. if (offset==y.cend()) // no point in commont
  39. return false;
  40. else if (equal(offset, y.cend(), x.cbegin())
  41. && equal(y.cbegin(), offset, x.cbegin()+(y.cend()-offset)))
  42. return true;
  43. // not equal. And in reverse order ?
  44. auto roffset = make_reverse_iterator(offset+1);
  45. return equal(roffset, y.crend(), x.cbegin())
  46. && equal(y.crbegin(), roffset, x.cbegin()+(y.crend()-roffset));
  47. }
  48.  
  49.  
  50. int main() {
  51. vector<Polygon> v {
  52. { { {-1,0}, {0,1}, {1,0}, {0,-1}, {0,5}}, 105 },
  53. { { {-1,0}, {0,1}, {1,0}, {0,-1}}, 100 }, // same
  54. { { {0,1}, {1,0}, {0,-1}, {-1,0 }}, 101 }, // same but with diff starting
  55. { { {-1,0}, {0,-1}, {1,0}, {0,1}}, 102 }, // same in reverse order
  56. { { {0,-1}, {1,0}, {0,1}, {-1,0}}, 104 }, // same in revers with diff starting
  57. { { {-2,0}, {0,-2}, {2,0}, {0,2}}, 103 }
  58. };
  59.  
  60. Polygon p { { {0,1}, {1,0}, {0,-1}, {-1,0 }}, 1110 };
  61.  
  62. auto f = find_if (v.begin(), v.end(), [&p](const auto &x) { return compare3(x.vertex, p.vertex); });
  63. if (f!=v.end()) {
  64. cout << "Found at "<< f-v.begin() <<endl;
  65. }
  66. else cout << "Not found";
  67.  
  68. return 0;
  69. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
Found at 1