Geometry question (mapping)

Soldato
Joined
8 Mar 2007
Posts
10,938
Hi all, probably a simple problem for anyone with a decent knowledge of geometry.

Long story short I'm writing a small application and want to mimic a Sat Navs ability to tell you what road you are on at any given time.

Here's my problem.....

210h7x3.jpg


I know the coordinates of all three points; let's call them 'Ax & Ay', 'Bx & By' and 'Cx & Cy'. For my purposes 'A' is the end of the road, 'B' is the start of the road and 'C' is the vehicle.

So I guess the question is, mathematically how do I test if coordinate 'C' is on the line of a joined to 'B'?

* Ignore bends, I will always be using straight lines.
* The angle of the road/line used above is just an example, the road/line could be in * any orientation, 360 degrees round including 'A' being at the bottom and 'B' being at the top.
* 'C' can obviously be on any part of the line between 'A' and 'B'.
 
Last edited:
If (cy-ay)/(cx-ax) = (by-cy)/(bx-cx) then c is on the line ab

But that determines if it falls anywhere on an infinite straight line, not necessarily whether it falls within A and B.

Not only that but a horizontal line breaks it because you can't divide by zero (with a horizontal line cy is equal to ay producing '0' as the first divider. so it fails the any orientation stipulation.

Thanks for the starter though, I sure we're on the right line here (see what I did there).
 
Last edited:
By Jove I think I got it.....

If (ay = by and by = cy) and ((cx > ax and cx < bx) or (cx < ax and cx > bx) then
'on line (horizontal line) and between points a & b
ElseIf (ax = bx and bx = cy) and ((cy > ay and cy < by) or (cy < ay and cy > by) then
'on line (vertical line) and between points a & b
ElseIf ((cy-ay)/(cx-ax) = (by-cy)/(bx-cx) and ((cy > ay and cy < by) or (cy < ay and cy > by) ) then
'on line (any diagonal) and between points a & b
Else
'Not on line
End if

How accurate are the coordinates that you are getting? I know that GPS coordinates can be a little off, so if the coordinates that are being sent to your program are also a little off then you might have to alter the equation Haircut gave you a little bit to determine if it is on the line or not to something like this

Yeah you are right but building in a margin of error is pretty simple (albeit a longer code) I figured once I had the main maths bit down.
 
Last edited:
Here's my final pseudo code to work with, it checks that C is on the line (or near it within a +/- 5% margin of error) and that it is between the two other points for any orientation.

If (ax = bx and (ax/cx => 0.95 and <= 1.05)) and ((cy => ay and cy <= by) or (cy <= ay and cy >= by) then
'on line (vertical line) and between points a & b
ElseIf (ay = by and (ay/cy => 0.95 and <= 1.05)) and ((cx => ax and cx <= bx) or (cx <= ax and cx >= bx) then
'on line (horizontal line) and between points a & b
Elseif (((ay-cy)/(cx-ax)) / ((by-cy)/(cx-bx)) >= 0.95 and <=1.05) and ((cy >= ay and cy <= by) or (cy <= ay and cy >= by) ) then
'on line (any diagonal) and between points a & b
Else
'Not on line
End If

Think that works. Thanks to everyone that helped!
 
Last edited:
Thanks DP, looks like I may have some reading to do.

For my purposes I will be using Lats and Longs, which through my job I have access to, including lats/longs for every turn in a round (so every route breaks down into straight lines).

My application is only needed to cover one small city, it doesn't need to work globally so hopefully the curvature issue shouldn't cause too many problems. I'm going to have a play and do a few tests, my immediate thought is to apply a function that takes the input lats and longs and converts that to a coordinate that would be true if you were to take the square area I'm interested in then flattening it.

Thanks for all the input guys
 
Back
Top Bottom