compare angle and up in strange behavior

依然范特西╮ 提交于 2021-01-29 15:02:06

问题


i have a func to compare angles.. if the angles si between the range -5/+5deg from the starting heading this should return Color .green otherwise should return .orange.

i simplify the project so it can be easy to use in swift Playground.

here my fun:

func convertToPositiveAngle(_ angle : Angle) -> Angle {
    var toRet = Angle(degrees: fmod(angle.degrees, 360))
    if toRet.degrees < 0 {
        toRet.degrees += 360.0;
    }
    return toRet
}


let headingStart = 3.0
let currentHeading = 3.0


func centro(startHeading:Double, currentHeading : Double)->Color {
    var coloreDaMostrare : Color = .black
    
    let HDGstart  = Angle(degrees: startHeading)
    let HDHCurrent = Angle(degrees: currentHeading)
    let rangeLower = convertToPositiveAngle(HDGstart - Angle(degrees: 5))
    let rangeUpper = (HDGstart + Angle(degrees:  5))
    
    
    if HDHCurrent.degrees>=rangeLower.degrees && HDHCurrent.degrees <= rangeUpper.degrees{
        coloreDaMostrare = .green
        debugPrint("1")
    } else if HDHCurrent.degrees > rangeUpper.degrees {
        debugPrint("2")
        coloreDaMostrare = .orange
    }else if HDHCurrent.degrees < rangeLower.degrees {
        debugPrint("3")
        coloreDaMostrare = .orange
    }

    return coloreDaMostrare
}


centro(startHeading: headingStart, currentHeading: currentHeading)

// should return green not orange

the problem appear the when the starting heading is close to 360/0... a small value..

if you use for example hight value there is no problem,all working fine..

what i'm doing wrong?


回答1:


Use this approach: Directly play with degrees.

func centro(startHeading:Double, currentHeading : Double)->Color {
    var coloreDaMostrare : Color = .black
    
    let HDGstart  = Angle(degrees: startHeading)
    let HDHCurrent = Angle(degrees: currentHeading)
    let finalAngle = convertToPositiveAngle(HDGstart)
    
    if ((finalAngle.degrees - 5)...(finalAngle.degrees + 5)).contains(HDHCurrent.degrees) {
        coloreDaMostrare = .green
        debugPrint("1")
    } else {
        debugPrint("2")
        coloreDaMostrare = .orange
    }
        /**
        //Or another option
        let lowerBound = finalAngle.degrees - 5
        let upperBound = finalAngle.degrees + 5
        let currentBound = HDHCurrent.degrees
        
        if lowerBound <= currentBound && currentBound <= upperBound {
            coloreDaMostrare = .green
            debugPrint("1")
        } else {
            debugPrint("2")
            coloreDaMostrare = .orange
        }
         */
    return coloreDaMostrare
}



回答2:


Simple way to check whether angle difference lies in predefined limits. Handles all angle signs and transition through 0.

Don't forget about radians/degrees (I don't know what units are need in Swift)

if (Cos(A - B) > Cos(5))
   difference is less than 5 
   
 


来源:https://stackoverflow.com/questions/65734626/compare-angle-and-up-in-strange-behavior

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!