Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
254 views
in Technique[技术] by (71.8m points)

swift - compare angle and up in strange behavior

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...