简体   繁体   中英

Calculating shortest distance from point to line defined by intercept and slope in R

I looked at other questions like this , this and this , but all these calculate the shortest distance to a line segment defined by two endpoints, whereas I've not been able to do the same but for a line defined by an intercept and a slope.

This is my data, which I plot and add a line that will always have an intercept of 0 and slope defined by the means of the two variables.

df <- data.frame(x = seq(1, 10, 1),
                 y = seq(1, 10, 2),
                 id = head(letters, 10))

plot(df$x, df$y, 
     abline(a = 0, b = (mean(df$x) / mean(df$y))))    

I am trying to calculate the shortest distance from each point to the line.

Test this (Modified from here )

#Perpendicular distance from point 'a' to a line with 'slope' and 'intercept'
dist_point_line <- function(a, slope, intercept) {
    b = c(1, intercept+slope)
    c = c(-intercept/slope,0)       
    v1 <- b - c
    v2 <- a - b
    m <- cbind(v1,v2)
    return(abs(det(m))/sqrt(sum(v1*v1)))
}

dist_point_line(c(2,1), 1, 0)
#[1] 0.7071068

In your case you could do something like this

apply(df, 1, function(x) dist_point_line(as.numeric(x[1:2]), slope = 1, intercept = 0) )
 #[1] 0.0000000 0.7071068 1.4142136 2.1213203 2.8284271 3.5355339 2.8284271 2.1213203 1.4142136 0.7071068

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM