rvectorangleprocedural

How to procedurally calculate angles between a great number of vectors?


I've got 4 vectors with their coordinates at different time steps.

lon <- list(505997.627175236, 505997.627175236, 505997.627175236, 505997.627175236, 505997.064187932, 505997.814896096,
          505997.843587834, 505997.880929633, 505996.906012923, 505998.486599226, 505998.075906002, 505998.079921271)   
lon <- do.call(rbind.data.frame, lon)

lat <- list(7941821.025438220, 7941821.025438220, 7941821.025438220, 7941821.025438220, 7941819.791667340, 7941821.329316000,
            7941821.741379530, 7941821.171989530, 7941819.103811300, 7941821.831421200, 7941822.024924560, 7941822.110412460)
lat <- do.call(rbind.data.frame, lat)

step <- list(1,1,1,1,2,2,2,2,3,3,3,3)
step <- do.call(rbind.data.frame, step)

allbuff <- cbind(lon, lat, step)
colnames(allbuff) <- c("lon", "lat", "STEP")

I've calculated the angles between the four vectors at step, step+1, ..., step+n with this following script:

M_PI <- 3.14159265359

output_angle = NULL

for (i in unique(allbuff$STEP)) {

  select = allbuff[allbuff$STEP == i, 1:2]

  result1 = atan2((select[1,2] - select[2,2]), (select[1,1] - select[2,1]))*(180/M_PI) # between 1 & 2
  result2 = atan2((select[1,2] - select[3,2]), (select[1,1] - select[3,1]))*(180/M_PI) # between 1 & 3
  result3 = atan2((select[1,2] - select[4,2]), (select[1,1] - select[4,1]))*(180/M_PI) # between 1 & 4
  result4 = atan2((select[2,2] - select[3,2]), (select[2,1] - select[3,1]))*(180/M_PI) # between 2 & 3
  result5 = atan2((select[2,2] - select[4,2]), (select[2,1] - select[4,1]))*(180/M_PI) # between 2 & 4
  result6 = atan2((select[3,2] - select[4,2]), (select[3,1] - select[4,1]))*(180/M_PI) # between 3 & 4

  result <- rbind(result1,result2,result3,result4,result5,result6)
  STEP <- c(i,i)
  result <- cbind(result, as.data.frame(STEP))

  output_angle = rbind(output_angle,result)

}

output_angle <- as.data.frame(output_angle)

It's working and not too long to code with a small number of vectors but with 1000 vectors, this way of coding can be very time consuming.

Therefore, is there a more efficient way (procedurally speaking) to calculate the angles between all the vectors at step n whatever the number of vector in input?


Solution

  • Maybe the base R code below can help a bit

    dfout <- do.call(rbind,
                     c(make.row.names = F,
                       lapply(split(allbuff,allbuff$STEP), function(v) 
                         data.frame(result = combn(nrow(v),2,function(k) atan2(diff(v[rev(k),2]),diff(v[rev(k),1])) )*(180/M_PI),
                                    STEP = unique(v[3]),
                                    row.names = NULL))))
    

    and you will get

    > dfout
           result STEP
    1     0.00000    1
    2     0.00000    1
    3     0.00000    1
    4     0.00000    1
    5     0.00000    1
    6     0.00000    1
    7  -116.02248    2
    8  -111.78912    2
    9  -120.61296    2
    10  -93.98304    2
    11  112.76891    2
    12   93.75220    2
    13 -120.09129    3
    14 -111.82589    3
    15 -111.32784    3
    16  -25.22807    3
    17  -34.45110    3
    18  -92.68914    3