views:

78

answers:

1

I have a simple polygon (convex or concave, but no holes) that I need to slice into parts with a line segment. I'm not sure how to actually determine how many polygons result after the slice, or how to group the vertices.

Basic convex cases the always results in 2 sub-polygons are easy, but how would I deal with a complicated concave shape? Take an "E" shape polygon for example. A vertical slice can yield 4 polygons. How can I determine which vertices make up each one of those sub-polygons?

Defining the Polygon: I have two options here. My polygon can be an ordered list of vertices, or it can be an array of triangles. I would prefer a solution that uses the array of triangles. It should be pretty easy to loop through every triangle and slice it with the line if they intersect. But then I have no idea how to group those triangles into the sub-polygons that result.

Pseudo-code or even general advice is good; a C# implementation is ideal.

+1  A: 

A while back I gave this answer to a somewhat different question.

That answer gives a way of establishing an outline of a shape, given a triangular decomposition of that shape.

The basic idea is to consider the edges of all the triangles as directed vectors, and then cancel out equal but opposite edges.

In your case, you would have a bunch of triangles representing the original shape. You would slice the individual triangles with the line. You would then regather the triangles into shapes using the method outlined above, with the proviso that sliced edges would not cancel.

There are details and pictures in the answer referred to above. But to summarize the steps would be

  1. Perform the triangle splits

  2. For each resulting triangle, add the three directed edges to a set. To determine clockwise order, check out the the answers to this question.

  3. Go through the edge set removing pairs of edges that are are equal but opposite (unless they are slice edges)

  4. Pick an edge in the set, then find the edge whose tail matches the head of the first edge. Then repeat for that edge, until you get to the beginning edge. Remove each edge from the edge set as you get to it. Whenever you get to the beginning edge you have a closed loop representing one of the result polygons.

  5. Perform Step 4 until there are no edges left.

All of this accommodates your wish to start from a triangulation of your polygon. But as one of the commenters to your original question has pointed out, you may want to consider the alternatives given at http://stackoverflow.com/questions/1775457/generate-new-polygons-from-a-cut-polygon-2d.

brainjam
Nice approach. I'm still a bit lost about implementing it though. Can you provide a few specifics? How would the loop logic run? 1) Take random triangles A and B. 2) Match up each side of A with B. 3) If any side A + any side B = zero, remove the side? But how do I remove a side when we're dealing with pionts? 4) Now I have a square C. Do I now loop again by taking the union of square C with random triangle D? And then how to I move on to the second sub-polygon?
Liquid
@Liquid, what do you mean by loop logic?
brainjam
I have to actually write a loop that runs through every triangle in my array and makes all the comparisons you suggest. I need to ultimately split the single triangle array into however many polygons result after the slice.
Liquid
@Liquid, I've added some more detail. You loop through the polygons to create an edge set, then run through the edge set to get the resultant polygons.
brainjam
Thanks! I guess my only question now is, how do I convert triangle data into 3 counter-clockwise edges? I have two arrays, one with x,y vertex coordinates of each polygon point. Then I have an array of triangles, each one being a 3-part designation that maps to the vertex array. Example: Tri[0,1,2] would specify vertex 1, 2, and 3 on the polygon, each vertex being a Vecto2 with x and y components. How do I convert that into counter-clockwise edges for doing the edge comparison?
Liquid
@Liquid, I've updated Step 2.
brainjam