-2

我有一个相当大的数据框(df),其中包含连续 x,y 坐标形式的路径信息:

df$x
df$y

有了这些数据,我想:
1. 计算一组连续向量
2. 确定每个向量之间的角度(以度为单位)
3. 计算数据框中满足某个阈值(即 <90 °)

谢谢!

请参阅此处的帖子以供参考

4

1 回答 1

0
require("ggplot2")

Hypocycloid <- function(num_points) {

  r = 1
  k = 3
  theta = seq(from = 0, to = 2*pi, length.out = num_points)

  x = r*(k - 1)*cos(theta) + r*cos((k - 1)*theta)
  y = r*(k - 1)*sin(theta) - r*sin((k - 1)*theta)

  df = data.frame(x = x, y = y)
  gg1 = ggplot(df,
               aes(x = x, y = y), 
               size = 1) +
        geom_path()
  print(gg1)

  return(df)
}

ComputeUnitVectors <- function(points_df) {
  npoints = nrow(points_df)
  vx = points_df$x[2:npoints] - points_df$x[1:(npoints-1)]
  vy = points_df$y[2:npoints] - points_df$y[1:(npoints-1)]
  length = sqrt(vx^2 + vy^2)
  return(data.frame(vx = vx/length, vy = vy/length))
}

ComputeAngles <- function(vectors_df) {

  Angle <- function(v1, v2) {
    return(acos(as.numeric(v1) %*% as.numeric(v2))*180/pi)
  }

  nvectors = nrow(vectors_df)
  v1 = vectors_df[1:(nvectors-1),]
  v2 = vectors_df[2:nvectors,]
  v_df = cbind(v1, v2)

  angle = apply(v_df, 1, function(row) {Angle(row[1:2], row[3:4])})
  return(data.frame(angle))
}

points.df = Hypocycloid(20)
vectors.df = ComputeUnitVectors(points.df)
print(vectors.df)
angles.df  = ComputeAngles(vectors.df)
print(angles.df)
于 2015-12-17T21:40:33.817 回答