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?
Printable View
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?
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
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
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.
My example should work for any kind of line, horizontal or vertical, it should not be any diference...
Quote:
Originally Posted by CVMichael
i dont really understand how? so if y = what? when it is on the line
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]
You want to test is X AND Y is on the line right ?Quote:
Originally Posted by nick_p
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
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.Quote:
Originally Posted by CVMichael
Thanks guys
sorry abotu that was jsut being stupid i get it now