22# ' distppll.R
33# ' (formerly 'xysegment.R')
44# '
5- # ' $Revision: 1.24 $ $Date: 2026/05/07 05:08:01 $
5+ # ' $Revision: 1.26 $ $Date: 2026/05/10 03:56:17 $
66# '
77# ' Low level utilities for analytic geometry for line segments
88# '
2222# ' distances from each of a list of points p[i,]
2323# ' to each of a list of line segments l[i,]
2424# ' [interpreted code uses large matrices and 'outer()']
25- # ' [Fortran implementation included!]
25+ # ' [Partial C implementation included!]
2626# '
2727# ' NNdist2segs
2828# ' distance to nearest line segment, from each point in a list.
29+ # ' Efficient C code used.
2930# '
3031# ' Copyright (c) Adrian Baddeley and Rob Foxall 1997-2026
3132# ' GNU Public Licence (>= 2.0)
@@ -51,10 +52,11 @@ distpl <- function(p, l) {
5152 si <- dy / leng
5253 # ' back-rotated coords of p
5354 xpr <- co * xpl + si * ypl
54- ypr <- - si * xpl + co * ypl
55- # ' test
56- if ( xpr > = 0 && xpr < = leng )
55+ # # ypr <- - si * xpl + co * ypl
56+ if ( xpr > = 0 && xpr < = leng ) {
57+ ypr <- - si * xpl + co * ypl
5758 dmin <- min(dmin , abs(ypr ))
59+ }
5860 return (dmin )
5961}
6062
@@ -79,13 +81,14 @@ distppl <- function(p, l) {
7981 si <- dy / leng
8082 # ' back-rotated coords of p
8183 xpr <- co * xpl + si * ypl
82- ypr <- - si * xpl + co * ypl
84+ # # ypr <- - si * xpl + co * ypl
8385 # ' ypr is perpendicular distance to infinite line
8486 # ' Applies only when xp, yp in the middle
8587 middle <- (xpr > = 0 & xpr < = leng )
86- if (any(middle ))
88+ if (any(middle )) {
89+ ypr <- - si * xpl + co * ypl
8790 dmin [middle ] <- pmin.int(dmin [middle ], abs(ypr [middle ]))
88-
91+ }
8992 return (dmin )
9093}
9194
@@ -173,19 +176,25 @@ distppll <- function(p, l, mintype=0,
173176# ' (distance to) nearest segment
174177
175178distppllmin <- function (p , l , big = NULL ) {
179+ # # find distance from each point to nearest segment
180+ # # and identifier of nearest segment
176181 if (is.null(big )) {
177182 xdif <- diff(range(c(p [,1 ],l [, c(1 ,3 )])))
178183 ydif <- diff(range(c(p [,2 ],l [, c(2 ,4 )])))
179184 big <- 2 * (xdif ^ 2 + ydif ^ 2 )
180185 }
181186 z <- NNdist2segments(p [,1 ], p [,2 ],
182187 l [,1 ], l [,2 ], l [,3 ], l [,4 ],
183- big )
188+ bigvalue = big ,
189+ wantindex = TRUE , wantproj = FALSE )
184190 return (list (min.d = sqrt(z $ dist2 ), min.which = z $ index ))
185191}
186192
187193NNdist2segments <- function (xp , yp , x0 , y0 , x1 , y1 , bigvalue ,
188194 wantindex = TRUE , wantproj = FALSE ) {
195+ # # find distance from each point to nearest segment
196+ # # If 'wantindex' then also return index of segment
197+ # # If 'wantproj' then also return (x,y) and (tp) coordinates of projection
189198 np <- length(xp )
190199 ns <- length(x0 )
191200 dist2 <- rep(bigvalue , np )
0 commit comments