How to find if an intersection occurs between two line segments represented by a Latitude and Longitude

好久不见. 提交于 2019-12-11 09:35:45

问题


In an application I am developing I have a number of lines represented by Latitude and Longitude values. I want to check if another line intersects any of these lines at any point.

Say I have the line A-B I want to know if it intersects the line C-D at any point. The first and second examples above would lead to no intersection whereas the third example would.

For my scenario the actual intersection point is not necassary for the application to function, so a simple yes/no to the intersection is all that is needed.

I understand that working with Latitude and Longitude values is not strictly accurate seeing as the world is an ellipsoid so the values would need converting first. However, to prove the concept I am working on, a line intersection algorithm that works for any orientation of line segments is what I am looking for.

I have seen several algorithms but have not managed to get any to work in practice.

Currently I have been looking at the method mentioned here How to compute line segment intersections on a map

I am using the code below and just using simple coordinates to prove it works, but from my testing it returns points of intersection even if they do not exist.

double lineABxLength = bX - aX;
double lineCDxLength = dX - cX;
double lineAByLength = bY - aY;
double lineCDyLength = dY - cY;

double lineDenomiter = (lineABxLength * lineCDyLength) - (lineAByLength * lineCDxLength);

double a = (aX * aY) - (bY * aX);
double b = (dX * cY) - (dY * cX);

double xIntersection = ((a * lineCDxLength) - (b * lineABxLength)) / lineDenomiter;
double bIntersection = ((a * lineCDyLength) - (b * lineAByLength)) / lineDenomiter;

来源:https://stackoverflow.com/questions/26640636/how-to-find-if-an-intersection-occurs-between-two-line-segments-represented-by-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!