要查找该点是否在包含两个点的指定线上,我进行以下检查:
-(Boolean)isOnLine:(Line*) line point:(CGPoint) point{
//If between two dots:
if (((line.first.x <= point.x && point.x <= line.last.x)||(line.first.x >= point.x && point.x >= line.last.x))&&((line.first.y<=point.y && point.y<= line.last.y)||(line.first.y>=point.y && point.y>=line.last.y)) ) {
//Calculate distance:
double dist = (((double)point.y - line.first.y)) / (0.00001+((double)(point.x - line.first.x)))- ((double)(line.last.y - line.first.y)) / (0.00001+((double)(line.last.x - line.first.x)));
NSLog(@"Dist to line: %f", fabs(dist));
return fabs(dist) <0.5;
}else
return NO;
}
}
然而,不知何故,该功能不适用于垂直线。我的猜测是 if 子句在某种意义上是无效的。