R/getangle2d.R

Defines functions getAngle2d

Documented in getAngle2d

#' Angles between vectors.
#'
#' Calcul the angles of two set of vectors.
#'
#' @param x0,y0 the x and y coordinates of the first set of vector.
#' @param x1,y1 the x and y coordinates of the second set of vector.
#' @param rad a logical. Should angles be expressed radians?
#'
#' @keywords angles, vectors
#'
#' @export
#'
#' @examples
#' # Example:
#' plot0(c(-10, 10), c(-10, 10))
#' arrows(c(0, 3), c(0, 0), c(3, 0), c(3, -3))
#' cool <- getAngle2d(c(0, 3), c(0, 0), c(3, 0), c(3, -3))
#'
getAngle2d <- function(x0, y0, x1, y1, rad = FALSE) {
    sz <- max(lengths(list(x0, y0, x1, y1)))
    x0 <- rep_len(x0, sz)
    x1 <- rep_len(x1, sz)
    y0 <- rep_len(y0, sz)
    y1 <- rep_len(y1, sz)
    #
    dstc <- sqrt((x1 - x0) * (x1 - x0) + (y1 - y0) * (y1 - y0))
    stopifnot(!sum(dstc == 0))
    cx <- acos((x1 - x0) / dstc)
    #
    ang <- cx * (-1 + 2 * ((y1 - y0) > 0))
    if (!rad) {
        ang <- ang * 180 / pi + (ang < 0) * 360
    }
    #
    ang
}
KevCaz/graphicsutils documentation built on Sept. 16, 2022, 10:05 a.m.