点与向量之间的相对角度

时间:2018-03-28 13:33:17

标签: r angle

我需要计算一个点数组和一个向量数组之间的角度(调整向量的方向)。我使用this中的angle2函数(基于atan2)计算角度,但我似乎做错了。

数据是2个连续曲目。一个轨道带有一个课程值,另一个轨道只是坐标。因此,一个轨道(Easting2,Northing2)是一个向量数组,另一个是(Easting,Northing) - 只是点。我想我必须将这些点设置为相对于另一个基于矢量的轨道的矢量,然后使用atan2方法。这是我的思考过程:

  1. 使用track2(Easting2,Northing2)调整track1的位置(Easting,Northing;即 轨道2成为track1的起源
  2. 如果track2采用长度为1的步长,则计算一组坐标 与给定的track2课程相同的方向(这是next.easting和next.northing)
  3. 计算重新定位的轨道1与之间的角度 轨迹的虚构点。
  4. 但是,在这里的示例中,我希望第一行的角度为~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 Earth图片。  enter image description here

1 个答案:

答案 0 :(得分:2)

图像对于提供的数字并不完全准确,因为Easting.adj对于行1是负的而对于行2是正的。问题是由于数学角度是从x方向逆时针定义的,而您的航海航线似乎是从北向顺时针定义的。一个人应该使用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)