直线多边形相交

发布于 2024-11-08 12:57:11 字数 474 浏览 0 评论 0 原文

我正在寻找/尝试开发一种与矩形相交的最佳算法。我正在测试的多边形没有孔。

此处这里适用于非常一般的多边形,并且解决方案非常复杂,这是可以理解的。

希望 SO 社区可以帮助我记录仅使用直线多边形的特殊情况的算法。

我正在寻找下图中用绿色填充的多边形:

与矩形相交的直线多边形

I am looking for / trying to develop an optimal algorithm for rectilinear polygon intersection with rectangles. The polygons I am testing do not have holes.

Answers like those given here and here are for very general polygons, and the solutions are understandably quite complex.

Hoping that the S.O. community can help me document algorithms for the special cases with just rectilinear polygons.

I am looking for the polygon filled in green in the image below:

rectilinear polygon intersection with a rectangle

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤独陪着我 2024-11-15 12:57:11

Preparata 和 Shamos 所著的《计算几何:简介》一书有一章介绍直线多边形。

The book Computational Geometry: an Introduction by Preparata and Shamos has a chapter on rectilinear polygons.

很快妥协 2024-11-15 12:57:11

使用扫描线算法,利用直线多边形由其顶点定义的事实。

表示顶点及其所属的矩形,即类似 (x, y, #rect) 的内容。将所有边相交产生的点添加到这组点中。这些新点的形式为(x, y, Final),因为我们已经知道它们属于结果点集。

现在:

  • 使用扫描线按 x 值对所有点进行排序
  • ,从第一个 x 坐标开始;对于每个新点:
    • 如果它是“起点”,请将其添加到临时集T中。如果它是矩形 A 中的点和 T 中矩形 B 的点的 y 坐标之间的点,则将其标记为“最终”(反之亦然)。
    • 如果它是“终点”,则从 T 中删除它及其对应的起点。

之后,所有标记为“最终”的点都表示生成的多边形的顶点。

令 N 为总点数。进一步假设通过查找 T 来测试是否应该将一个点标记为“最终”需要花费 O(log(n)) 时间,整个算法的时间复杂度为 O(N*log(N)) 。

请注意,查找所有交点的任务可以合并到上述算法中,因为有效地查找所有交点本身通常是一种扫描线算法。另请注意,生成的一组点可能包含多个多边形,这使得从“最终”顶点重建解多边形变得稍微困难​​。

Use a sweep line algorithm, making use of the fact that a rectilinear polygon is defined by its vertices.

Represent the vertices along with the rectangle that they belong to, i.e. something like (x, y, #rect). To this set of points, add those points that result from the intersections of all edges. These new points are of the form (x, y, final), since we already know that they belong to the resulting set of points.

Now:

  • sort all points by their x-value
  • use a sweep line, starting at the first x-coordinate; for each new point:
    • if it's a "start point", add it to a temporary set T. Mark it "final" if it's a point from rectangle A and between y-coordinates from points from rectangle B in T (or vice versa).
    • if it's an "end point", remove it and its corresponding start point from T.

After that, all points that are marked "final" denote the vertices of the resulting polygon.

Let N be the total number of points. Further assuming that testing whether we should mark a point as being "final" takes time O(log(n)) by looking up T, this whole algorithm is in O(N*log(N)).

Note that the task of finding all intersections can be incorporated into the above algorithm, since finding all intersections efficiently is itself a sweep line algorithm usually. Also note that the resulting set of points may contain more than one polygon, which makes it slightly harder to reconstruct the solution polygons out of the "final" vertices.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文