
Apr 10th, 2006, 09:46 AM
#1
Thread Starter
New Member
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?

Apr 10th, 2006, 10:49 AM
#2
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:
Option Explicit
Private Sub Form_Load()
Dim X1 As Double, X2 As Double, Y1 As Double, Y2 As Double
Dim TestX As Double, TestY As Double
X1 = 11
Y1 = 59
X2 = 15
Y2 = 52
TestX = 31
TestY = 24
Debug.Print FindYForX(TestX, X1, Y1, X2, Y2) = TestY
End Sub
Public Function FindYForX(ByVal X As Double, ByVal X1 As Double, ByVal Y1 As Double, _
ByVal X2 As Double, ByVal Y2 As Double) As Double
Dim M As Double, B As Double
M = (Y1  Y2) / (X1  X2)
B = Y1  M * X1
FindYForX = M * X + B
End Function

Apr 10th, 2006, 02:26 PM
#3
Addicted Member
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...

Apr 20th, 2006, 09:13 AM
#4
Thread Starter
New Member
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= (y2y1)/(x2x1)X+ (y1[(y2y1)/(x2x1)]*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.

Apr 20th, 2006, 09:17 AM
#5
Re: points
My example should work for any kind of line, horizontal or vertical, it should not be any diference...

Apr 20th, 2006, 09:22 AM
#6
Thread Starter
New Member
Re: points
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.

Apr 20th, 2006, 09:41 AM
#7
Frenzied Member
Re: points
How about this:
VB Code:
Option Explicit
Private Sub Form_Load()
Debug.Print Intersects(2, 2, 0, 0, 3, 3)
Debug.Print Intersects(2, 3, 0, 0, 3, 3)
End Sub
Private Function Intersects(x As Long, y As Long, x1 As Long, y1 As Long, x2 As Long, y2 As Long) As Boolean
On Error GoTo ERR_Intersects
' Works out m, and c of y=mx+c, and checks to see if the point appears on the line
Dim m As Long 'Gradient
Dim c As Long 'Intercept
' Get m, and c of y=mx+c
m = (y2  y1) / (x2  x1)
c = y1  (m * x1)
' Check against the point for equality
Intersects = (y = ((m * x) + c))
Exit Function
ERR_Intersects:
Err.Raise Err.Number, Err.Source, Err.Description, Err.HelpFile, Err.HelpContext
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

Apr 20th, 2006, 09:43 AM
#8
Re: points
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

Apr 20th, 2006, 09:45 AM
#9
Re: points
yrwyddfa, that's exactly what my function is doing, except I test the Y outside the function....

Apr 20th, 2006, 09:48 AM
#10
Frenzied Member
Re: points
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

Apr 20th, 2006, 10:01 AM
#11
Thread Starter
New Member
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

Forum Rules

Click Here to Expand Forum to Full Width
