Path: news.mathworks.com!not-for-mail From: <HIDDEN> Newsgroups: comp.soft-sys.matlab Subject: Re: distance between point and line segment Date: Wed, 2 Apr 2008 18:00:20 +0000 (UTC) Organization: AGCO Lines: 114 Message-ID: <ft0hjk$96o$1@fred.mathworks.com> References: <4a2f4ac1-c21e-4ebf-8530-352f15098a47@41g2000hsc.googlegroups.com> <d122bed7-41d5-446a-8b0a-e786bdb938fb@a22g2000hsc.googlegroups.com> <fsrkeb$os2$1@fred.mathworks.com> <fsrvq5$5cu$1@fred.mathworks.com> Reply-To: <HIDDEN> NNTP-Posting-Host: webapp-02-blr.mathworks.com Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 8bit X-Trace: fred.mathworks.com 1207159220 9432 172.30.248.37 (2 Apr 2008 18:00:20 GMT) X-Complaints-To: news@mathworks.com NNTP-Posting-Date: Wed, 2 Apr 2008 18:00:20 +0000 (UTC) X-Newsreader: MATLAB Central Newsreader 1073303 Xref: news.mathworks.com comp.soft-sys.matlab:460636 Thanks Roger. In the realm of GPS, latiudes and longitudes, the difference in the result is quite significant in terms of acuracy of distances in meters and centimeter. Chuk "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <fsrvq5$5cu$1@fred.mathworks.com>... > "Chukwuemeka Igwe" <chukigwe@yahoo.com> wrote in message <fsrkeb > $os2$1@fred.mathworks.com>... > > Yes thanks for your replies. > > > > However if I use the cross product and assume that the z > > axis have zero components I actually get some reuslts > > which are more than an order of magnitude bigger thant the > > case when I use the determinant to solve for two > > dimensions. > > Please what is happening to cause such a difference in > > results. > > > > Thanks > > > > Chuk > -------- > First of all, Chuk, let me apologize for the errors in the formulas I gave back > on Feb. 19, and for not noticing them this past Saturday. I cannot imagine > what I was thinking of. The 3D formula should read: > > d = norm(cross(Q2-Q1,P-Q1))/norm(Q2-Q1); > > and the 2D versions ought to read: > > d = abs(det([Q2-Q1,P-Q1]))/norm(Q2-Q1); % for col. vectors > d = abs(det([Q2-Q1;P-Q1]))/norm(Q2-Q1); % for row vectors. > > In this corrected form the 3D formula is identical to the formula (9) of the > website: > > http://mathworld.wolfram.com/Point-LineDistance3- Dimensional.html > > which ImageAnalyst mentioned. > > A note of caution! Formula (4) at that website, which ImageAnalyst also > mentioned, while being mathematically equivalent to its formula (9), is not as > computationally robust as (9) in cases where the triangle PQ1Q2 is nearly flat, > that is, where it has two angles nearly zero and one nearly pi. > > Here is a concrete example of such a loss of accuracy: > > Q1 = [0;0;0]; Q2 = [2;4;6]; P = [1.00001;1.99998;3.00001]; > > This point P will lie very close to the line Q1Q2 (actually very near its > midpoint.) We can then compare three different ways of computing the > orthogonal distance. First, use formula (4) > > d1 = sqrt(norm(Q2-Q1)^2*norm(P-Q1)^2-dot(Q2-Q1,P-Q1) ^2)/norm(Q2- > Q1); > > Then use my cross product formula which is also formula (9): > > d2 = norm(cross(Q2-Q1,P-Q1))/norm(Q2-Q1); > > and finally, as a check, directly compute the nearest point, R, on line Q1Q2 > and then compute its distance from P: > > R = (dot(P-Q2,Q1-Q2)*Q1+dot(P-Q1,Q2-Q1)*Q2)/dot(Q2- Q1,Q2-Q1); > d3 = norm(R-P); > > Now compare the answers using 'format long': > > [d1;d2;d3] > ans = > > 1.0e-04 * > > 0.24494825921622 > 0.24494897427992 > 0.24494897427992 > > Formula (4) is off in the 7th place while the other two methods preserve > normal matlab accuracy. > > Roger Stafford > >