2010-02-17 33 views
1

好的。這可能是更多的數學問題,但在這裏。如何檢查給定的經度值是否在任何經度範圍內?

我有一個經度值,比方說X.我想知道X是否落在任何兩個經度值之間。

例如,如果我的X是145,範圍是[21,-179]。範圍由Google Map API邊界給出,我可以在谷歌地圖上看到X確實落在該範圍內。

但是,我怎麼能真正計算出這個?

回答

1
// check if x is between min and max, inclusively 
if (x >= minLongitude && x <= maxLongitude) 
    return true; 
0
/* 
* 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); 
} 
相關問題