views:

63

answers:

3
A: 

Its perfectly possible. I am assuming you're using Java2d for this. You have find a method in it called as intersects. Using that you can do this.

You might have to modify this implementation of polygon and write one more intersects method which passes a Line2D object and customize it so that it passes an array polygons (possible since the same line cut can produce infinite polygons - assume a zigzag polygon) or null.

Bragboy
Thanks for the response, but I don't know how I would even go about doing that ( the math involved ). It might have to do with the Line intersecting each of the Polygon's lines. ( and splitting the shape like that ).
PickleMan
+2  A: 

It's possible, but if the polygon is not convex then splitting it across a line potentially leads to more than two polygons.

Traverse the polygons edges, and for each edge determine whether it intersects the line. Find the first such edge which does so. Continue traversing until you find another such edge, but add each you encounter along the way to a new polygon (including the part of the first edge which was split by the line which is on "this side" and likewise for the last edge). Finally, add a closing edge to the new Polygon. Now continue processing edges - on the other side of the line, creating another Polygon in the same manner. You now have two polygons split across the line. The same technique will work, if you are careful, with splitting a non-convex polygon into multiples polygons.

Beware of corner cases such as the line crossing a vertex of the polygon, and the line not intersecting the polygon at all.

davmac
Thanks. That's probably what I'll do as soon as I get the time
PickleMan
A: 

All you need is a polygon clipping alogrithem. You can see an overview here: Polygon clipping I think there are penty implementations out there where you can learn from.

InsertNickHere