Results 1 to 11 of 11

Thread: points

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2006
    Posts
    5

    points

    i have and origin and a destination of a line stored as co ords of a grid (x,y)

    i need to calculate if another random point is on the path of that line

    any 1 know how?

  2. #2
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,794

    Re: points

    So I guess you want to find out if your point lies on the line ?

    I have a function I made where you give a line, and the an X on the line, and it returns Y coresponding to that X on the line.

    You can use the same function for your purposes.
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.     Dim X1 As Double, X2 As Double, Y1 As Double, Y2 As Double
    5.     Dim TestX As Double, TestY As Double
    6.    
    7.     X1 = 11
    8.     Y1 = 59
    9.    
    10.     X2 = 15
    11.     Y2 = 52
    12.    
    13.     TestX = 31
    14.     TestY = 24
    15.    
    16.     Debug.Print FindYForX(TestX, X1, Y1, X2, Y2) = TestY
    17. End Sub
    18.  
    19. Public Function FindYForX(ByVal X As Double, ByVal X1 As Double, ByVal Y1 As Double, _
    20.         ByVal X2 As Double, ByVal Y2 As Double) As Double
    21.    
    22.     Dim M As Double, B As Double
    23.    
    24.     M = (Y1 - Y2) / (X1 - X2)
    25.     B = Y1 - M * X1
    26.    
    27.     FindYForX = M * X + B
    28. End Function

  3. #3
    Addicted Member Rassis's Avatar
    Join Date
    Jun 2004
    Location
    Lisbon
    Posts
    248

    Re: points

    If points (X1; Y1) and (X2; Y2) belong to a straight line, then any point (x; y) will be on the path of this line if you replace x in the equation y = Y1 + [(Y2 Y1) / (X2 X1)]*(x X1) by x and you get y.

    Rui
    ...este projecto dos Deuses que os homens teimam em arruinar...

  4. #4

    Thread Starter
    New Member
    Join Date
    Apr 2006
    Posts
    5

    Re: points

    i needed it to work for diagonal lines as well really.

    i need to calculate if a given (X,Y) is on a line that spans from (x1,y1) to (x2,y2) i have the following

    0= (y2-y1)/(x2-x1)X+ (y1-[(y2-y1)/(x2-x1)]*x1) -Y

    but it only seems to work for horizontal lines my maths is pretty bad and i would really appreciate it if any one could explain or even better tell me how i could make it work as i am really lost.

  5. #5
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,794

    Re: points

    My example should work for any kind of line, horizontal or vertical, it should not be any diference...

  6. #6

    Thread Starter
    New Member
    Join Date
    Apr 2006
    Posts
    5

    Re: points

    Quote Originally Posted by CVMichael
    My example should work for any kind of line, horizontal or vertical, it should not be any diference...

    i dont really understand how? so if y = what? when it is on the line
    Last edited by nick_p; Apr 20th, 2006 at 09:31 AM.

  7. #7
    Frenzied Member yrwyddfa's Avatar
    Join Date
    Aug 2001
    Location
    England
    Posts
    1,253

    Re: points

    How about this:

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.  
    5.     Debug.Print Intersects(2, 2, 0, 0, 3, 3)
    6.     Debug.Print Intersects(2, 3, 0, 0, 3, 3)
    7.  
    8. End Sub
    9.  
    10. Private Function Intersects(x As Long, y As Long, x1 As Long, y1 As Long, x2 As Long, y2 As Long) As Boolean
    11.  
    12.     On Error GoTo ERR_Intersects
    13.  
    14.     ' Works out m, and c of y=mx+c, and checks to see if the point appears on the line
    15.    
    16.     Dim m As Long   'Gradient
    17.     Dim c As Long   'Intercept
    18.  
    19.     ' Get m, and c of y=mx+c
    20.     m = (y2 - y1) / (x2 - x1)
    21.     c = y1 - (m * x1)
    22.    
    23.     ' Check against the point for equality
    24.     Intersects = (y = ((m * x) + c))
    25.    
    26.     Exit Function
    27.    
    28. ERR_Intersects:
    29.     Err.Raise Err.Number, Err.Source, Err.Description, Err.HelpFile, Err.HelpContext
    30. End Function

    x,y = point of interest
    x1,y1 = origin of line
    x2,y2 = end of line

    Because y=mx+c is the equation of a straight line, and we know enough of the terms we can solve (mx+c) and see if it equals y; if this is true then the y=mx+c holds true, and therefore the point must be on that line.

    [edit] you might want to change the datatype into doubles if you want to use this for production code; I haven't adapted it properly - as I'm far too bone idle[/edit]
    Last edited by yrwyddfa; Apr 20th, 2006 at 09:46 AM.
    "As far as the laws of mathematics refer to reality, they are not certain; and as far as they are certain, they do not refer to reality." - Albert Einstein

    It's turtles! And it's all the way down

  8. #8
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,794

    Re: points

    Quote Originally Posted by nick_p
    i dont really understand how? so if y = what? when it is on the line
    You want to test is X AND Y is on the line right ?

    Lets say your X & Y are TestX and TestY
    You give to my function TestX, and returns the Y for that TestX
    Then you just Test to see if your TestY equals to the returned Y

    Just look at my example:

    The line coordinates:
    X1 = 11
    Y1 = 59

    X2 = 15
    Y2 = 52

    The test points X & Y:
    TestX = 31
    TestY = 24

    This line returns Y for TestX, and then tests if the returned Y = TestY....
    Debug.Print FindYForX(TestX, X1, Y1, X2, Y2) = TestY

  9. #9
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,794

    Re: points

    yrwyddfa, that's exactly what my function is doing, except I test the Y outside the function....

  10. #10
    Frenzied Member yrwyddfa's Avatar
    Join Date
    Aug 2001
    Location
    England
    Posts
    1,253

    Re: points

    Quote Originally Posted by CVMichael
    yrwyddfa, that's exactly what my function is doing, except I test the Y outside the function....
    I understand that I thought the fella was looking for more than a function that solved the problem - my version is annotated to help explain the mathematics behind it.
    "As far as the laws of mathematics refer to reality, they are not certain; and as far as they are certain, they do not refer to reality." - Albert Einstein

    It's turtles! And it's all the way down

  11. #11

    Thread Starter
    New Member
    Join Date
    Apr 2006
    Posts
    5

    Re: points

    Thanks guys

    sorry abotu that was jsut being stupid i get it now

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