如果两个线段重叠或相交,则将它们合并在同一个圆上 [英] Combine two segments on the same circle if they overlap or intersect

查看:23
本文介绍了如果两个线段重叠或相交,则将它们合并在同一个圆上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果两个部分重叠或相交,我会尝试将它们组合起来.我的问题类似于 thisthis.但是,我想要的是组合两个部分.

I am try to combine two segments if they overlap or intersect.My question is similar to this and this. However, what I want is to combine two segments.

public class Segment
{
    private readonly double _from;
    private readonly double _to;
    public Segment(double from, double to)
    {
        _from = Normalize(from); // 0 <= x < 360
        _to = Normalize(to);
    }

    public bool Inside(double target)
    {
        if (_from < _to)
            return _from <= target && target <= _to;
        return _from <= target || target <= _to;
    }
}

我正在尝试编写一个 TryCombine().

I am trying to write a TryCombine().

public bool TryCombine(Segment other, out Segment result)
{
    // if not intersect
    // return false

    // else if overlap
    // return true, out larger one

    // else if intersect
    // return true, out a new one with merged bound
}

预期结果

var seg1 = new Segment(0, 100);
var seg2 = new Segment(50, 150);
var seg3 = new Segment(110, -100);
var seg4 = new Segment(10, 50);
Segment result;

seg1.TryCombine(seg2, result); // True, result = Segment(0, 150)
seg1.TryCombine(seg3, result); // False
seg1.TryCombine(seg4, result); // True, result = Segment(0, 100)
seg2.TryCombine(seg3, result); // True, result = Segment(260, 150)

推荐答案

您可以使用我在第二个链接中的回答中描述的方法.

You can use approach described in my answer in you second link.

ma = (a2 + a1)/ 2  
mb = (b2 + b1)/ 2  
cda = Cos(da)
cdb = Cos(db)

要检查是否存在交集以及发生什么样的交集,找到4个布尔值

To check whether intersection exists and what kind o intersection takes place, find 4 boolean values

 BStartInsideA = (Cos(ma - b1) >= cda)
 BEndInsideA  =  (Cos(ma - b2) >= cda)
 AStartInsideB = (Cos(mb - a1) >= cdb)
 AEndInsideB =   (Cos(mb - a2) >= cdb)

这些组合可以形成 16 种可能的结果(并非所有结果都可靠).我会将这些结果组合为 4 位值的位,并在 case 语句中处理它们.

These combinations could form 16 possible results (not all are reliable). I'd combine these results as bits of 4-bit value and treat them in the case statement.

例如,如果第一个和最后一个值都为真(值 0b1001 = 9),你有简单的交集,就像你的 seg1-seg2 案例 - 所以得到 AStart ans 起点,BEnd 作为结束指向并标准化它们(如果 BEnd 小于 AStart,则将 360 添加到 BEnd).

For example, if the first and the last values are true (value 0b1001 = 9), you have simple intersection like your seg1-seg2 case - so get AStart ans starting point, BEnd as ending point and normalize them (add 360 to BEnd if it is less than AStart).

预归一化步骤应提供 BEnd>=BStart 和 AEnd>=AStart(例如,将 (3,1) 弧变换为中点 182 和半角 179 的 (3, 361)

Pre-normalization step should provide BEnd>=BStart and AEnd>=AStart (for example, transform (3,1) arc into (3, 361) with middle point 182 and semi-angle 179)

可能的结果(两个额外的案例,4 个简单的结束组合,4 个单端重合的案例):

Possible results (two extra cases, 4 simple end combinations, 4 one-end coinciding cases):

 0000: no intersection
 1111: full circle

 0011: AStart-AEnd
 1001: AStart-BEnd
 0110: BStart-AEnd
 1100: BStart-BEnd

 0111: AStart-AEnd
 1011: AStart-AEnd
 1110: BStart-BEnd
 1101: BStart-BEnd

一位组合和 1010、0101 看起来不可能

One-bit combinations and 1010, 0101 look impossible

带有通配符,正如作者所建议的:

with wildcards, as author proposed:

 At first check for 
   0000: no intersection
   1111: full circle
 then
   **11: AStart-AEnd
   1001: AStart-BEnd
   0110: BStart-AEnd
   11**: BStart-BEnd

这篇关于如果两个线段重叠或相交,则将它们合并在同一个圆上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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