How to check if three coordinates form a line

1.2k Views Asked by At

Assume I have three coordinates from a world map in Longitude + Latitude. Is there a way to determine these three coordinates form a straight line?

What if I was using a system with bounds that defines the 2 corners (northeast - southwest) in Long/Lat?

The long & lat are expressed in decimal degrees.

3

There are 3 best solutions below

1
On BEST ANSWER

If a point has longitude $\alpha$ (from $-180^\circ$ to $+180^°$) and latitude $\beta$ (from $-90^°$ to $+90^°$), the corresponding 3D point is $$ (x,y,z)=(\cos\alpha\cos\beta,\sin\alpha\cos\beta,\sin\beta).$$ If you have three such points, the determinant $$ \left|\begin{matrix}x_1&x_2&x_3\\y_1&y_2&y_3\\z_1&z_2&z_3\end{matrix}\right|$$ is zero if and only if the plane through the three points passes through the Earth center. In the presence of rounding errors aou will never have exactly zero, so the question is when to accept an almost zero value as good ...

A simplified alternative: If all your points are relatively close to one another (e.g. less than 100km apart and not too close to the poles), it is a not-too-bad approximation to work with the angles themselves (after all viewing the Earth as a sphere is also just an approximation) as $x$ and $y$ coordinates and check collinearity, that is $$ \left|\begin{matrix}\alpha_1&\alpha_2&\alpha_3\\\beta_1&\beta_2&\beta_3\\1&1&1\end{matrix}\right|\approx 0.$$

3
On

I'll assume that by "line" you mean "great circle" -- that is, if you want to go from A to C via the shortest possible route, then keep going straight until you circle the globe and get back to A, you'll pass B on the way.

The best coordinates for the question to be in are cartesian -- the 3D vector from the center of the earth to the point on the map. (Lat/long are two thirds of a set of "spherical coordinates", and can be converted to cartesian coordinates given the radius of the earth as $r$, though for this question $r$ doesn't matter).

Once you have those points, which each one being a 3D vector), find the plane containing them, and check that it includes the center of the earth.

Or, as a shortcut, just mark the points on a Gnomonic projection map, and use a ruler to see if they form a straight line there. Unfortunately, a given gnomonic projection map can't include the whole world.

0
On

The following MATLAB code might do the trick

function result = collinear(lat1, lon1, lat2, lon2, lat3, lon3)
% result = collinear(lat1, lon1, lat2, lon2, lat3, lon3)
%
% returns a measure of the collinearity of the points (lat1,lon1),
% (lat2,lon2), (lat3,lon3) defined as follows:
%
% Find the middle point (opposite the longest edge); for the sake
% of illustration assume this is point 2.  Compute bearings at
% point 2 for the line from 1 to 2 and from 2 to 3.  If the points
% are collinear, these bearings are equal.  So use the difference
% in bearings (in degrees) as the measure of collinearity.
%
% Requires the package
%  http://www.mathworks.com/matlabcentral/fileexchange/39108
%
% This version is not vectorized.  It would be relatively easy to
% make it vectorized.
  lata = [lat1; lat2; lat3];  lona = [lon1; lon2; lon3];
  latb = [lat2; lat3; lat1];  lonb = [lon2; lon3; lon1];
  [s12, azi1, azi2] = geoddistance(lata, lona, latb, lonb);
  imax = find(s12 == max(s12));
  imax = imax(1);
  ia = mod(imax,3)+1; ib = mod(imax+1,3)+1;
  azia = azi2(ia); azib = azi1(ib);
  result = azib - azia;
  if result >= 180
    result = result - 360;
  elseif result < -180
    result = result + 360;
end

Alternatively you can divide the area of the triangle (given by geodarea) by the longest sides to get a measure of how far the opposite point is from the longest side.