从极坐标向量中找到最接近的值

Find the Closest value from vector of Polar coordinates

我假设我的问题有一个非常简单的解决方案,我有一个表示极角(即 1 到 360 度)的向量。

x<-seq(300,340)

我需要找到最接近 y 的 x 值,

y<-30

在这个例子中,我需要从 x 得到 340 returned 我知道 DescTools 包中的 Closest 函数,它将 return 300 来自 x。我敢肯定答案可能很简单,但它让我望而却步。

欢迎思考

这是你想要的吗?

x <- 300:340
y <- 30

polardist <- sapply(x,function(x){min((y-x)%%360,(x-y)%%360)})
ans <- x[polardist==min(polardist)] #Will have length>1 if there are ties