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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…