我可以检测到两条线的交点,但如果我的线没有屏幕的长度,它会检测到不应该在的点。
这是一个预览: 所以,它不应该检测到这个交叉点,因为水平线没有那么长。
代码:
- (NSMutableArray *) intersectWithLines:(CGPoint)startPoint andEnd:(CGPoint)endPoint {
NSMutableArray *intersects = [[NSMutableArray alloc] init];
for(GameLine *line in [_lineBackground getLines]) {
double lineStartX = line.startPos.x;
double lineStartY = line.startPos.y;
double tempEndX = line.endPos.x;
double tempEndY = line.endPos.y;
double d = ((startPoint.x - endPoint.x)*(lineStartY - tempEndY)) - ((startPoint.y - endPoint.y) * (lineStartX - tempEndX));
if(d != 0) {
double sX = ((lineStartX - tempEndX) * (startPoint.x * endPoint.y - startPoint.y * endPoint.x) - (startPoint.x - endPoint.x) * (lineStartX * tempEndY - lineStartY * tempEndX)) / d;
double sY = ((lineStartY - tempEndY) * (startPoint.x * endPoint.y - startPoint.y * endPoint.x) - (startPoint.y - endPoint.y) * (lineStartX * tempEndY - lineStartY * tempEndX)) / d;
if([self isValidCGPoint:CGPointMake(sX, sY)]) {
[intersects addObject:[NSValue valueWithCGPoint:CGPointMake(sX, sY)]];
}
}
}
return intersects;
}