作为初步检查,您可以使用叉积计算点和线之间的距离:
\n\n(x1,y1) = p1, (x2,y2) = p2\n(cx, cy) = c = circle center\ndelta = p2 - p1 (the difference vector)\nunit = delta/norm(delta) (the unit vector along the line segment)\n(c-p1) x unit = (cx-x1) * unity - (cy-y1) * unitx = d (distance of the circle center to the line)\nRun Code Online (Sandbox Code Playgroud)\n\n注意d有一个方向(符号)。
如果d在[-R,R]范围之外,则线段不能与圆相交。
如果您的线段移动量不大,您可以保存单位向量以供以后重复使用。
\n\n如果圆确实与直线相交(而不是线段),它可能仍然不与线段相交。检查这三个条件:
\n\np1位于圆内;范数(p1-c) < Rp2位于圆内;范数(p2-c) < Rp1和之间p2:(unit . p1 < unit . c < unit . p2) or (unit . p2 < unit . c < unit . p1)其中.是向量点积。
如果这些条件都不成立,那么它们就不会相交。
\n\n您可能还需要知道它们相交的位置:
\n\nperp = (-unity, unitx) (The perpendicular vector)\npclosest = perp * d + c (The point on the line closest to the circle center)\ndline = sqrt(R^2 - d^2) (The distance of the intersection points from pclosest)\ni{1,2} = \xc2\xb1dline * unit + pclosest\nRun Code Online (Sandbox Code Playgroud)\n\n显然,您需要单独检查是否i{1,2}位于p1和之间p2,就像我们在上面的第三个条件中所做的那样。
| 归档时间: |
|
| 查看次数: |
12504 次 |
| 最近记录: |