gpt4 book ai didi

ruby - 将方向(N、S、SE、SSE 等)转换为方位角

转载 作者:太空宇宙 更新时间:2023-11-03 18:13:33 24 4
gpt4 key购买 nike

ruby 或 gem 中是否有将具有方向(标题中的示例)的字符串转换为以度为单位的方位角的功能,方位角定义如下?

A numerical value representing the direction in degrees, with true north at 0° and progressing clockwise.

最佳答案

这适用于 8 个主要方向:

def cardinal_direction_degrees(s)
h = {n: 0, ne: 45, e: 90, se: 135, s: 180, sw: 225, w: 270, nw: 315}
h[s.to_s.downcase.to_sym]
end

puts cardinal_direction_degrees('N') #=> 0
puts cardinal_direction_degrees('SW') #=> 225

您可以通过向散列中添加更多元素来轻松添加剩余的方向。

关于ruby - 将方向(N、S、SE、SSE 等)转换为方位角,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29812871/

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