Code covered by the BSD License

### Highlights from Calculation of distances from a given set of points to a set of segments

4.5
4.5 | 2 ratings Rate this file 3 Downloads (last 30 days) File Size: 3.07 KB File ID: #28268 Version: 1.4

# Calculation of distances from a given set of points to a set of segments

### Andrei Bejan (view profile)

22 Jul 2010 (Updated )

A fully vectorised calculation of distances from a given set of points to a set of segments.

File Information
Description

CSMV calculates distances from a given set of points to a set of linear segments

SYNOPSIS: [squared_distances_to_segments I t]=csmv(P,R,Q,to_plot)
[squared_distances_to_segments I t]=csmv(P,R,Q,...)

INPUT P,R : arrays of coordinates of 2n segment endpoints
(i.e., if there are n segments considered in p-dimensional
Euclidean space, than P and R are n-by-p matrices); the sizes of P
and R should coincide; p should be greater or equal than unity.
Q : an array of coordinates corresponding to the set of m points
from which distances to the segments PR are calculated
(Q should therefore be an m-by-p matrix).
to_plot : (optional) graphical output.
Any non-zero value would cause the graphical output. There will be
no graphical output by default (if this parameter is not passed).

OUTPUT squared_distances_to_segments : an n-by-m matrix whose j-th column
contains n squared distance from the j-th point of Q to the segments PR.
I : a vector of m elements representing indices of the segments PR
which are the closest to points from Q.
t : an n-by-m matrix whose ij-th element is a real number between 0
and 1 equal to |P(i,:)Qi*(j,:)|/|P(i,:)R(i,:)|, where Qi*(j,:) is the
point on the segment P(i,:)R(i,:) closest to Q(j,:). Thus, for example, if
P(i,:) is closest to Q(j,:), then t(i,j)=0; if R(i,:) is closest to
Q(j,:), then t(i,j)=1.

NOTE: (i) the dimensions of P and R should coincide!
(ii) you may encounter an error if some points P and R coincide;
this can be easily avoided by suitably changing the code - feel
free to do so.

Acknowledgements

This file inspired Simple Map Matching Algorithm For Edges And Arcs.

MATLAB release MATLAB 7.8 (R2009a)
06 Dec 2011 leila

### leila (view profile)

Does the tool specify whether closest point falls within the line segment or not?

Comment only
20 Aug 2010 Ben

### Ben (view profile)

31 Jul 2010 Andrei Bejan

### Andrei Bejan (view profile)

Steve, thanks!

I think I should complete this function by considering the case p=1. Will do soon.

Comment only
31 Jul 2010 Steve

### Steve (view profile)

This is exactly what I needed. Thanks very much, that's a great contribution.

22 Jul 2010 1.1

Small changes in the description of this function have been made.

23 Jul 2010 1.2

minor corrections in the description of the function csmv

20 Aug 2010 1.3