home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!ferkel.ucsb.edu!taco!rock!stanford.edu!ames!sun-barr!cs.utexas.edu!zaphod.mps.ohio-state.edu!darwin.sura.net!sgiblab!sgigate!sgi!fido!blivet!reuel
- From: reuel@blivet.houst.sgi.com (Reuel Nash)
- Newsgroups: comp.graphics
- Subject: Re: Quickest way to calculate the distance between a point and a line
- Date: 12 Nov 1992 23:57:44 GMT
- Organization: Silicon Graphics, Inc. Mountain View, CA
- Lines: 66
- Message-ID: <1dur1oINNede@fido.asd.sgi.com>
- References: <BxML0r.II@vuse.vanderbilt.edu>
- NNTP-Posting-Host: blivet.houst.sgi.com
-
-
- In article <BxML0r.II@vuse.vanderbilt.edu>, brandon@se.vanderbilt.edu writes:
- > Hi, I've checked the FAQ and can't find this one, even though its quite basic.
- >
- > I'm developing a dynamic graphic simulation, and I need advice on the subject.
- > I have many circles, all the same radius, flowing down a channel
- > (in the future they will be spheres in a tube.)
- >
- > I need the quickest algorithm for calculating the distance between a
- > point (x,y) and a line f(x). I will have an initial distance which will be updated at
- > each major time slice.
- >
- > The straightforward way is iteratively better guesses until the perpendicular
- > is found which passes through (x,y). Is there a quicker way? I need to check
- > many of these and want the quickest algorithm.
- >
-
- Find the parametric equations for the line:
-
- y = a * t + c1
- x = b * t + c2
-
- There are lots of ways to do this, but one way is:
- If f(x) = slope * x + y_int (original line)
-
- x = 1 * t + 0 (parametric eqs. for original line)
- y = slope * t + y_int
-
- -> a = 1, b = slope, c1 = 0, c2 = y_int
-
- The parametric equations for a line perpendicular to the original line through
- the point (X,Y) will be:
-
- y = -b * s + Y (param. eqs. for perpendicular line thru X,Y)
- x = a * s + X
-
- These two lines cross at the closest point, so equate coordinates and solve:
-
- a * t + c1 = -b * s + Y
- b * t + c2 = a * s + X
-
- or
- a * t + b * s + c1 - Y = 0
- b * t - a * s + c2 - X = 0
-
- find s,t. (really only need one of s or t)
-
- (2 linear equations in 2 unknowns -- I'll let you do it ;-)
-
-
- Then substitute t back into the parametric equations for the original line
- and find Xl,Yl (the point on the line closest to X,Y)
-
- Then use the distance formula on (Xl,Yl) and (X,Y).
-
-
-
- No iteration at all!
-
-
- --
-
- Reuel Nash (713)293-9292 FAX (713)293-0503
- Silicon Graphics, 11490 Westheimer Suite 100, Houston, TX 77077
-
- Visualize Whirled Peas
-