CGPathRef交集 [英] CGPathRef intersection

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

问题描述

有办法找出两个CGPathRef是否相交。在我的情况下,所有的CGPath都有closePath。



例如,我有两个路径。一个路径是以一定角度旋转的矩形,另一个路径是弯曲路径。两个路径起源将频繁变化。在某一点它们可以相交。我想知道他们什么时候相交。



提前感谢

解决方案

将一个路径作为剪切路径,绘制其他路径,然后搜索在剪切过程中存活的像素:

  / initialise和erase context 
CGContextAddPath(context,path1);
CGContextClip(context);

//将填充颜色设置为交叉颜色
CGContextAddPath(context,path2);
CGContextFillPath(context);

//搜索匹配交叉颜色的像素

=交叉。



不要忘记交集取决于内部性的定义,其中有几个。此代码使用圈数填充规则,您可能想要偶数奇数规则或其他别的。如果内部性不能让你在晚上,那么这段代码应该很好。



我以前的答案涉及绘制透明曲线到RGBA上下文。此解决方案优于旧解决方案,因为它是


  1. 更简单















  2. $ b

    谁可以要求更多?



    我想你可以要求一个完整的实施,准备削减不了,但这会破坏乐趣,



    p>将 CGPathRefs 另外 以50%的透明度绘制为零, CGBitmapContextCreate RGBA内存缓冲区并检查任何像素值> 128.这可以在任何支持CoreGraphics(即iOS和OSX)的平台上运行。



    在伪代码

      //零内存

    CGContextRef上下文;
    context = CGBitmapContextCreate(memory,wide,high,8,wide * 4,CGColorSpaceCreateDeviceRGB(),kCGImageAlphaPremultipliedLast);
    CGContextSetRGBFillColor(context,1,1,1,0.5); //现在你绘制的所有内容都将是50%

    //将你的路径1绘制到上下文

    //将你的路径2绘制到上下文

    //对于存储器缓冲区中的每个像素
    if(* p> 128)return true; // curves intersect
    else p + = 4; // keep looking

    让光栅化版本的分辨率成为您的精度,性能需求。


    Is there a way to find out whether two CGPathRefs are intersected or not. In my case all the CGPaths are having closePath.

    For example, I am having two paths. One path is the rectangle which is rotated with some angle and the other path is curved path. Two paths origin will be changing frequently. At some point they may intersect. I want to know when they are intersected. Please let me know if you have any solution.

    Thanks in advance

    解决方案

    Make one path the clipping path, draw the other path, then search for pixels that survived the clipping process:

    // initialise and erase context
    CGContextAddPath(context, path1);
    CGContextClip(context);
    
    // set fill colour to intersection colour
    CGContextAddPath(context, path2);
    CGContextFillPath(context);
    
    // search for pixels that match intersection colour
    

    This works because clipping = intersecting.

    Don't forget that intersection depends on the definition of interiority, of which there are several. This code uses the winding-number fill rule, you might want the even odd rule or something else again. If interiority doesn't keep you up at night, then this code should be fine.

    My previous answer involved drawing transparent curves to an RGBA context. This solution is superior to the old one because it is

    1. simpler
    2. uses a quarter of the memory as an 8bit greyscale context suffices
    3. obviates the need for hairy, difficult-to-debug transparency code

    Who could ask for more?

    I guess you could ask for a complete implementation, ready to cut'n'paste, but that would spoil the fun and obfuscate an otherwise simple answer.

    OLDER, HARDER TO UNDERSTAND AND LESS EFFICIENT ANSWER

    Draw both CGPathRefs separately at 50% transparency into a zeroed, CGBitmapContextCreate-ed RGBA memory buffer and check for any pixel values > 128. This works on any platform that supports CoreGraphics (i.e. iOS and OSX).

    In pseudocode

    // zero memory
    
    CGContextRef context;
    context = CGBitmapContextCreate(memory, wide, high, 8, wide*4, CGColorSpaceCreateDeviceRGB(), kCGImageAlphaPremultipliedLast);
    CGContextSetRGBFillColor(context, 1, 1, 1, 0.5);  // now everything you draw will be at 50%
    
    // draw your path 1 to context
    
    // draw your path 2 to context
    
    // for each pixel in memory buffer
    if(*p > 128) return true; // curves intersect  
    else p+= 4; // keep looking
    

    Let the resolution of the rasterised versions be your precision and choose the precision to suit your performance needs.

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

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