点和矢量之间的相对角度
Relative angle between point and vector
我需要计算点数组和向量数组之间的角度(调整向量的方向)。我正在使用 this 的 angle2 函数(基于 atan2)来计算角度,但是我似乎做错了什么。
数据是2个连续轨道。一条轨道带有航向值,另一条轨道只是坐标。所以一个轨道(Easting2,Northing2)是一个矢量数组,另一个(Easting,Northing) - 只是点。我想我必须将这些点变成相对于另一个基于矢量的轨道的矢量,然后使用 atan2 方法。这是我的思考过程:
- 利用track2(Easting2, Northing2)调整track1(Easting, Northing;即
轨道 2 成为轨道 1)
的原点
- 如果track2走步长度为1,则计算一组坐标
与给定的 track2 路线相同的方向(这是 next.easting 和 next.northing)
- 计算重新定位的track1与
track2.
的虚点
但是,在此处的示例中,我希望第一行的角度约为 30 度,第二行的角度约为 50 度。我做错了什么??
玩具示例:
library(dplyr)
df <- structure(list(Easting = c(519984.801109577, 520254.016648783
), Northing = c(8015778.00697284, 8017130.41190275), Easting2 = c(520033.416692364,
518599.418722116), Northing2 = c(8029164.59837475, 8023952.71894817
), Course = c(195.6, 196)), .Names = c("Easting", "Northing",
"Easting2", "Northing2", "Course"), row.names = c(NA, -2L), class = c("tbl_df",
"tbl", "data.frame"))
# function from the linked post, to calculate angles between vectors
angle2 <- function(M,N){
atan2(N[2],N[1]) - atan2(M[2],M[1])
}
df %>%
mutate(Easting.adj = Easting - Easting2,
Northing.adj = Northing - Northing2,
Course.rad = Course * pi / 180,
Next.Easting2 = cos(Course.rad),
Next.Northing2 = sin(Course.rad)) %>%
rowwise() %>%
mutate(Angle = angle2(c(Next.Easting2, Next.Northing2), c(Easting.adj, Northing.adj)),
# convert back to degrees
Angle = Angle * 180 / pi)
这里有一张 Google 地球图像,如果两条线。
对于所提供的数字,图像并不完全准确,因为 Easting.adj
第 1 行为负数,第 2 行为正数。问题是因为数学角度是 counter-clockwise 从 x-direction,而您的航海路线似乎是从 north-direction 开始顺时针定义的。应该使用 90 - Course
:
df %>%
mutate(Easting.adj = Easting - Easting2,
Northing.adj = Northing - Northing2,
Course.rad = (90 - Course) * pi / 180,
Next.Easting2 = cos(Course.rad),
Next.Northing2 = sin(Course.rad)) %>%
rowwise() %>%
mutate(Angle = angle2(c(Next.Easting2, Next.Northing2), c(Easting.adj, Northing.adj)),
# convert back to degrees
Angle = Angle * 180 / pi)
以上代码生成 15.4 度和 29.6 度作为您的角度。或者,您可以保持角度:
df %>%
mutate(Easting.adj = Easting - Easting2,
Northing.adj = Northing - Northing2,
math_course = 90 - Course) %>%
rowwise() %>%
mutate(direction = atan2(Northing.adj, Easting.adj) * 180 / pi,
angle = direction - math_course)
我需要计算点数组和向量数组之间的角度(调整向量的方向)。我正在使用 this 的 angle2 函数(基于 atan2)来计算角度,但是我似乎做错了什么。
数据是2个连续轨道。一条轨道带有航向值,另一条轨道只是坐标。所以一个轨道(Easting2,Northing2)是一个矢量数组,另一个(Easting,Northing) - 只是点。我想我必须将这些点变成相对于另一个基于矢量的轨道的矢量,然后使用 atan2 方法。这是我的思考过程:
- 利用track2(Easting2, Northing2)调整track1(Easting, Northing;即 轨道 2 成为轨道 1) 的原点
- 如果track2走步长度为1,则计算一组坐标 与给定的 track2 路线相同的方向(这是 next.easting 和 next.northing)
- 计算重新定位的track1与 track2. 的虚点
但是,在此处的示例中,我希望第一行的角度约为 30 度,第二行的角度约为 50 度。我做错了什么??
玩具示例:
library(dplyr)
df <- structure(list(Easting = c(519984.801109577, 520254.016648783
), Northing = c(8015778.00697284, 8017130.41190275), Easting2 = c(520033.416692364,
518599.418722116), Northing2 = c(8029164.59837475, 8023952.71894817
), Course = c(195.6, 196)), .Names = c("Easting", "Northing",
"Easting2", "Northing2", "Course"), row.names = c(NA, -2L), class = c("tbl_df",
"tbl", "data.frame"))
# function from the linked post, to calculate angles between vectors
angle2 <- function(M,N){
atan2(N[2],N[1]) - atan2(M[2],M[1])
}
df %>%
mutate(Easting.adj = Easting - Easting2,
Northing.adj = Northing - Northing2,
Course.rad = Course * pi / 180,
Next.Easting2 = cos(Course.rad),
Next.Northing2 = sin(Course.rad)) %>%
rowwise() %>%
mutate(Angle = angle2(c(Next.Easting2, Next.Northing2), c(Easting.adj, Northing.adj)),
# convert back to degrees
Angle = Angle * 180 / pi)
这里有一张 Google 地球图像,如果两条线。
对于所提供的数字,图像并不完全准确,因为 Easting.adj
第 1 行为负数,第 2 行为正数。问题是因为数学角度是 counter-clockwise 从 x-direction,而您的航海路线似乎是从 north-direction 开始顺时针定义的。应该使用 90 - Course
:
df %>%
mutate(Easting.adj = Easting - Easting2,
Northing.adj = Northing - Northing2,
Course.rad = (90 - Course) * pi / 180,
Next.Easting2 = cos(Course.rad),
Next.Northing2 = sin(Course.rad)) %>%
rowwise() %>%
mutate(Angle = angle2(c(Next.Easting2, Next.Northing2), c(Easting.adj, Northing.adj)),
# convert back to degrees
Angle = Angle * 180 / pi)
以上代码生成 15.4 度和 29.6 度作为您的角度。或者,您可以保持角度:
df %>%
mutate(Easting.adj = Easting - Easting2,
Northing.adj = Northing - Northing2,
math_course = 90 - Course) %>%
rowwise() %>%
mutate(direction = atan2(Northing.adj, Easting.adj) * 180 / pi,
angle = direction - math_course)