http://www.mathworks.com/matlabcentral/newsreader/view_thread/311602
MATLAB Central Newsreader  Shortest distance from Matrix to a line segment
Feed for thread: Shortest distance from Matrix to a line segment
enus
©19942015 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Mon, 15 Aug 2011 20:03:28 +0000
Shortest distance from Matrix to a line segment
http://www.mathworks.com/matlabcentral/newsreader/view_thread/311602#849233
James D
Dear all,<br>
I have a problem that I am trying to solve.<br>
<br>
I have a matrix of 600 x 840 different positions of latitude and longitude and I am trying to find and output the shortest distance to a line segment.<br>
<br>
I have created the following for the distance to a point and vectorised it so that it works faster than a for statement.<br>
<br>
I was wondering how I would code the shortest distance to a particular line segment for each of the 600 x 840 points in the matrix.<br>
<br>
distancematrix=sqrt( bsxfun(@plus, ((yllcorner+600*spacing0.020833333:spacing:yllcorner+0.020833333).'centrallat).^2 ,...<br>
((xllcorner+0.020833333:spacing:xllcorner+840*spacing0.020833333)centrallong).^2 ) );<br>
<br>
I hope that you can help!<br>
Many thanks in advance,<br>
James.

Mon, 15 Aug 2011 20:11:28 +0000
Re: Shortest distance from Matrix to a line segment
http://www.mathworks.com/matlabcentral/newsreader/view_thread/311602#849235
James D
I forgot to say that<br>
yllcorner = 22<br>
xllcorner = 121<br>
spacing = 0.041666666<br>
<br>
and the line segment is from (27.222,128.111) to (32.111, 133.222).<br>
<br>
Cheers.<br>
<br>
<br>
"James D" <j.e.daniell@gmail.com> wrote in message <j2bu2g$ghi$1@newscl01ah.mathworks.com>...<br>
> Dear all,<br>
> I have a problem that I am trying to solve.<br>
> <br>
> I have a matrix of 600 x 840 different positions of latitude and longitude and I am trying to find and output the shortest distance to a line segment.<br>
> <br>
> I have created the following for the distance to a point and vectorised it so that it works faster than a for statement.<br>
> <br>
> I was wondering how I would code the shortest distance to a particular line segment for each of the 600 x 840 points in the matrix.<br>
> <br>
> distancematrix=sqrt( bsxfun(@plus, ((yllcorner+600*spacing0.020833333:spacing:yllcorner+0.020833333).'centrallat).^2 ,...<br>
> ((xllcorner+0.020833333:spacing:xllcorner+840*spacing0.020833333)centrallong).^2 ) );<br>
> <br>
> I hope that you can help!<br>
> Many thanks in advance,<br>
> James.

Mon, 15 Aug 2011 20:56:26 +0000
Re: Shortest distance from Matrix to a line segment
http://www.mathworks.com/matlabcentral/newsreader/view_thread/311602#849244
Roger Stafford
"James D" <j.e.daniell@gmail.com> wrote in message <j2bu2g$ghi$1@newscl01ah.mathworks.com>...<br>
> Dear all,<br>
> I have a problem that I am trying to solve.<br>
> <br>
> I have a matrix of 600 x 840 different positions of latitude and longitude and I am trying to find and output the shortest distance to a line segment.<br>
> <br>
> I have created the following for the distance to a point and vectorised it so that it works faster than a for statement.<br>
> <br>
> I was wondering how I would code the shortest distance to a particular line segment for each of the 600 x 840 points in the matrix.<br>
> <br>
> distancematrix=sqrt( bsxfun(@plus, ((yllcorner+600*spacing0.020833333:spacing:yllcorner+0.020833333).'centrallat).^2 ,...<br>
> ((xllcorner+0.020833333:spacing:xllcorner+840*spacing0.020833333)centrallong).^2 ) );<br>
> <br>
> I hope that you can help!<br>
> Many thanks in advance,<br>
> James.<br>
         <br>
That seems a remarkably inefficient way of finding the minimum distance from an evenlyspaced rectangular gridwork of points to a given point. You are having to compute 600*840 = 504,000 distances that way. It's very easy to find the gridwork coordinates of the four corners of a square (rectangle?) that immediately surrounds your given point. It just takes one subtraction, followed by one division, and then a call on 'ceil' and 'floor', for each of the two dimensions. Then you only have to find the minimum among four distances.<br>
<br>
Finding the minimum distance to a line segment is more complicated but I think the above reasoning can be applied in an analogous manner to greatly reduce the total number of distances that need to be computed. Of course you will have to decide precisely what you mean by the distance to a line segment. The usual definition is the distance to the orthogonal projection of a point onto the line of the line segment if the projected point lies within the segment, and otherwise the distance to the closer of the segment's two endpoints.<br>
<br>
Roger Stafford

Mon, 15 Aug 2011 21:45:29 +0000
Re: Shortest distance from Matrix to a line segment
http://www.mathworks.com/matlabcentral/newsreader/view_thread/311602#849257
Bruno Luong
"James D" <j.e.daniell@gmail.com> wrote in message <j2bu2g$ghi$1@newscl01ah.mathworks.com>...<br>
> Dear all,<br>
> I have a problem that I am trying to solve.<br>
> <br>
> I have a matrix of 600 x 840 different positions of latitude and longitude and I am trying to find and output the shortest distance to a line segment.<br>
> <br>
> I have created the following for the distance to a point and vectorised it so that it works faster than a for statement.<br>
> <br>
> I was wondering how I would code the shortest distance to a particular line segment for each of the 600 x 840 points in the matrix.<br>
> <br>
> distancematrix=sqrt( bsxfun(@plus, ((yllcorner+600*spacing0.020833333:spacing:yllcorner+0.020833333).'centrallat).^2 ,...<br>
> ((xllcorner+0.020833333:spacing:xllcorner+840*spacing0.020833333)centrallong).^2 ) );<br>
> <br>
> I hope that you can help!<br>
> Many thanks in advance,<br>
> James.<br>
<br>
I assume the rectangle is (long, lat) such that<br>
left <= long <=right (eqt1)<br>
bottom <= lat <=top (eqt2).<br>
<br>
The line segment in its parametric form is (x,t)<br>
x(t) = x1 + t*(x2x1)<br>
y(t) = y1 + t*(y2y1)<br>
0 <= t <= 1 (eqt3).<br>
<br>
The distance squared is d2<br>
d2 = (x(t)long)^2 + (y(t)lat)^2.<br>
<br>
It is clear that d2 is a quadratic form with respect to (3 x 1) vector [long,lat,t]'.<br>
<br>
The problem is minimize<br>
d2 such that box constraints (13).<br>
<br>
This can be solved with QUADPROG. There may be some function in FEX that can solve such problem.<br>
<br>
Bruno