检查同一圆上的两个线段是否重叠/相交 [英] check if two segments on the same circle overlap / intersect

查看:36
本文介绍了检查同一圆上的两个线段是否重叠/相交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Given two circle segments of the same circle: A=[a1, a2] and B=[b1, b2], with:

  • a1、a2、b1、b2 值在 -inf 和 +inf 之间的度数
  • a1 <= a2 ;b1 <= b2
  • a2-a1<=360;b2-b1<=360

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

How can I find out if these two circle segments overlap? (i.E. if they intersect or touch in at least one point)

示例:

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° 时将其分成两部分,但我不确定这是否涵盖所有情况,我想知道是否有一个优雅的公式.

This looks like a deceptively simple problem but I cannot wrap my head around it. I currently have a basic idea for a solution which involves splitting each segment into two if it crosses 0°, but I am not sure if that covers all cases, and I was wondering if there is an elegant formula.

推荐答案

正如@admaoldak 提到的,首先标准化度数:

As @admaoldak mentioned, normalize the degrees first:

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

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

Now to check if b1 is within (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)

这篇关于检查同一圆上的两个线段是否重叠/相交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆