如何确定CGPath的交集 [英] How to determine intersection of CGPaths

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

问题描述

我的问题类似于 this

我有2个CGPathRef,1个将通过手指触摸移动。我想找到2 CGPathRef是否相交?这个问题差不多在两年前被问过,我想知道是否在同一时间找到了什么。

I have 2 CGPathRef and 1 will be moved by finger touch. I want to find that whether the 2 CGPathRef are intersected? That question was asked almost 2 years ago and I want to know whether something has been found in the mean time.

推荐答案

这是相当老,但我发现它寻找一个类似的解决方案,在我的问题中我想找到一个圆与路径重叠(你的问题的一个特例)。

This is fairly old, but I found it looking for a similar solution, in my problem I wanted to find when a circle overlapped with a path (a special case of your question).

我通过使用 CGPathCreateCopyByStrokingPath 解决了这个问题,使用圆的半径作为笔划宽度创建原始路径的描边版本。如果圆的中心点与描边路径重叠,则原始路径与圆重叠。

I solved this by using CGPathCreateCopyByStrokingPath to create a stroked version of the original path using the radius of the circle as the stroke width. If the center point of the circle overlaps the stroked path then the original path overlaps the circle.

BOOL CGPathIntersectsCircle(CGPathRef path, CGPoint center, CGFloat radius)
{
    CGPathRef fuzzyPath;
    fuzzyPath = CGPathCreateCopyByStrokingPath(path, NULL, radius,
                                               kCGLineCapRound,
                                               kCGLineJoinRound, 0.0);
    if (CGPathContainsPoint(fuzzyPath, NULL, center, NO))
    {
        CGPathRelease(fuzzyPath);
        return YES;
    }
    CGPathRelease(fuzzyPath);
    return NO;
}

编辑:未发布fuzzyPath的小错误。

这篇关于如何确定CGPath的交集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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