- 我有一条线段(起点x1,y1,终点x2,y2(D = 5)),和一个圆(半径R,中心点x3,y3)
如何检查我的线段是否与我的圆相交?
作为初步检查,您可以使用叉积计算点与直线之间的距离:
(x1,y1) = p1, (x2,y2) = p2
(cx, cy) = c = circle center
delta = p2 - p1 (the difference vector)
unit = delta/norm(delta) (the unit vector along the line segment)
(c-p1) x unit = (cx-x1) * unity - (cy-y1) * unitx = d (distance of the circle center to the line)
d
有一个方向(符号)。d
超出范围[-R,R],则线段不能与圆相交。p1
在圆内;norm(p1-c) < R
- p2
在圆内;norm(p2-c) < R
- 从线到圆心的最近点位于p1
和p2
之间:
(unit . p1 < unit . c < unit . p2) or (unit . p2 < unit . c < unit . p1)
其中.
是向量点积。perp = (-unity, unitx) (The perpendicular vector)
pclosest = perp * d + c (The point on the line closest to the circle center)
dline = sqrt(R^2 - d^2) (The distance of the intersection points from pclosest)
i{1,2} = ±dline * unit + pclosest
显然,您需要单独检查{1,2}是否位于p1
和p2
(y2-y1)/(x2-x1)*x + D = y
和 (x-x3)^2 + (y-y3)^2 = R^2
推导出 (x, y)
。然后判断 (x, y)
是否属于你的线段。