Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
LatLng.wrap should return a new instance of LatLng (#10769)
Browse files Browse the repository at this point in the history
LatLng.wrap shoudl wrap mulpiples of max value to max
  • Loading branch information
osana committed Dec 21, 2017
1 parent c2250e6 commit 28a21f3
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
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
Expand Down Expand Up @@ -204,8 +204,33 @@ public double getAltitude() {
* @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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
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;

Expand Down Expand Up @@ -255,8 +256,17 @@ public void testParcelable() {

@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
Expand Down

0 comments on commit 28a21f3

Please sign in to comment.