针对矩形裁剪多边形 [英] clipping polygon against rectangle

查看:89
本文介绍了针对矩形裁剪多边形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天,我有一个(简单的)渲染问题.我当前的项目从文件获取数据以生成SVG文件.多亏了SVG格式,将事物绘制为多边形非常容易,但是我有一个问题:我的某些多边形位于页面的AND内(这意味着它们中的某些部分会显示,而其余部分由于事实而不会显示)它们超出了显示限制).为了优化最终的SVG文件,我需要将多边形缩小为更简单的形式.

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 :)

我也要处理这个棘手的情况:

I have this tricky case to handle as well :

谢谢.

推荐答案

  1. 使用矩形剪切多边形.我们将这个问题减少到
  2. 使用线剪切多边形.我们将其简化为一个更简单的问题:
  3. 用一条线剪切多边形的一个边缘.真的是
  4. 查找线段与线的交点(如果存在).
  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?

这篇关于针对矩形裁剪多边形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆