[R] distances from points to line
Hisaji ONO
ono96 at fa2.so-net.ne.jp
Fri Nov 23 14:21:05 CET 2001
Hi.
>
> I now wonder, is there a function to create a line object like a
> watercourse and then calculate the distances between many points in space
> and this line?
>
I've tried to port a fortran code of a distance from a line segment in
Bowyer, A. and Woodwark, J. (1983):"a programmer's geometry", Butterworth,
47-48, to R function. But I know your request was calculating points
between polyline. So this function is not too enough. Just try this, if you
are interested.
#
# a function that returns a distance from a line
#
# a point coordinate: xj, yj
#
# a line segment coordinates: xk, yk, xl, yl
#
p2linedist<-function(xj, yj, xk, yk, xl, yl){
xkj<- xk - xj
ykj<- yk - yj
xlk<- xl - xk
ylk<- yl - yk
denom <- xlk * xlk + ylk * ylk
distance <- 0
if(denom < 1.0e-6){
sqrt(xkj*xkj + ykj*ykj) # segments ends coinside
}else{
t <- -(xkj*xlk + ykj*ylk)/denom
t <- min(max(t,0),1)
xfac <- xkj + t * xlk
yfac <- ykj + t * ylk
sqrt(xfac*xfac + yfac*yfac)
}
}
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list