如何获取 uibezierpath 周围的缓冲区
我有一些 uibezierpaths。作为路径,它们实际上没有厚度。 但是,我希望找到一种方法来定义路径周围的区域,例如这张图片中线条周围的灰色区域
,我想测试绘制的线条是否落在线条周围的缓冲区内。 我以为这会很简单,但事实证明它比我想象的要复杂得多。我可以使用 CGPathApply 函数来检查路径上的点,然后获取每个点的范围 + 或 - ,但它比角度和曲线更复杂。有什么想法吗?
i have some uibezierpaths. as paths, they don't really have thickness.
but, I am hoping to find a way to define an area around a path like the grayish areas around the lines in this picture
basically, i want to test whether drawn lines fall within the buffer zone around the lines.
i thought this would be simple, but it's turning out to be much more complex than i thought. I can use the CGPathApply function to examine the points along my path, and then getting a range +or- each point, but it's more complicated than that with angles and curves. any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
扩大路径的宽度实际上是相当困难的。然而,你可以用更粗的宽度来抚摸它,并获得几乎相同的效果。像...
...之类的东西会产生一张像你问题中那样的图片。但我怀疑这对你来说不是新闻。
真正的技巧是测试“画出的线是否落在缓冲区内”。这个问题与我刚刚在 中为自己回答的问题非常相似另一个问题。查看我在那里分享的 LineSample.zip 代码。这实现了位图/按位数据比较,以检测与您需要的非常相似的行上的命中。您可以将较粗的“缓冲区”路径绘制到位图中进行测试,并在视图中显示较细的线条。
Expanding the width of a path is actually quite difficult. However, you could just stroke it with a thicker width and get pretty much the same effect. Something like...
...would produce a picture like the one in your question. But I doubt that's news to you.
The real trick is the test of "whether drawn lines fall within the buffer zone." That problem is very similar to one which I just answered for myself in another question. Take a look at the LineSample.zip code I shared there. This implements a bitmap/bitwise data comparison to detect hits on lines much like you need. You could just draw the thicker "buffer" paths into the bitmap for testing and show the thinner lines in your view.
基本上,您想要检查是否有任何点落在路径周围指定大小的区域内。
其实做起来非常简单。首先,您需要一个值来定义要测试的路径周围的空间量。就说20分吧。因此,您需要做的是启动一个 FOR 循环,从 -20 开始到 20,并在每次迭代时创建路径的副本,转换路径的 x 和 y 坐标,检查它们中的每一个。
所有这些在此代码示例中都更加清晰。
Basically, you want to check if any point falls inside a region of specified size around your path.
It is actually very simple to do. First, you need a value which will define the amount of space around path you want to test. Let's say 20 points. So what you need to do is start a FOR loop, starting from -20 to 20, and at each iteration, create a copy of your path, translate the path's x and y co-odrinates, check each of them.
All of this is more clear in this code sample.