[R] Regular point pattern on multi-segment line

(Ted Harding) Ted.Harding at manchester.ac.uk
Fri May 29 14:14:30 CEST 2009

On 29-May-09 11:23:15, Pascal LAFFARGUE wrote:
> I need your help for the following purpose :
> I would like to create regularly spaced points on a multisegment
> line (a 'psp class' object).
> A function of the spatstat library( = pointsOnLines() ) is dedicated
> to that objective but the problem is that the probability of falling
> on a particular segment is proportional to the length of the segment.
> What about a solution using the whole multi-segment line and not 
> individual segments for points distribution ?
> Many thanks in advance for your help !
> Plaff

If I understand you right, one fairly obvious approach would be the

1. Let L be a vector of lengths of segments: L[i] is the length
   of segment i (say there are K segments).

2. Use X <- sort(sum(L)*runif(N)) to place N points uniformly
   on (0,sum(L))

3. Let Lcum <- c(0,cumsum(L))

4. whichsegs <- cut(X,breaks=Lcum,labels=FALSE)

Then whichsegs is a vector of N integers, from 1:K, which give the
segments into whch the N points in X fall.

So, for(i in (1:K)), extract the values of X[whichsegs==i],
subtract Lcum[i] from each, and then place the points at the
resulting distances along segment i.

I think this should work (not tested in detail)!

E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 29-May-09                                       Time: 13:14:27
------------------------------ XFMail ------------------------------

More information about the R-help mailing list