合并核心图形中的路径?

发布于 2024-09-14 23:31:35 字数 934 浏览 7 评论 0原文

我画了两个重叠的圆圈。我希望能够将它们填充和描边为合并的新形状。

目前,我创建了一次路径序列,对其进行描边,然后创建它的副本,填充它并将两个相同的路径添加到彼此的顶部,以便它们显示为单个形状。有更好的方法还是这样可以?

更新:这是一个示例代码:

CGMutablePathRef path = CGPathCreateMutable();

CGContextSetStrokeColorWithColor(theContext, strokeColor.CGColor);
CGContextSetLineWidth(theContext, 2);
CGContextSetFillColorWithColor(theContext, fillColor.CGColor);

CGRect rect1 = CGRectMake(0,0, mySize*0.6, mySize*0.6);
CGRect rect2 = CGRectMake(mySize*0.4,0, mySize*0.6, mySize*0.6);

CGPathAddEllipseInRect(path, NULL, rect1);
CGPathAddEllipseInRect(path, NULL, rect2);

CGContextAddPath(theContext, path);
CGContextDrawPath(theContext, kCGPathFillStroke);

CGPathRef pathFill = CGPathCreateCopy ( path );
CGContextAddPath(theContext, pathFill);
CGContextDrawPath(theContext, kCGPathFill);

CGPathRelease(path);
CGPathRelease(pathFill);

如您所见,我创建了原始路径的副本并将其绘制在没有笔划的顶部,所以最终它看​​起来像一个统一的形状。有没有办法避免创建重复项?

I've drawn two circles that overlap. I want to be able to fill and stroke them as a merged new shape.

At the moment, I create the path sequence once, stroke it, then create a copy of it, fill it and add the two identical paths on top of each other, so they appear as a single shape. Is there a better approach or is this ok?

Update: Here is a sample code:

CGMutablePathRef path = CGPathCreateMutable();

CGContextSetStrokeColorWithColor(theContext, strokeColor.CGColor);
CGContextSetLineWidth(theContext, 2);
CGContextSetFillColorWithColor(theContext, fillColor.CGColor);

CGRect rect1 = CGRectMake(0,0, mySize*0.6, mySize*0.6);
CGRect rect2 = CGRectMake(mySize*0.4,0, mySize*0.6, mySize*0.6);

CGPathAddEllipseInRect(path, NULL, rect1);
CGPathAddEllipseInRect(path, NULL, rect2);

CGContextAddPath(theContext, path);
CGContextDrawPath(theContext, kCGPathFillStroke);

CGPathRef pathFill = CGPathCreateCopy ( path );
CGContextAddPath(theContext, pathFill);
CGContextDrawPath(theContext, kCGPathFill);

CGPathRelease(path);
CGPathRelease(pathFill);

As you can see, I create a copy of the original path and draw it on top without the stroke, so in the end it looks like one united shape. Is there a way to avoid creating the duplicate?

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

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

发布评论

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

评论(1

笑咖 2024-09-21 23:31:35

有没有办法避免创建重复项?

是的:只是不要创建它。

Core Graphics 中的路径对象只是路径,仅此而已。它们没有颜色、没有图案、没有填充或描边属性,什么都没有——只是由 moveto、lineto、curveto 和 closepath 段组成的子路径。

填充颜​​色、描边颜色、线宽等都是上下文中图形状态的属性。当前路径也是上下文的属性(但不是 gstate 的属性)。

当您将路径添加到上下文时,这就是您所做的一切:将路径对象中的子路径添加到上下文中的当前路径中。原来的路径对象保持不变;它没有图形状态,即使有,“从路径添加子路径到上下文”操作也会更改上下文,而不是路径对象。

类似地,填充或抚摸上下文的当前路径只会重置上下文的当前路径;它不会更改您可能用于构建该路径的任何路径对象。如果是这样,那么复制路径就太晚了,因为原始路径已经被更改了,但事实并非如此,所以复制路径是不必要的。

因此,只需两次将相同的路径对象添加到当前路径即可。

Is there a way to avoid creating the duplicate?

Yes: Just don't create it.

Path objects in Core Graphics are paths and nothing else. They have no colors, no patterns, no fill or stroke properties, nothing—just subpaths consisting of moveto, lineto, curveto, and closepath segments.

The fill color, stroke color, line width, etc. are all properties of the graphics state in the context. The current path is also a property of the context (but not of the gstate).

When you add a path to the context, that's all you're doing: Adding the subpaths from the path object into the current path in the context. The original path object remains unchanged; it has no graphics state, and even if it did, the “add subpaths from path to context” operation changes the context, not the path object.

Similarly, filling or stroking the current path of a context only resets the current path of the context; it makes no changes to any path objects you might have used to build up that path. If it did, copying the path when you do would be too late, as the original would have already been changed—but it doesn't, so copying the path is unnecessary.

So, just add the same path object to the current path both times.

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