I’m writing an algorithm that iterates over a list of points, calculates the distance between them and inserts additional points if the distance is too great. However I seem to be lacking the proper familiarity with STL to come up with an elegant solution. I’m hoping that I can learn something, so I’ll just show you my code. You might have some hints for me.
for (std::list<PathPoint>::iterator it = ++points_.begin();
it != points_.end(); it++)
{
Vector curPos = it->getPosition();
Vector prevPos = (--it)->getPosition();
Vector vecFromPrev = curPos - prevPos;
float distance = vecFromPrev.abs();
it++;
if (distance > MAX_DISTANCE_BETWEEN_POINTS)
{
int pointsToInsert = (int)(distance / MAX_DISTANCE_BETWEEN_POINTS);
Vector curPos = prevPos;
for (int i = 0; i < pointsToInsert; i++)
{
curPos += vecFromPrev / pointsToInsert;
it = points_.insert(it, PathPoint(curPos, false));
it++;
}
}
}
Consider using
adjacent_findto find an iterator position where the distance between consecutive elements is too large, then insertingpointsToInsertitems.http://www.sgi.com/tech/stl/adjacent_find.html
In addition, you could use
generatewith a functor to fill in the intermediate points.http://www.sgi.com/tech/stl/generate.html
Not sure how deep you want to go into STL 🙂