gpt4 book ai didi

swift - 如何告诉用户朝北或朝东的准确度数?

转载 作者:行者123 更新时间:2023-11-28 13:30:27 24 4
gpt4 key购买 nike

我正在创建一个导航应用程序。我想知道我当前的航向与东向之间的度数。我这样做的方法是减去角度为 0 的真实航向,如果是北,如果是东,则减去 90 度,依此类推。当差异达到let i: ClosedRange<Double> = 0...20时,我猜航向朝向预期的方向,在本例中为东。

我想知道这是否是完美的方法。我仍然很困惑是否应该改用轴承。

  //calculate the difference between two angles ( current heading and east angle, 90 degrees)

func cal(firstAngle: Double) -> Double {
var diff = heading - 90
if diff < -360 {
diff += 360
} else if diff > 360 {
diff -= 360
}
return diff
}
// check if the difference falls in the range
let i: ClosedRange<Double> = 0...20

if !(i.contains(k)) {
k = cal(firstAngle: b)
} else if (i.contains(k)) {
let message = "You are heading east"
print(message)
} else {return}
}
  func locationManager(_ manager: CLLocationManager, didUpdateHeading newHeading: CLHeading) {
var heading = newHeading.trueHeading }

最佳答案

这应该可以满足您的需求。代码中的注释:

func cal(heading: Double, desired: Double) -> Double {
// compute adjustment
var angle = desired - heading

// put angle into -360 ... 360 range
angle = angle.truncatingRemainder(dividingBy: 360)

// put angle into -180 ... 180 range
if angle < -180 {
angle += 360
} else if angle > 180 {
angle -= 360
}

return angle
}

// some example calls
cal(heading: 90, desired: 180) // 90
cal(heading: 180, desired: 90) // -90
cal(heading: 350, desired: 90) // 100
cal(heading: 30, desired: 270) // -120

let within20degrees: ClosedRange<Double> = -20...20

let adjust = cal(heading: 105, desired: 90)
if within20degrees ~= adjust {
print("heading in the right direction")
}
heading in the right direction

关于swift - 如何告诉用户朝北或朝东的准确度数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57434269/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com