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

A fully vectorised calculation of distances from a given set of points to a set of segments.
1.2K Downloads
Updated 13 Dec 2011

View License

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.

Cite As

Andrei Bejan (2024). Calculation of distances from a given set of points to a set of segments (https://www.mathworks.com/matlabcentral/fileexchange/28268-calculation-of-distances-from-a-given-set-of-points-to-a-set-of-segments), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2009a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Published Release Notes
1.4.0.0

A third output argument is added. This explicitly indicates whether the closest points on the segments are inner points or the end points.

1.3.0.0

one-dimensional case is added (p=1)

1.2.0.0

minor corrections in the description of the function csmv

1.1.0.0

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

1.0.0.0