realbasic-games
[Top] [All Lists]

Re: 2D geometry/trigonometry question

To: REALbasic Games <realbasic-games at lists dot realsoftware dot com>
Subject: Re: 2D geometry/trigonometry question
From: William Squires <wsquires at satx dot rr dot com>
Date: Sun, 17 Jul 2005 09:03:26 -0500
Cc:
Delivered-to: realbasic-games at lists dot realsoftware dot com
References: <006b01c58a93$a03ac980$6400a8c0 at xp>
You have four basic cases:

1) x1 = x2 and y1 = y2. Two subcases:
   a) x3 <> x1 and y3 <> y1
Then the distance is simply Sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 -y2))
   b) x3 = x1 and y3 = y1
      Then the distance is 0.
2) The line defined by x1,y1 - x2,y2 is vertical; that is: If (x1 = x2) And (y1 <> y2) Then...
   Two subcases:
a) x3 = x1 (the point lies on the line that overlays the line segment)
      Two subcases here.
i) (y3 is not in the range y1..y2) (the point is outside the line segment) In which case, the closest point is the one (x1,y1 or x2,y2) with the smaller distance from y3 as measured by taking the Abs(y3 - yTest) where yTest is either y1 or y2.
      ii) (y3 is in the range y1..y2)
In which case the distance is 0, since the point lies in the line segment! b) x3 <> x1 (the point is not on the line that overlays the line segment)
       Two subcases:
i) y3 is not in the range y1..y2. In which case, take the Abs(y3 - yTest) - where yTest is either y1 or y2 - and see which gives you the smaller value. Use the pythagorean theorem to calculate
          the distance from x3,y3 to that closest point.
ii) y3 is in the range y1..y2. Then the closest point is (x1, y3) - which is also (x2, y3) 3) The line defined by x1,y1 - x2,y2 is not vertical. In this case, you'll be able to take the slope of the line segment. If the slope is 0, the line is horizontal; in which case, it proceeds similarly to
   case 2 above, but you swap the use of the y- and x-coordinates.
4) The line defined by x1,y1 - x2,y2 is neither horizontal nor vertical. Or just take the arctan(y2-y1 / x2-x1), but remember that the answer will be in radians, not degrees. This gives you the angle, alpha, of the line segment with the x-axis. Now rotate all the points about the origin by -alpha to return the 'diagram' to one in which the line segment is horizontal. You can now use case 3.


On Jul 17, 2005, at 12:51 AM, Martin Dillon wrote:

Hello all,

Given a line from x1,y1 to x2,y2 how can I calculate the minimum distance of a mousedown at arbitrary location x3,y3 to the line?

Thanks,
Martin
_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives of this list here:
<http://support.realsoftware.com/listarchives/lists.html>


William H Squires Jr
wsquires at satx dot rr dot com dot nospam <- remove the .nospam

_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives of this list here:
<http://support.realsoftware.com/listarchives/lists.html>

<Prev in Thread] Current Thread [Next in Thread>