I am working with Latitude / Longitude coordinates in a google map.
I have two lines :
I then use the following formula to find the point where they cross.
var XAsum = A.LngStart - A.LngEnd;
var XBsum = B.LngStart - B.LngEnd;
var YAsum = A.LatStart - A.LatEnd;
var YBsum = B.LatStart - B.LatEnd;
var LineDenominator = XAsum * YBsum - YAsum * XBsum;
if(LineDenominator == 0.0)
return false;
var a = A.LngStart * A.LatEnd - A.LatStart * A.LngEnd;
var b = B.LngStart * B.LatEnd - B.LatStart * B.LngEnd;
var x = (a * XBsum - b * XAsum) / LineDenominator;
var y = (a * YBsum - b * YAsum) / LineDenominator;
This tells me that the lines do indeed cross and returns the x and y values.
However, when I plot the returned point, it is offset (not much) from the real intersection.
Is there a better and just as fast algorithm I could use which will return me the correct intersection point ?
It needs to be fast as I am iterating over a large number of lines (~1000).
EDIT : Note this is giving me an error offset of around 7.5 meters
I'm assuming the algorithm you're using is the one for finding line intersections on a Cartesian coordinate system (i.e. a plane). Unfortunately, the earth is not a plane (or even a sphere) so using that algorithm will introduce error. Google Maps uses an ellipsoid (specifically WGS84) to approximate the surface of the earth, so you'll need an algorithm for finding the intersections of arcs on an ellipsoid.
This page might contains some helpful information: http://mathhelpforum.com/calculus/90196-point-intersection-two-lines.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With