如何使用R将方位(以度为单位)转换为坡度(坡度)



我想将方位转换为梯度,比如说"045"转换为1。我的代码如下:

bearing = 
grad = 1/tan(bearing)

有两个问题

  • 它假定轴承为弧度
  • 如果轴承是90的任意倍数,则该方程将失效

我如何将r更改为以度为单位的读数(或者我只需要自己转换它(,以及我如何避免轴承是90 的倍数的情况

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

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

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

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)

相关内容

  • 没有找到相关文章

最新更新