我使用以下代码绘制线,它的工作原理非常棒,
http://www.merowing.info/2012/04/drawing-smooth-lines-with-cocos2d-ios-inspired-by-paper/
现在我要…..
1 GT;检测线是否与自身相交.
2)检测CCSprite是否在该闭合线内.
在搜索时遇到了LineIntersection的许多逻辑,但都没有准确.我给其中一个检测到一个十字路口,但是当没有相交线时也会检测到.
>第一种方法
- (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;
}
这就是我所说的这个方法,
我已经把我的分数存储在arrLinePoints中,从pangesture识别方法
if ([self lineIntersectOccured:[[arrlinePoints objectAtIndex:0] CGPointValue] pointEnd:[[arrlinePoints objectAtIndex:[arrlinePoints count] - 1] CGPointValue]] )
{
NSLog(@"Line Intersected");
}
即使在以下情况下,这也使我成真
我也用不同的方法尝试了相同的功能,通过添加视图到CCDirector的视图
UIBezierPath intersect
但是这是提供性能问题,我的fps降低到近3到6.而且交叉路口的问题依然如此.
交点的完美情况是
请尽快帮忙!
感谢所有支持.
解决方法
当您自己构建路径时,不应该需要测试像素.而是使用用于创建路径的点.
找不到好的线段交集算法不算太难.这个问题似乎最好的答案有一个很好的方法:
Determining if two line segments intersect?
一旦你找到一个命中,使用确切的点和点的历史来构造一个多边形.
从那里你应该能够执行一个“多边形点”测试.
一些表现提示:
>在搜索交叉点时,只检查最新的线段与其他线段的冲突(以前没有相交的所有线条都不会相互交叉)>当您可以断定两点在线段的一个极端时,您可以跳过段,例如:current.a.x< current.b.x&&& (foreign.a.x< current.a.x&& foreign.b.x< current.a.x) 我希望这可以帮助你.