views:

80

answers:

2

OK. This might be more of a math question but here goes.

I have a longitude value, let's say X. And I want to know if X falls between any two longitude values.

For example, if my X is 145 and the range is [21, -179]. The range is given by the Google Map API bounds and I can see on the google map that X does fall within that range.

However, how can I actually calculate this?

Thank you in advance!

A: 
// check if x is between min and max, inclusively
if ( x >= minLongitude && x <= maxLongitude ) 
   return true;
Muad'Dib
A: 
/*
 * Return true if and only if the longitude value lng lies in the range [min, max].
 *
 * All input values should be in the range [-180, +180].
 * The test is conducted clockwise.
 */
private boolean isLongitudeInRange(double lng, double min, double max) {

    assert(lng >= -180.0 && lng <= 180.0);
    assert(min >= -180.0 && min <= 180.0);
    assert(max >= -180.0 && max <= 180.0);

    if (lng < min) {
        lng += 360.0;
    }

    if (max < min) {
        max += 360.0;
    }

    return (lng >= min) && (lng <= max);
}
richj