Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

clipping polygon against rectangle

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.

Polygon and page as I have them now Polygon and page as I need to have them 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 :

enter image description hereenter image description here

Thank you.

like image 593
lvictorino Avatar asked Feb 16 '26 21:02

lvictorino


1 Answers

  1. Clipping a polygon with a rectangle. We reduce this problem to
  2. clipping a polygon with a line. We reduce this to an even simpler problem:
  3. clipping one edge of a polygon with a line. Which is really just
  4. 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?

like image 128
Beta Avatar answered Feb 21 '26 16:02

Beta



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!