views:

99

answers:

2

Given two points, A and B, defined by longitude and latitude I want to determine if another point C is ~between~ A and B. ~between~ is hard for me to define. I don't mean on the line - it almost certainly won't be.

Geometric diagram

In this diagram, point C is ~between~ A and B because it is between the normals of points A and B and the line between them (normals denoted by thin line). Point D is not ~between~ A and B but it is ~between~ B and F.

Another way of saying this is that I want to determine if the triangles ABC and ABD are obtuse or not.

Note that the points will be very close together - within 10s of metres normally.

I'm thinking that the law of haversines may help but I don't know what the inverse of haversine is.

Many thanks for all help.

+3  A: 

First, start with translating your points to local tangent plane. We will use the fact that your triangles are much smaller than the earth's radius. (Tangent space is such that equal deltas in each of the two coordinates correspond to equal distances)

This is done by dividing longtitudes by sin(lat):

A_local_x = A_lat_rads;
A_local_y = A_lon_rads/sin(A_lat_rads);

Then,

Compute lengths:

double ABsquared = (A_local_x - B_local_x)*(A_local_x - B_local_x) + (A_local_y - B_local_y)*(A_local_y - B_local_y);
double BCsquared = ..., ACsquared.

Finally:

bool obtuse = (ABsquared+BCsquared < ACsquared) || (ABsquared+ACsquared < BCsquared); 

Obtuse means "it is not within the line", as you say. I am not checking whether triangle ABC is obtuse, but whether the angles at B and at A are obtuse. That's it.

note: I haven't tested this code. Please tell me how it works by plugging different points, if there's a bug I will fix it.

Pavel Radzivilovsky
Nice. Your distance formula should add the squares, of course. Do tangent planes guarantee angles being preserved? Or only equal distance in one system == equal distance in the plane?
Alok
Thanks, corrected the "-". Oops.Angles - yes. They are preserved. It's like mappping all points in the vicinity to a tangent plane.
Pavel Radzivilovsky
Pavel: For the simple points I've thrown at my implementation of this answer it works very well. Thank you.
Sarge
+3  A: 
Alok
Yes, Alok, you understand the problem correctly.
Sarge
+1; however, this would require translating to 3d cartesian coordinates (important). see my answer on how to avoid it.
Pavel Radzivilovsky