gpt4 book ai didi

algorithm - 检查同一个圆上的两个线段是否重叠/相交

转载 作者:太空狗 更新时间:2023-10-29 23:12:36 26 4
gpt4 key购买 nike

给定同一个圆的两个圆段:A=[a1, a2] 和 B=[b1, b2],其中:

  • a1、a2、b1、b2 的值介于 -inf 和 +inf 之间
  • a1 <= a2 ; b1 <= b2
  • a2-a1<=360; b2-b1<=360

如何确定这两个圆弧段是否重叠?(即如果它们至少在一点相交或接触)

例子:

A=[  -45°,    45°]; B=[   10°,   20°] ==> overlap
A=[ -45°, 45°]; B=[ 90°, 180°] ==> no overlap
A=[ -45°, 45°]; B=[ 180°, 360°] ==> overlap
A=[ -405°, -315°]; B=[ 180°, 360°] ==> overlap
A=[-3600°, -3601°]; B=[ 3601°, 3602°] ==> overlap (touching counts as overlap)
A=[ 3600°, 3601°]; B=[-3601°,-3602°] ==> overlap (touching counts as overlap)
A=[ -1°, 1°]; B=[ 3602°, 3603°] ==> no overlap

这看起来像是一个看似简单的问题,但我无法全神贯注。我目前有一个解决方案的基本想法,如果它穿过 0°,则将每个线段分成两部分,但我不确定这是否涵盖所有情况,我想知道是否有一个优雅的公式。

最佳答案

正如@admaoldak 提到的,首先将度数归一化:

a1_norm = a1 % 360
a2_norm = a2 % 360
b1_norm = b1 % 360
b2_norm = b2 % 360

现在检查 b1 是否在 (a1,a2) 内,

def intersect(b, as, ae
Intersect = False
If as > ae:
if b >= as or b <= ae:
return True
Else:
if b>=as and b<=ae:
return True
return False

最终答案是:

intersect(b1_norm,a1_norm,a2_norm)||intersect(b2_norm,a1_norm,a2_norm)||
intersect(a1_norm,b1_norm,b2_norm)||intersect(a2_norm,b1_norm,b2_norm)

关于algorithm - 检查同一个圆上的两个线段是否重叠/相交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45090776/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com