Path: news.mathworks.com!not-for-mail
From: "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid>
Newsgroups: comp.soft-sys.matlab
Subject: Re: distance between point and line segment
Date: Tue, 1 Apr 2008 00:32:05 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 77
Message-ID: <fsrvq5$5cu$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>
Reply-To: "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid>
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 1207009925 5534 172.30.248.37 (1 Apr 2008 00:32:05 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Tue, 1 Apr 2008 00:32:05 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1187260
Xref: news.mathworks.com comp.soft-sys.matlab:460218


"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