I can detect the intersection point of two lines, but if my line don't has the length of my screen, it detects the point, where it shouldn't be.
Here a preview:
So, it shouldn't detect this intersection because the horizontal line isn't that long.
Code:
- (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;
}