R/triang_fun.R

Defines functions tri

Documented in tri

#' tri
#'
#' @param x1 numeric, x-coordinate of station 1
#' @param x2 numeric, x-coordinate of station 2
#' @param y1 numeric, y-coordinate of station 1
#' @param y2 numeric, y-coordinate of station 2
#' @param alpha1 numeric, alpha value of station 1
#' @param alpha2 numeric, alpha value of station 2
#' @export
#'









tri <- function(x1, y1, alpha1, x2, y2, alpha2) {
  # For Triangulation GK Coordinates are necesarry!
  # First calculate tan keeping in mind that 0° in geo-coordinates are 90° in a x-y plane
  ta1 <- (alpha1 %% 360) / 180 * pi
  ta2 <- (alpha2 %% 360) / 180 * pi
  if (((alpha1 - alpha2) %% 180) == 0) { # print("No triangulation possible: all three points are on one line")
    return(c(NA, NA))
  }

  # Findinf Intersection Using solver
  b <- c(x2 - x1, y2 - y1)
  a1 <- c(sin(ta1), cos(ta1))
  a2 <- c(-sin(ta2), -cos(ta2))
  a <- matrix(c(a1, a2), nrow = 2)
  l <- solve(a, b)
  px <- x1 + l[1] * sin(ta1)
  py <- y1 + l[1] * cos(ta1)

  if (l[2] > 0 & l[1] > 0) {
    return(data.frame(x = px, y = py))
  } else {
    return(data.frame(x = NA, y = NA))
  }
}
Nature40/tRackIT documentation built on Nov. 21, 2023, 3:43 a.m.