Vector removal


  • QA Engineer

    Got it. int The vector, the elements with the plain indexes of which contain the coordinates x of the nearest points, and the inaccurate coordinates from the same points. The co-ordinates are either 0 or 1, i.e. the point(0.3)(1.3) neighbours and (1.2),(3,2), no (--- see example*).

    Example: 1,1,1,2,2,2,2,1 - Point vector (1,1),(1,2),(2,2),(2,1)the coordinates x and y of the following points are different from those of 0 or 1; the figure obtained is a square; it could be either 11122221or 12222111 - It's important that points go straight without repeating.

    Well, if we've got a vector, for example. 1,1,2,2,3,3,4,2,5,1,4,1,3,1,2,1i.e. triangle on points (1,1),(2,2),(3,3),(4,2),(5,1),(4,1),(3,1),(2,1) with summit (1,1),(3,3),(5,1)I'd like to get a vector from the original vector with these three peaks, that is. 1,1,3,3,5,1 , i.e. remove intermediate pointswhich "no role" from the vector (for any figure, not just a triangle).

    Here's my code to remove these points:

    i=0; // текущий индекс 
    while (i<v.size()-1) // до последней координаты x
    {
        int prevX=(!i ? v[v.size()-2] : v[i-2]),nextX=(i==v.size()-2 ? v[0] : v[i+2]),prevY=(!i ? v[v.size()-1] : v[i-1]),nextY=(i==v.size()-2 ? v[1] : v[i+3]); // получаем предыдущие и следующие координаты
        if (v[i]==(prevX+nextX)/2 && v[i+1]==(prevY+nextY)/2){ // если условие удаления выполняется
            v.erase(v.begin()+i,v.begin()+i+2); // вот здесь не уверен, пробовал два удаления подряд - безрезультатно
            vector<int>(v).swap(v); // очистка памяти
        } else i+=2; // продвигаем индекс на следующую координату x (при удалении индекс сам "продвигается")
    }
    

    The point is that the coordinates of the current point are checked (in the vector) v[i](d) Mean arithmetic for the previous v[i-2] and follow-up v[i+2] coordinates x. Same for y.

    He doesn't work, I suspect it's a mistake in the distance itself. Please indicate the error/and.



  • Remove the divide, write 2*v[i]==. ♪ ♪


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2