1
function [k]=dist2polylines(xp,yp,pt)
3
//Author : Serge Steer 2002
5
// computes minimum distance from a point to a set of polylines
6
//xp xp : polylines coordinates (one polyline by column)
7
//d minimum distance to polyline
8
//pt coordinate of the polyline closest point
10
// if negative polyline closest point is a polyline corner:pt=[xp(-ind) yp(-ind)]
11
// if positive pt lies on segment [ind ind+1]
14
//mxx=max(xp)/10;mxy=max(yp)/10;
16
//ki=find((xp(1:$-1,:)<=(pt(1)+mxx)&(pt(1)-mxx)<xp(2:$,:))&..
17
// (yp(1:$-1,:)<=(pt(2)+mxy)&(pt(2)-mxy)<yp(2:$,:)))
21
cr=4*sign((xp(1:$-1,:)-x).*(xp(1:$-1,:)-xp(2:$,:))+..
22
(yp(1:$-1,:)-y).*(yp(1:$-1,:)-yp(2:$,:)))+..
23
sign((xp(2:$,:)-x).*(xp(2:$,:)-xp(1:$-1,:))+..
24
(yp(2:$,:)-y).*(yp(2:$,:)-yp(1:$-1,:)))
29
//projection on segments
36
kz=find(d_d==0);d_d(kz)=1
38
d_x=( dy.*(-x(:,2).*y(:,1)+x(:,1).*y(:,2))+dx.*(dx*pt(1)+dy*pt(2)))./d_d;
39
d_y=(-dx.*(-x(:,2).*y(:,1)+x(:,1).*y(:,2))+dy.*(dx*pt(1)+dy*pt(2)))./ d_d;
40
D=(d_x-pt(1)).^2+(d_y-pt(2)).^2 //distance with all points
42
[d,k]=min(D) //distance with all points
44
k=floor((ki(k)-1)/size(xp,1))+1