如何计算 R 中给定 'x' 和 'y' 位置的轨迹半径

how to calculate the radius of a trajectory with given 'x' and 'y' positions in R

我在数据框中有一些移动物体的轨迹,例如,这里考虑了三个轨迹,如下所示:

> d1 <- data.frame(X1 = c(86.46, 79.88, 73.63, 67.63, 61.78, 56.05, 50.40, 44.80, 39.25, 33.79, 28.49, 23.40, 18.53, 13.84, 9.31, 5.04, 1.12), 
             Y1 = c(4.28, 5.49, 6.80, 8.16, 9.59, 11.18, 13.05, 15.28, 17.86, 20.81, 24.12, 27.75, 31.68, 35.87, 40.31, 44.92, 49.61))

> d2 <- data.frame(X2 = c(0.32, 4.00,  8.00, 12.31, 16.87, 21.64, 26.60, 31.75, 37.08, 42.62, 48.36, 54.33, 60.59, 67.25, 74.48, 82.42), 
             Y2 = c(57.55, 52.67, 47.98, 43.49, 39.24, 35.26, 31.59, 28.24, 25.19, 22.42, 19.92, 17.65, 15.55, 13.54, 11.54, 9.47))

> d3 <- data.frame(X3 = c(0.04, 1.76,  3.61, 5.63, 7.89, 10.42, 13.19, 16.14, 19.25, 22.61, 26.29, 30.35, 34.83, 39.71, 44.97, 50.58, 56.47, 62.56, 68.79, 75.19, 81.82),
             Y3 = c(58.34, 55.97, 53.49, 50.89, 48.15, 45.27, 42.35, 39.43, 36.50, 33.57, 30.66, 27.85, 25.18, 22.66, 20.27, 18.02, 15.94, 14.02, 12.22, 10.48, 8.83))


my.lists <- list(d1, d2, d3)

df12 <- do.call(qpcR:::cbind.na, my.lists) 

现在我想计算 df12 数据框中每条轨迹的半径。 从每个轨迹我们可以得到有用的参数,例如弧长和弦长如下:

#for arc length
> library(geosphere) ## for calculating distance between successive points #step-1:

> d11 <-  data.frame(Distance = sqrt(diff(d1$X1)^2 + diff(d1$Y1)^2)) #step-2: 

> d11$csum1 <- ave(d11$Distance, FUN=cumsum) #step-3: 

#for chord length
> sqrt((d1[1,1]-d1[17,1])^2+(d1[1,2]-d1[17,2])^2)

有没有计算轨迹半径的方法?

提前致谢

"radius of a trajectory" 的意思还不完全清楚。 在这里,我假设您想要 fit 轨迹的数据点与圆 贯穿于这几点。我们将查看您的第一个示例。

library(pracma)
x <- d1$X1; y <- d1$Y1              # data points
res <- circlefit(x, y, fast=TRUE)   # "fitting a circle"
res
## RMS error: 0.2696326 
## [1]  93.85894 123.25466 118.51384

这将计算一个半径为 r0 = 118.51384 且圆心位于 (93.85894, 123.25466),RMS 误差约为 0.27。可视化:

x0 <- res[1]; y0 <- res[2]; r0 <- res[3]    # center and radius
ts <- seq(0, 2*pi, length.out = 100)
xs <- x0 + r0*cos(ts); ys <- y0 + r0*sin(ts)

plot(xs, ys, type='l', col = "red", asp=1)
points(x, y); grid()

不使用circlefit中的fast = FALSE选项,优化 由 optim() 控制的进程疯狂运行。

您还可以在程序包 conicfit 中应用函数 CircleFitBy...() -- 也许那是更好的选择。这也取决于你的 首选 'measure of fit' 是。