【问题标题】:How do you convert a bearing (in degrees) to a gradient (slope) using R?如何使用 R 将方位角(以度为单位)转换为梯度(斜率)?
【发布时间】:2018-07-24 09:03:24
【问题描述】:

我希望将方位转换为渐变,例如“045”为1。我的代码如下:

bearing = 
grad = 1/tan(bearing)

有两个问题

  • 假定方位角为弧度
  • 如果方位角为 90 的任意倍数,则等式将失败

如何将 r 更改为度数(或者我只需要自己转换)以及如何避免方位角是 90 的倍数的情况

预期输入和输出的一些示例如下:

000 -> Inf 
062 -> 1.88
180 -> -Inf
301 -> -1.66

【问题讨论】:

  • 您可以轻松地将弧度转换为度数:度数 = 弧度 × 180° / π
  • 不能解决90°的倍数问题
  • degrees <- degrees %% 90?
  • 哪个方程会失败?如果您在第二个等式中包含 45,90,180 或任何数字作为方位角,您仍然会得到结果,尽管它可能不正确。
  • 您能否提供一些轴承示例和预期输出?

标签: r trigonometry


【解决方案1】:

以下函数会将方位角转换为梯度

library(dplyr)

dtr <- function(degrees) { degrees * (pi/180) } # Converts degrees to radians 

btg <- function(bearing) {

  if( bearing > 180 ) { bearing <- bearing - 180 } # Since the gradient of bearing x and x - 180 are the same when x > 180

  if( bearing == 0 ) { gradient <- Inf } # Replace Inf with NA_character_ if preferred
  if( bearing == 90 ) { gradient <- 0 }
  if( bearing == 180 ) { gradient <- Inf }

  if( !bearing %in% c(0, 90, 180)) {

    if(bearing < 90 ) {
      gradient <- sin(dtr(90 - bearing)) / sin(dtr(180-bearing))
    }

    if(bearing > 90 ) {
      gradient <- (sin(dtr(90 - bearing)) / sin(dtr(180-bearing)))
    }

  }

  return(gradient)
}

例如

bearings <- c(30, 45, 90, 135, 170, 179, 180, 235, 250, 290, 360) # We can test the btg function on some bearings

for (i in 1:length(bearings)) {

  gradients[i] <- btg(bearings[i])

}

data.frame(bearings, gradients)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-16
    • 1970-01-01
    相关资源
    最近更新 更多