将圆圈分成多个标题

时间:2018-06-24 22:08:33

标签: swift geometry

我正在尝试将一个圆分成一个给定的角度的3个部分,这个角度决定了这些部分的位置,以获取有关在线搜索飞机保持模式的详细信息。

在其中一个部分跨越360度两侧的情况下,我无法使逻辑正常工作。到目前为止,这就是我所拥有的。径向和航向都是已知数字,在我的测试案例中,分别为310和70。代码写得很快。

func setEntryLimits(){
    directHigh = abs((radial+110.0).truncatingRemainder(dividingBy: 360))
    directLow = abs((radial-70.0).truncatingRemainder(dividingBy: 360))

    parallelHigh = abs(radial.truncatingRemainder(dividingBy: 360))
    parallelLow = abs((radial+110.0).truncatingRemainder(dividingBy: 360))

    tearDropHigh = abs((radial-70.0).truncatingRemainder(dividingBy: 360))
    tearDropLow = abs(radial.truncatingRemainder(dividingBy: 360))


    turnHeadingDial()

}

func turnHeadingDial(){
    var heading = value*360
    inbound = radial + 180 

    if(tearDropHigh>360||parallelHigh>360||directHigh>360){
    heading = heading+360
    }

    if(tearDropHigh<tearDropLow){
        let tempT = tearDropLow
        tearDropLow = tearDropHigh
        tearDropHigh = tempT
    }

    if(parallelHigh<parallelLow){
        let tempP = parallelLow
        parallelLow = parallelHigh
        parallelHigh = tempP
    }

    if(directHigh<directLow){
        let tempD = directLow;
        directLow = directHigh;
        directHigh = tempD;
    }

    if(inbound>360){
        inbound = inbound-360
    }

    if(heading<tearDropHigh&&heading>(tearDropLow)){
        entryLabel.setText("Tear Drop")
        test1.setText(String(tearDropHigh))
        test2.setText(String(tearDropLow))
    }
    else if(heading<(parallelHigh)&&heading>parallelLow){
        entryLabel.setText("Parallel")
        test1.setText(String(parallelHigh))
        test2.setText(String(parallelLow))
    }
    else if(heading<(directHigh)&&heading>directLow){
        entryLabel.setText("Direct")
        test1.setText(String(directHigh))
        test2.setText(String(directLow))
    }
    else {
        entryLabel.setText("calc error")
    }
    print(tearDropHigh," -tear- ",tearDropLow," ",parallelHigh," -parallel- ",parallelLow," ",directHigh," -direct- ",directLow)

    headingLabel.setText(String(heading))
}

1 个答案:

答案 0 :(得分:1)

您的逻辑要复杂得多。只需计算航向和径向之间的差异,然后将其归一化为0到360之间即可。

然后将其与三个范围进行比较。

let radial = ... // some radial in the range 0-360
let heading = ... // some heading in the range 0-360
let diff = (heading - radial + 360).truncatingRemainder(dividingBy: 360)

if diff >= 110 && diff <= 290 {
    // direct entry
} else if diff > 290 {
    // teardrop entry
} else {
    // parallel entry
}

这就是您所需要的。