R/distPointToLine.R

Defines functions distPointToLine

Documented in distPointToLine

distPointToLine <- function(pt, l1, l2){ # Finds shortest distance between pt and line, not line segment (defined by l1 and l2)

	if(is.matrix(pt)){
		if(nrow(pt) > 1){
			d <- rep(NA, nrow(pt))
			for(i in 1:length(d)){
				d[i] <- sqrt(sum(cprod(pt[i, ] - l1, pt[i, ] - l2)^2)) / sqrt(sum((l2 - l1)^2))
			}
			return(d)
		}
	}

	return(sqrt(sum(cprod(pt - l1, pt - l2)^2)) / sqrt(sum((l2 - l1)^2)))
}

Try the linkR package in your browser

Any scripts or data that you put into this service are public.

linkR documentation built on May 2, 2019, 2:14 p.m.