Results 1 to 6 of 6

Thread: Line Intersecting a Point

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2002
    Posts
    3

    Question Line Intersecting a Point

    Hi all!
    I have an easy question for you math buffs (which I have started studying once again... math... not buffs):
    What is the equation to determine if a line intersects a given point? For example:
    Consider Line(x1,y1)-(x2,y2)
    Does this line intersect point xy?

    As far as how I plan on using it:
    A user may draw a line, then they may move that line by selecting it with a mouseup on the picture. I need to know if the user clicked the line, or clicked the background.

    Thanks!

  2. #2
    Frenzied Member Jotaf98's Avatar
    Join Date
    Jun 2000
    Location
    I'm not gonna give you my IP address! Ok... Portugal, South-Western Europe, 3rd rock from the sun (our star is easy to find, a 47 Ursae Majoris in the Milky Way :p )
    Posts
    1,457
    Aww man!!! I'm on holydays and now geometry has come to torture me again! You're really lucky that I learned that this year... hmm... Here's the equation:

    (x2-x1)/x = (y2-y1)/y

    If that doesn't work try switching the x and y (the ones without the numbers right after them). That should work
    Code:
    Temp = Me.GetIQ()
    'Error 9: Overflow
    'DON'T PANIC! :eek:

    To learn how to use realistic effects in your games like fire, rain, snow and magic effects, read my article on particles systems here.


    Jotaf's Theories!
    "Cats land on their feet. Toast lands peanut butter side down. A cat with toast strapped to its back will hover above the ground in a state of quantum indecision."

  3. #3

    Thread Starter
    New Member
    Join Date
    Aug 2002
    Posts
    3

    Errr...

    Thanks for your quick response, but that doesn't seem to work, with any of the numbers rearranged. Hmmn... let me try this:

    Public Sub DrawPoint(byval Pnt as XYPoint)
    '// draw a dot, green or red. Green if it is on the provided line.
    Dim Ln as ALine '// with x1,y1,x2,y2
    Dim RGBCol as RGBColor '// with R,G,B values

    Ln.x1 = 50
    Ln.y1 = 125
    Ln.x2 = 139
    Ln.y2 = 420

    If IsOnThisLine(Pnt,Ln)
    RGBCol.R = 0
    RGBCol.G = 255
    Else
    RGBCol.R = 255
    RGBCol.G = 0
    End If

    PictureTarget.PSet (Pnt.x,Pnt.y),RGB(RGBCol.R, RGBCol.G,0)

    End Sub

    Private Function IsOnThisLine(ByVal Pnt as XYPoint, ByVal Ln as ALine) As Boolean
    Dim Res1 as Single
    Dim Res2 as Single

    '.... super code here....

    End Function

  4. #4
    Frenzied Member Jotaf98's Avatar
    Join Date
    Jun 2000
    Location
    I'm not gonna give you my IP address! Ok... Portugal, South-Western Europe, 3rd rock from the sun (our star is easy to find, a 47 Ursae Majoris in the Milky Way :p )
    Posts
    1,457
    Oh ok, sorry. You're right, the right formula is:

    (X - X1) / (X2 - X1) = (Y - Y1) / (Y2 - Y1)

    Don't bother testing it, it doesn't work - you always get False. I think it's because VB always rounds the result of the divisions to fit in a Double or Single or whatever, so the comparison doesn't work... sorry, can't help

    Search the internet or something... I'm sure this has been used before, they must have used another method based on Bresenham's algorythm or something complicated like that
    Code:
    Temp = Me.GetIQ()
    'Error 9: Overflow
    'DON'T PANIC! :eek:

    To learn how to use realistic effects in your games like fire, rain, snow and magic effects, read my article on particles systems here.


    Jotaf's Theories!
    "Cats land on their feet. Toast lands peanut butter side down. A cat with toast strapped to its back will hover above the ground in a state of quantum indecision."

  5. #5

    Thread Starter
    New Member
    Join Date
    Aug 2002
    Posts
    3

    Thumbs up

    Actually, I did test it and with a little bit of modification it works well enough!

    result1 = (x - x1) / (x2 - x1)
    result2 = (Y - y1) / (y2 - y1)

    result1 = Round(result1, 1)
    result2 = Round(result2, 1)

    If result1 = result2 Then
    IsOnThisLine = True
    Else
    IsOnThisLine = False
    End If

    With a little additional logic (since its actually a line segment, not an infinite line) I will be now be able to determine if a user clicks close to the line! Yay!

    Thanks man!

  6. #6
    Frenzied Member Jotaf98's Avatar
    Join Date
    Jun 2000
    Location
    I'm not gonna give you my IP address! Ok... Portugal, South-Western Europe, 3rd rock from the sun (our star is easy to find, a 47 Ursae Majoris in the Milky Way :p )
    Posts
    1,457
    Heh you're welcome

    You're right it's for an infinite line sorry... I never tought of rounding the 2 members of the equation because I thought that each one was the slope of the line (a small fractional number)
    Heh anyways it's working so no problem
    Code:
    Temp = Me.GetIQ()
    'Error 9: Overflow
    'DON'T PANIC! :eek:

    To learn how to use realistic effects in your games like fire, rain, snow and magic effects, read my article on particles systems here.


    Jotaf's Theories!
    "Cats land on their feet. Toast lands peanut butter side down. A cat with toast strapped to its back will hover above the ground in a state of quantum indecision."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width