Let’s say I have a (convex) polygon with vertices 0..n-1. I want to split this polygon in half, say, between verticies i and j. Vertices i and j should appear in both polygons.
As far as I can tell, there are only two cases. One where i < j, or when i > j. i is never equal to j (nor are they ever adjacent).
I’m storing my vertices like vector<Point> poly. You can assume Point is just a basic struct with two doubles x and y, with points indexed sequentially in CCW order.
If i < j, then I just need to copy the vertices from i to j (inclusive) into one vector, and then from j to n-1 plus 0 to i into another. Or vice versa for the other case, right?
Here’s the code I’m using, but it doesn’t seem to work right (let j == closestIndex):
if (i < closestIndex) { lowerPoly.insert(lowerPoly.end(), poly.begin() + i, poly.begin() + closestIndex + 1); upperPoly.insert(upperPoly.end(), poly.begin() + closestIndex, poly.end()); upperPoly.insert(upperPoly.end(), poly.begin(), poly.begin() + i + 1); } else { lowerPoly.insert(lowerPoly.end(), poly.begin() + i, poly.end()); lowerPoly.insert(lowerPoly.end(), poly.begin(), poly.begin() + closestIndex + 1); upperPoly.insert(upperPoly.end(), poly.begin() + closestIndex, poly.begin() + i + 1); }
As a side note:
You don’t really have 2 cases. If i > j then just swap i and j. Then you are always in the case where i < j, assuming i != j.
I would probably code it like follows: