我正在使用以下代码画线,效果非常好,
http://www.merowing.info/2012/04/drawing-smooth-lines-with-cocos2d-ios-inspired-by-paper/
现在我想......
1> 检测线是否与自身相交。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; }
这就是我调用此方法的方式,我已将我的点存储在来自 pangesture 识别器方法的 arrLinePoints 中
if ([self lineIntersectOccured:[[arrlinePoints objectAtIndex:0] CGPointValue] pointEnd:[[arrlinePoints objectAtIndex:[arrlinePoints count] - 1] CGPointValue]] )
{
NSLog(@"Line Intersected");
}
即使在以下情况下,这也给了我真实的
我还通过将视图添加到 CCDirector 的视图中尝试了使用不同方法的相同功能
但这带来了性能问题,我的 fps 降低到几乎 3 到 6。而且那个交叉点问题仍然存在。
相交的完美情况是
请尽快提供帮助!感谢大家的支持。