R/math.R

Defines functions pol2cart

Documented in pol2cart

##' Convert polar to cartesian coordinates
##'
##'
##' @param r Distance
##' @param theta Angle (default radians)
##' @param deg If specified, then theta is in degrees
##' @param recycle If specified, recycles \code{r}
##' @return Matrix of x,y coordinates
##' @export
pol2cart <- function(r, theta, deg = FALSE, recycle = FALSE) {
    if (deg) theta <- theta * pi/180
    if (length(r) > 1 && length(r) != length(theta) && !recycle)
        stop("'r' vector different length than theta, if recycling 'r' values is desired 'recycle' must be TRUE")
    xx <- r * cos(theta)
    yy <- r * sin(theta)
    ## Account for machine error in trig functions
    xx[abs(xx) < 2e-15] <- 0
    yy[abs(yy) < 2e-15] <- 0
    out <- cbind(xx, yy)
    colnames(out) <- c("x", "y")
    return( out )
}
nverno/stuff documentation built on May 24, 2019, 10:55 a.m.