Cut polygon with a line in Java [closed]
Can you share some code in Java to cut polygon with a line or path?
I was tackling with this kind of a problem while developing some sort of a 'page curl' implementation for Android. While the code isn't necessarily (I'll add link later on) as readable as one could hope for, here's briefly the idea I started with. Which possibly extends to path but I was dealing with a line only.
- You have a set of vertices presenting a polygon.
- Given a cut line, you have a slope for it.
- Choose any point on that cut line.
- Based on information 2. and 3. translate original vertices so that point you chose from the line becomes origin. And after that rotate vertices so that the cut line becomes vertical. Or horizontal if you prefer doing testing on y -axis instead.
- After previous transformation you have a setup in which your original cut line is y -axis at x=0.
- At this point it's relatively easy to do calculate intersections as all you have to do is check if two vertices creating a line are on opposite sides of y -axis. Namely, other one has positive x, and the other one negative one. And it isn't very difficult to calculate the exact point were this line crosses x=0. It has x=0, you have to find y only.
- Do the exact transform for this intersection point you did earlier, backwards, and you have a cut point on original polygon.
There are most definitely more sophisticated methods for calculating intersections for a polygon but given the nature of the project I was working with - I really enjoyed having a bit more simplified environment. Also take this with a grain of salt as I didn't find this anywhere but it's more of a idea I came up by myself.
And the code as promised.
精彩评论