c++11 - sorting elements in a vector of curves -
i have class point represents x , y coordinate , class curve have 2 point, start point , end point.
class point { public: double x{0.0}, y{0.0}; //......... } class curve { public: point start, end; //......... }
i have vector of curves, needs sorted. start point of 1 curve equals end point of other. output curve (keeping 1 curve after other) can open curve or close curve(always continuous curve).
current logic lot of loops , 2/3 vectors.. there way implement same using standard algorithms(c++11).
assuming first element of vector starting point of path , there 1 solution, following lines job:
bool operator!=(point& a,point& b) { return !(a.x == b.x && b.y == a.y); } bool operator==(point& a, point& b) { return (a.x == b.x && b.y == a.y); } void order(std::vector<curve>& vin) { auto = vin.begin(); auto end = vin.end(); while (it+1 != end) { if (it->end != (it + 1)->start) { std::swap(*(it + 1), *std::find_if(it + 2, end, [it](curve& c){ return c.start == it->end ; })); } ++it ; } }
if need find first element, define predicate is_the_beginning , similar call swap before loop:
bool is_the_beginning(curve& c) { if ( ... ) return true; else return false ; } std::swap(*it, *std::find_if(it+1, end, is_the_beginning ) ) ;
maybe need take account precision of double
operator ==
, !=
. can replace them functions
Comments
Post a Comment