C function d = find_min_dist(lx,ly,x,y) C C Given a grid (x,y) and a line (lx,ly) compute the minimum distance C from each point of (x,y) to a point on the line. C C Written by George Beckett - Matlab 5.3 MEX, C Friday 9th March 2001. C SUBROUTINE mexFunction(nlhs, plhs, nrhs, prhs) INTEGER plhs(*), prhs(*) INTEGER nlhs, nrhs INTEGER m, n, n2 INTEGER plx, ply, px, py, pd C C Firstly, validate input and output arguments. C IF (nrhs .NE. 4) THEN CALL mexErrMsgTxt('Invalid no. input arguments.') ELSE IF (nlhs .NE. 1) THEN CALL mexErrMsgTxt('Invalid no. input arguments.') ENDIF C C Then read input arguments. C plx = mxGetPr(prhs(1)) ply = mxGetPr(prhs(2)) px = mxGetPr(prhs(3)) py = mxGetPr(prhs(4)) C C Get size of grid matrices and length of line vector. C m = mxGetM(prhs(3)) n = mxGetN(prhs(3)) n2 = mxGetN(prhs(1)) C C Allocate space for the minimum distance matrix C plhs(1) = mxCreateFull(m,n,0) pd = mxGetPr(plhs(1)) C C Step through the mesh one point at a time C CALL compmind(%val(px), %val(py), %val(pd), & %val(plx), %val(ply), m, n, n2) C RETURN C END C C This is the subroutine which computes the minimum distance C SUBROUTINE compmind(xmtx, ymtx, distmtx, lx, ly, m, n, n2) C PARAMETER (LDOMAIN = 100.0) C INTEGER m, n, n2 INTEGER ii, jj, kk REAL*8 xmtx(m,n), ymtx(m,n), distmtx(m,n) REAL*8 lx(n2), ly(n2) REAL*8 dist, mindist C DO 30 jj=1,n DO 20 ii=1,m mindist = LDOMAIN C DO 10 kk=1,n2 dist=(lx(kk)-xmtx(ii,jj))**2+(ly(kk)-ymtx(ii,jj))**2 C IF(dist .LT. mindist) mindist=dist 10 CONTINUE C distmtx(ii,jj)=mindist 20 CONTINUE 30 CONTINUE RETURN END