我有两个点A和B,它们在设备屏幕上定义了一个线段,还有另一个点C。请使用高效且简短易懂的算法(最好使用标准数学库),如何检查线段AB是否与点C的距离小于R?
我知道有一种简单的方法可以找到点到直线的最短距离,但是它假设直线是无限长的。我手头的是有两个端点的线段。
我曾考虑在Math SE中发布此问题,但决定不这样做,因为我不想得到所有那些像https://math.stackexchange.com/questions/2837/how-to-tell-if-a-line-segment-intersects-with-a-circle中那样的长数学公式作为答案。我需要的是一个高效而易读的计算机算法,而不是一个正式的数学定理。
附:我有以下Objective-C方法骨架需要实现:
我知道有一种简单的方法可以找到点到直线的最短距离,但是它假设直线是无限长的。我手头的是有两个端点的线段。
我曾考虑在Math SE中发布此问题,但决定不这样做,因为我不想得到所有那些像https://math.stackexchange.com/questions/2837/how-to-tell-if-a-line-segment-intersects-with-a-circle中那样的长数学公式作为答案。我需要的是一个高效而易读的计算机算法,而不是一个正式的数学定理。
附:我有以下Objective-C方法骨架需要实现:
typedef struct {
CGPoint a;
CGPoint b;
} CGLineSegment;
+ (BOOL)isLineSegment:(CGLineSegment)line withinRadius:(CGFloat)radius fromPoint:(CGPoint)point {
}
解决方法:
感谢veredesmarald的回答(我已经接受),我已经实现了该方法,以下作为其他人的参考:
+ (BOOL)isLineSegment:(CGLineSegment)line withinRadius:(CGFloat)radius fromPoint:(CGPoint)point {
CGPoint v = CGPointMake(line.b.x - line.a.x, line.b.y - line.a.y);
CGPoint w = CGPointMake(point.x - line.a.x, point.y - line.a.y);
CGFloat c1 = dotProduct(w, v);
CGFloat c2 = dotProduct(v, v);
CGFloat d;
if (c1 <= 0) {
d = distance(point, line.a);
}
else if (c2 <= c1) {
d = distance(point, line.b);
}
else {
CGFloat b = c1 / c2;
CGPoint Pb = CGPointMake(line.a.x + b * v.x, line.a.y + b * v.y);
d = distance(point, Pb);
}
return d <= radius;
}
CGFloat distance(const CGPoint p1, const CGPoint p2) {
return sqrt(pow(p2.x - p1.x, 2) + pow(p2.y - p1.y, 2));
}
CGFloat dotProduct(const CGPoint p1, const CGPoint p2) {
return p1.x * p2.x + p1.y * p2.y;
}