Hi Ron,

Never use trig function either, if you can avoid it, they're slow and hard to get 
right - too many special cases to handle. Using
only vector arithematic is much better.

In your case I would use vector "dot products" (is that the correct term?) to test 
whether or not point A's "foot print" is on the
B-B' vector, and only if so employ the old "distance-to-line" function.

In MB-like pseudo-code (let's rename B' to be C for clarity's sake):

Function Dot (p1x, p1y, p2x, p2y) as float
    Dot = (p1x * p2x) + (p1y * p2y)
End Function

If Dot(ax-bx, ay-by, cx-bx, cy-by) < 0 Then
    'point B is closest
ElseIf Dot(ax-cx, ay-cy, bx-cx, by-cy) Then
    'point C is closest
Else 'use foot print on vector
    'calculate line parameters a,b,c for vector B-C: ax + by + c
    a = cy - by
    b = bx - cx
    c = - (a * bx + b * by)
    dist = abs(a * ax + b * ay + c) / sqrt(a*a + b*b)
End If

HTH

Best regards/Med venlig hilsen
Lars V. Nielsen
GisPro, Denmark
http://www.gispro.dk/
http://hjem.get2net.dk/lars-online/
WGS84: 10.26'40"E 55.20'20"N
----- Original Message -----
From: "Ron Halliday (Portolan Geomatics)" <[EMAIL PROTECTED]>
To: "MapInfo-L" <[EMAIL PROTECTED]>
Sent: Thursday, April 03, 2003 7:49 AM
Subject: MI-L MI/MB - Shortest Distance to...


> Hello listers, here's a mathematical problem I have:
>
> I have point A and a line with start/end coordinates B and B'. Using
> MapBasic, how do I determine the shortest distance from point A to any
> part of that line segment, assuming that the only points on that line
> are B and B'? The only way I can think to do it is calculating the
> angle/azimuth of line B-B', then creating a line from point A at a 90�
> angle from this value, ensuring that it is long enough to pass through
> some part of B-B' (which would only be the case if neither B nor B' are
> actually the closest point...) Then I could use targeting commands to
> create a node where they overlap, and get the coordinate of this
> location.
>
> Now, I'm pretty sure I can program this. But, as many of us know,
> MapBasic applications tend to run faster if you can avoid object
> manipulation and work with pure mathematics. Is there anyone out there
> that would know how to solve this using just trig functions or calculus?
>
> Thanks in advance, I will sum up at the end!
> Ron
>
> Ron Halliday, Cartographer
> Portolan Geomatics Inc.
> www.portolan.ca
>


---------------------------------------------------------------------
List hosting provided by Directions Magazine | www.directionsmag.com |
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
Message number: 6193

Reply via email to