LatLng.wrap should return a new instance of LatLng (#10769)
LatLng.wrap shoudl wrap mulpiples of max value to max
This commit is contained in:
@@ -6,7 +6,7 @@ import android.os.Parcelable;
|
||||
import android.support.annotation.FloatRange;
|
||||
|
||||
import com.mapbox.services.android.telemetry.constants.GeoConstants;
|
||||
import com.mapbox.services.android.telemetry.utils.MathUtils;
|
||||
|
||||
|
||||
/**
|
||||
* A geographical location which contains a single latitude, longitude pair, with
|
||||
@@ -204,8 +204,33 @@ public class LatLng implements ILatLng, Parcelable {
|
||||
* @return new LatLng object with wrapped Longitude
|
||||
*/
|
||||
public LatLng wrap() {
|
||||
longitude = MathUtils.wrap(longitude, GeoConstants.MIN_LONGITUDE, GeoConstants.MAX_LONGITUDE);
|
||||
return this;
|
||||
return new LatLng(latitude, wrap(longitude, GeoConstants.MIN_LONGITUDE, GeoConstants.MAX_LONGITUDE));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Constrains value to the given range (including min & max) via modular arithmetic.
|
||||
* <p>
|
||||
* Same formula as used in Core GL (wrap.hpp)
|
||||
* std::fmod((std::fmod((value - min), d) + d), d) + min;
|
||||
*
|
||||
* Multiples of max value will be wrapped to max.
|
||||
*
|
||||
* @param value Value to wrap
|
||||
* @param min Minimum value
|
||||
* @param max Maximum value
|
||||
* @return Wrapped value
|
||||
*/
|
||||
static double wrap(double value, double min, double max) {
|
||||
double delta = max - min;
|
||||
|
||||
double firstMod = (value - min) % delta;
|
||||
double secondMod = (firstMod + delta) % delta;
|
||||
|
||||
if (value >= max && secondMod == 0) {
|
||||
return max;
|
||||
}
|
||||
return secondMod + min;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -12,6 +12,7 @@ import org.junit.rules.ExpectedException;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNotSame;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@@ -255,8 +256,17 @@ public class LatLngTest {
|
||||
|
||||
@Test
|
||||
public void testWrapped() {
|
||||
LatLng latLng = new LatLng(45.0, -185.0).wrap();
|
||||
assertEquals("longitude wrapped value", latLng.getLongitude(), 175.0, DELTA);
|
||||
LatLng originalLatLng = new LatLng(45.0, -185.0);
|
||||
LatLng newLatlng = originalLatLng.wrap();
|
||||
assertNotSame(" new wrapped LatLng is created", originalLatLng, newLatlng);
|
||||
assertEquals("longitude wrapped value", originalLatLng.getLongitude(), -185.0, DELTA);
|
||||
assertEquals("longitude wrapped value", newLatlng.getLongitude(), 175.0, DELTA);
|
||||
|
||||
newLatlng = new LatLng(45.0, 180.0).wrap();
|
||||
assertEquals("longitude wrapped max value", newLatlng.getLongitude(), 180.0, DELTA);
|
||||
|
||||
newLatlng = new LatLng(45.0, -180.0).wrap();
|
||||
assertEquals("longitude wrapped min value", newLatlng.getLongitude(), -180.0, DELTA);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user