问题
today I have a (simple) rendering problem for you. My current project gets datas from a file to generate a SVG file. Drawing things as polygon is pretty easy thanks to the SVG format, but I have a single problem: some of my polygons are in AND out of the page (meaning that some parts of them are displayed while the rest is not shown due to the fact they are out of the display limit). In order to optimize the final SVG file I need to reduce my polygon to a simpler form.
Consider the grey rectangle as my page. Consider the green polygon as the thing I actually draw.
First picture shows you the thing that I actually have while the second picture shows you the final result I want to have.
First I thought to reduce my polygon in simple triangles in order to only draw points in the display limits. But I think a simpler solution exists... if you have it do not hesitate to share it with me :)EDIT:
I have this tricky case to handle as well :
Thank you.
回答1:
- Clipping a polygon with a rectangle. We reduce this problem to
- clipping a polygon with a line. We reduce this to an even simpler problem:
- clipping one edge of a polygon with a line. Which is really just
- Finding the intersection of a line segment with a line (if it exists).
The last problem is pretty easy, considering that your lines are vertical or horizontal. Is that enough?
来源:https://stackoverflow.com/questions/6243411/clipping-polygon-against-rectangle