[android] #4475 - Introducing LatLng.wrap() and removing usage of wrap() in JNI's nativeLatLngForPixel()

This commit is contained in:
Brad Leege
2016-03-25 15:34:43 -05:00
parent 8cdb4f74e1
commit 335e346ed1
4 changed files with 41 additions and 5 deletions

View File

@@ -6,6 +6,7 @@ import android.os.Parcelable;
import com.mapbox.mapboxsdk.constants.GeoConstants;
import com.mapbox.mapboxsdk.constants.MathConstants;
import com.mapbox.mapboxsdk.utils.MathUtils;
import java.io.Serializable;
@@ -118,6 +119,17 @@ public class LatLng implements ILatLng, Parcelable {
return altitude;
}
/**
* Return a new LatLng object with a wrapped Longitude. This allows original data object
* to remain unchanged.
* @return New LatLng object with wrapped Longitude
*/
public LatLng wrap(){
LatLng wrappedVersion = new LatLng(this);
wrappedVersion.setLongitude(MathUtils.wrap(wrappedVersion.getLongitude(), GeoConstants.MIN_LONGITUDE, GeoConstants.MAX_LONGITUDE));
return wrappedVersion;
}
@Override
public boolean equals(Object o) {
if (this == o) {

View File

@@ -24,4 +24,23 @@ public class MathUtils {
return Math.max(min, Math.min(max, value));
}
/**
* Constrains n to the given range (including min, excluding max) via modular arithmetic.
*
* Same formula as used in Core GL (math.hpp)
* std::fmod((std::fmod((value - min), d) + d), d) + min;
*
* @param value Value to wrap
* @param min Minimum value
* @param max Maximum value
* @return Wrapped value
*/
public static double wrap(double value, double min, double max) {
double delta = max - min;
double firstMod = (value - min) % delta;
double secondMod = (firstMod + delta) % delta;
return secondMod + min;
}
}