Advertisement
encoree1996

Merge adjacent points

Oct 22nd, 2015
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.06 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5.  
  6.  
  7.  
  8. struct Vector
  9. {
  10.     float x,y,z;
  11.     Vector() : Vector(0,0,0) {}
  12.     Vector(float a, float b, float c) : x(a), y(b), z(c){}
  13.  
  14.     Vector operator/(float var)
  15.     {
  16.         return var == 0? Vector(0,0,0): Vector(x/var,y/var,z/var);
  17.     }
  18.  
  19.     Vector operator-(const Vector& input)
  20.     {
  21.         return Vector(x-input.x, y-input.y, z-input.z);
  22.     }
  23.  
  24.     float Length()
  25.     {
  26.         return sqrt(x*x+y*y+z*z);
  27.     }
  28. };
  29.  
  30. struct coord
  31. {
  32.     float x,y,z;
  33.     Vector ToVector()
  34.     {
  35.         return Vector(x,y,z);
  36.     }
  37.     coord() : coord(Vector(0,0,0)) {}
  38.     coord(float a, float b, float c) : x(a), y(b), z(c) {}
  39.     coord(Vector d) : x(d.x), y(d.y), z(d.z){}
  40.     bool operator<(const coord& input)
  41.     {
  42.         if(x != input.x)
  43.             return x < input.x;
  44.         else if(y != input.y)
  45.             return y < input.y;
  46.         else return z < input.z;
  47.     }
  48. };
  49.  
  50. std::ostream& operator<<(std::ostream& os, const coord& input)
  51. {
  52.     os<<input.x<<" "<<input.y<<" "<<input.z;
  53.     return os;
  54. }
  55.  
  56. std::ostream& operator<<(std::ostream& os, const Vector& input)
  57. {
  58.     os<<input.x<<" "<<input.y<<" "<<input.z;
  59.     return os;
  60. }
  61.  
  62.  
  63. int main()
  64. {
  65.     std::vector<coord> positions;
  66.     coord tmp;
  67.     while(std::cin>>tmp.x>>tmp.y>>tmp.z)
  68.         positions.push_back(tmp);
  69.  
  70.     std::cout<<"Points before: "<<positions.size()<<std::endl;
  71.  
  72.     for(int i=0;i<positions.size()-1;i++)
  73.     {
  74.         auto beg = positions[i].ToVector();
  75.         for(int j = i+1; j < positions.size(); j++)
  76.         {
  77.             if((beg-positions[j].ToVector()).Length() < 50)
  78.             {
  79.                 positions[j].x = 0.0f;
  80.                 positions[j].y = 0.0f;
  81.                 positions[j].z = 0.0f;
  82.            }
  83.         }
  84.         auto it = std::remove_if(positions.begin(),positions.end(),[](coord& input){return input.x == 0.0f && input.y == 0.0f && input.z == 0.0f;});
  85.         positions.erase(it, positions.end());
  86.     }
  87.     std::cout<<"Points after: "<<positions.size();
  88.     return 0;
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement