在iOS上组合相交的CGPath [英] Combining Intersecting CGPaths on iOS

查看:123
本文介绍了在iOS上组合相交的CGPath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用的应用存在问题。假设我有两个非常复杂的CGPath,并将它们都添加到CGMutablePath中(因此将它们组合在一起)。好吧,两条路径相交的地方会有彼此之间的点。我要消除这些内部点,并从本质上绘制路径的外部或轮廓。我很难弄清楚该怎么做。

I have a problem in an app I'm working on. Say I have two CGPaths that are fairly complex and I add them both to a CGMutablePath (thus combining them). Well, where the two paths intersect there will be points inside of each other. I want to eliminate those inside points and essentially draw the outside or outline of the path. I am having difficulty figuring out how I would go about this.

编辑:这是我所谈论的示例。蓝色和红色框代表沿CGPath的点。红色框是两条路径中的点。我想以某种方式消除红点,然后重新绘制路径的轮廓。

Here's an example of what I am talking about. The blue and red boxes represent points along the CGPaths. The red boxes are the points that are within both paths. I would like to somehow eliminate the red points and redraw just the outline of the path.

推荐答案

您所描述的是路径内部的并集。

What you are describing is the union of the paths' interiors.

如果路径包含曲线,这是一个很难的问题。

If your paths contain curves, this is a hard problem.

但是,您的示例仅显示直线段,因此我假设您只关心仅包含直线段的路径

However, your example shows only straight line segments, so I will assume you only care about paths that solely contain straight line segments.

在这种情况下,您需要一个多边形并集函数。这种算法在称为计算几何的领域中非常基础。我不知道任何特定于Objective-C的多边形联合实现。您也许可以找到一个纯C库,但是查找C ++库要容易得多。如果将文件扩展名从 .m 更改为 .mm ,则可以使用C ++。以下是一些可以计算多边形并集的C ++库:

In that case, you want a polygon union function. This sort of algorithm is pretty basic in the field known as "computational geometry". I don't know of any Objective-C-specific implementation of polygon union. You might be able to find a pure C library, but it's much easier to find a C++ library. You can use C++ if you change your file extension from .m to .mm. Here are some C++ libraries that can compute the union of polygons:

  • Clipper
  • GEOS - see Polygon::Union
  • CGAL - see 2D Regularized Boolean Set-Operations
  • boost geometry - see union_

请注意,在所有情况下,您都需要使用 CGPathApply 提取t如果您还没有其他格式的顶点,那么它就是您路径的顶点。

Note that in all cases, you'll need to use CGPathApply to extract the vertices of your path, if you don't already have them in another format.

这篇关于在iOS上组合相交的CGPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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