Computes 2D orthogonal distance between a point and a line given the points coordinates and the line's slope and intercept or model formula

dist.2d.line(x = NULL, y = NULL, slope = NULL, intercept = NULL, form = NULL)

Arguments

x

numeric, the x coordinate of the point

y

numeric, the y coordinate of the point

slope

numeric, the slope of the line

intercept

numeric, the slope of the line

form

formula of type lm describing the line, ignored if slope and intercept are specified

Value

The distance between the point and line

Details

if slope and intercept are missing, a model formula of form lm(y~x) can be passed to form.

Examples

x <- runif(1:10)
y <- x*0.5

xy.lm <- lm(y~x)

x.p <- y.p <- -3

#one way . . .
dist.2d.line(x.p,y.p,form=xy.lm)
#> [1] 1.341641

#or another
dist.2d.line(x.p,y.p,slope=coef(xy.lm)[2],intercept=coef(xy.lm)[1])
#> [1] 1.341641