【问题标题】:Intersection detection between two circles not working两个圆之间的交点检测不起作用
【发布时间】:2021-06-28 17:36:32
【问题描述】:

我正在尝试使用this answer 来检测两个圆之间是否存在交集。答案是:The above solution should work even for the "one circle inside the other" case.

但是,当我尝试使用以下输入时:

x0: 296
y0: 728
r0: 14

x1: 299
y1: 731
r1: 7

在视觉上看起来像这样:

然后它似乎不起作用。也就是说,下面的等式不成立:

(R0 - R1)^2 <= (x0 - x1)^2 + (y0 - y1)^2 <= (R0 + R1)^2

49 <= 18 <= 441

也就是说,当它们明显相交时,函数将返回 false(它们不相交)。

是我做错了什么,还是公式不正确?

【问题讨论】:

  • 所以我需要做的就是删除等式的左边,然后它会起作用吗?也就是改用(x0 - x1)^2 + (y0 - y1)^2 <= (R0 + R1)^2
  • 哦,他们删除了他们的评论。
  • ^^ 是的。只检查 (r0 + r1),(r0 - r1) 部分允许嵌套的圆圈不相交 - 所以删除它。

标签: javascript math geometry collision intersection


【解决方案1】:

圆圈是嵌套的,但不相交

【讨论】:

  • 是否可以修改方程式以使其在嵌套时也返回 true?
【解决方案2】:

让我们考虑两个外部圆,这意味着它们没有任何共同点,甚至没有切点。

这些圆验证了它们的中心之间的距离大于它们的半径之和。
因此(伪代码):

//we don't need to calculate square roots. Squaring is enough and faster.
centerDist2 = (c1x - c2x)^2 + (c1y-c2y)^2
radiousSum2 = (R1 + R2)^2
//condition test
if centerDist2 > radiousSum2
  return false //no intersection
else
  return true

注意测试中的>。这不包括切线。如果您希望它们被包含为“交叉点”,请替换为 >=

此外,由于浮点数问题,您最好使用一些阈值:

littleValue = 0.0001
if centerDist2 > (radiousSum2 - littleValue)
...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-15
    • 2015-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多