连续二维向量之间的角度 [英] Angles Between Continuous 2D Vectors

查看:54
本文介绍了连续二维向量之间的角度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在计算连续二维向量之间的顺时针角度时遇到了一些麻烦.当我在绘图上通过眼睛比较它们时,我计算出的角度似乎不正确.下面是我在 R 中的过程.

I'm having some trouble calculating the clockwise angles between continuous 2D vectors. My computed angles do not seem right when I compare them by eye on a plot. What follows is my process in R.

如有必要,安装并启用circular"包:

If necessary, install and enable the "circular" package:

install.packages('circular')
library(circular)

生成二维坐标的小数据框:

Generate a small data frame of 2D coordinates:

functest <- data.frame(x=c(2,8,4,9,10,7),y=c(6,8,2,5,1,4))

绘制点以供参考:

windows(height=8,width=8)
par(pty="s")
plot(functest, main = "Circular Functions Test")
## draw arrows from point to point :
s <- seq(length(functest$x)-1)  # one shorter than data
arrows(functest$x[s], functest$y[s], functest$x[s+1], functest$y[s+1], col = 1:3)

创建一个计算两个向量之间角度的函数:

Create a function that computes the angle between two vectors:

angle <- function(m)
{   # m is a matrix
  dot.prod <- crossprod(m[, 1], m[, 2])
  norm.x <- norm(m[, 1], type="2")
  norm.y <- norm(m[, 2], type="2")
  theta <- acos(dot.prod / (norm.x * norm.y))
  as.numeric(theta) # returns the angle in radians
}

生成以度为单位的罗盘角度向量(顺时针旋转):

Generate a vector of compass angles in degrees (clockwise rotation):

functest_matrix <- cbind(x = functest$x,y = functest$y)
moves <- apply(functest_matrix, 2, diff)
tst <- lapply(seq(nrow(moves) - 1), function(idx) moves[c(idx, idx + 1), ])
functest_angles <- vapply(tst, angle, numeric(1))
functest_object <- circular(functest_angles, type="angles", units="radians", zero=0, rotation = "counter")
functest_convert <- conversion.circular(functest_object, type = "angles", units = "degrees", rotation = "clock", zero = pi/2)
functest_compass <- lapply(functest_convert, function(x) {if (x < 0) x+360 else x}) # converts any negative rotations to positive

当我尝试将弧度的正常"逆时针角度转换为度数的顺时针罗盘角度时,我怀疑在我的最后三行代码中可能出现问题.任何帮助将不胜感激!

I suspect something wrong may be occuring in my last three lines of code when I try to convert "normal" counterclockwise angles in radians to clockwise compass angles in degrees. Any help would be greatly appreciated!

推荐答案

不知道 R,但看到您使用标量积计算向量之间的角度.

Don't know R but see that you calculate angle between vectors using scalar product.

请注意,结果角度不是定向的 - 既不是顺时针也不是逆时针(考虑到标量积对矢量交换不敏感).

Note that resulted angle is not directed - it is neither clockwise, nor counterclockwise (consider that scalar product is insensitive to vector exchange).

如果您确实需要定向角(旋转第一个向量以使其与第二个向量共线所需的角度),则必须应用 ArcTan2 (atan2)方法
(结果范围通常是-Pi..Pi)

If you really need directed angle (the angle needed to rotate the first vector to make it collinear with the second one), you have to apply ArcTan2 (atan2) approach
(result range usually is -Pi..Pi)

Theta = ArcTan2(CrossProduct(v1,v2), DotProduct(v1,v2))

这篇关于连续二维向量之间的角度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆