gpt4 book ai didi

ios - 两个 UIBezierPaths 交集作为一个 UIBezierPath

转载 作者:搜寻专家 更新时间:2023-10-30 22:04:55 71 4
gpt4 key购买 nike

我有两个 UIBezierPath,一个表示图像的多边形部分,另一个是要在其上绘制的路径。

我需要找到它们之间的交点,以便只有在这个交点区域内的点才会被着色。

UIBezierPath 中是否有一种方法可以找到两条路径之间的交点或新路径?

最佳答案

我不知道有什么方法可以获取两条路径相交的新路径,但您可以通过使用每条路径的裁剪属性来填充或以其他方式绘制交点。

在这个例子中,有两条路径,一个正方形和一个圆:

let path1 = UIBezierPath(rect: CGRect(x: 0, y: 0, width: 100, height: 100))
let path2 = UIBezierPath(ovalIn: CGRect(x:50, y:50, width: 100, height: 100))

我制作了一个渲染器来绘制这些,但您可以在 drawRect 或任何地方执行此操作:

let renderer = UIGraphicsImageRenderer(bounds: CGRect(x: 0, y: 0, width: 200, height: 200))
let image = renderer.image {
context in
// You wouldn't actually stroke the paths, this is just to illustrate where they are
UIColor.gray.setStroke()
path1.stroke()
path2.stroke()
// You would just do this part
path1.addClip()
path2.addClip()
UIColor.red.setFill()
context.fill(context.format.bounds)
}

生成的图像看起来像这样(为了清晰起见,我已经按照代码注释中的说明对每条路径进行了描边,实际上您只需要填充部分):

enter image description here

关于ios - 两个 UIBezierPaths 交集作为一个 UIBezierPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42418390/

71 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com