Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...