~ubuntu-branches/ubuntu/hoary/scilab/hoary

« back to all changes in this revision

Viewing changes to macros/metanet/dist2polylines.sci

  • Committer: Bazaar Package Importer
  • Author(s): Torsten Werner
  • Date: 2005-01-09 22:58:21 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20050109225821-473xr8vhgugxxx5j
Tags: 3.0-12
changed configure.in to build scilab's own malloc.o, closes: #255869

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
function [k]=dist2polylines(xp,yp,pt)
 
2
//Copyright INRIA
 
3
//Author : Serge Steer 2002
 
4
 
 
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
 
9
//ind  
 
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]
 
12
 
 
13
// Copyright INRIA
 
14
//mxx=max(xp)/10;mxy=max(yp)/10;
 
15
//mxx=0;mxy=0;
 
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:$,:)))
 
18
 
 
19
  x=pt(1);y=pt(2);
 
20
 
 
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,:)))
 
25
  cr($+1,:)=0
 
26
  ki=find(cr==5);
 
27
 
 
28
  if ki<>[] then
 
29
    //projection on segments
 
30
    x=[xp(ki) xp(ki+1)];
 
31
    y=[yp(ki) yp(ki+1)];
 
32
    dx=x(:,2)-x(:,1);
 
33
    dy=y(:,2)-y(:,1);
 
34
    d_d=dx.^2+dy.^2;
 
35
 
 
36
    kz=find(d_d==0);d_d(kz)=1
 
37
 
 
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
 
41
    D(kz)=%inf
 
42
    [d,k]=min(D) //distance with all points
 
43
    if d<2 then
 
44
      k=floor((ki(k)-1)/size(xp,1))+1
 
45
    else
 
46
      k=[]
 
47
    end
 
48
  else 
 
49
    k=[]
 
50
  end
 
51
endfunction