线条绘制+该线条与self的交点,还可以检测绘制线条内的CCSprites

Ana*_*and 10 line-intersection cocos2d-iphone line-drawing ios ccsprite

我正在使用以下代码绘制线条,它的工作效果令人惊叹,

http://www.merowing.info/2012/04/drawing-smooth-lines-with-cocos2d-ios-inspired-by-paper/

现在我想......

1>检测线是否与自身相交.2)检测CCSprite是否在此闭合线内.

在搜索时,我遇到了LineIntersection的许多逻辑,但没有一个是准确的.我给它们中的一个检测交叉点,但是当没有交叉线时它也检测到它.

  1. 第一种方法

    - (BOOL) lineIntersectOccured:(CGPoint)t1 pointEnd:(CGPoint)t2
    {
        BOOL result = NO;
        int pointsCount = [arrlinePoints count];
    
        CGPoint cp1;
        CGPoint cp2;
    
        for(int i = 0, j = 1; j < pointsCount; i++,j++)
        {
            [[arrlinePoints objectAtIndex:i] getValue:&cp1];
            [[arrlinePoints objectAtIndex:j] getValue:&cp2];
    
            // lines connected do not need to be included.
            if((cp2.x == t1.x && cp2.y == t1.y) || (cp1.x == t2.x && cp1.y == t2.y))
            {
                continue;
            }
    
            CGPoint diffLA = CGPointMake(cp2.x - cp1.x,cp2.y - cp1.y);
            CGPoint diffLB = CGPointMake(t2.x - t1.x, t2.y - t1.y);
    
            float compA = diffLA.x*cp1.y - diffLA.y * cp1.x;
            float compB = diffLB.x*t1.y - diffLB.y*t1.x;
    
            BOOL compA1 = (diffLA.x*t1.y - diffLA.y*t1.x) < compA;
            BOOL compA2 = (diffLA.x*t2.y - diffLA.y*t2.x) < compA;
            BOOL compB1 = (diffLB.x*cp1.y - diffLB.y*cp1.x) < compB;
            BOOL compB2 = (diffLB.x*cp2.y - diffLB.y*cp2.x) < compB;
    
            if(((!compA1 && compA2) || (compA1 && !compA2)) && ((!compB1 && compB2) || (compB1 && !compB2)))
            {
                result = YES;
            }
        }
        return result;
    }
    
    Run Code Online (Sandbox Code Playgroud)

这就是我称之为这种方法的方法,我已将我的点存储在来自pangesture识别器方法的arrLinePoints中

  if ([self lineIntersectOccured:[[arrlinePoints objectAtIndex:0] CGPointValue] pointEnd:[[arrlinePoints objectAtIndex:[arrlinePoints count] - 1] CGPointValue]] )
  {
      NSLog(@"Line Intersected");
  }
Run Code Online (Sandbox Code Playgroud)

即使有以下情况,这也让我真实

在此输入图像描述

通过在CCDirector的视图中添加视图,我也尝试了不同方法的相同功能

UIBezierPath相交

但这是性能问题,我的fps降低到接近3到6.而且这个交叉点问题仍然是一样的.

交叉口的完美情况是

在此输入图像描述

请尽快帮忙!感谢所有支持.

NiK*_*KKi 1

虽然没有实现,但我想您可以使用在 TouchMoved 方法中绘制的线条的像素值来检测绘制的线条的像素值。或者您可以参考这里了解详细方法。这里也做了同样的工作。