use of com.google.android.gms.maps.model.LatLng in project cw-omnibus by commonsguy.
the class PolyUtil method containsLocation.
/**
* Computes whether the given point lies inside the specified polygon.
* The polygon is always cosidered closed, regardless of whether the last point equals
* the first or not.
* Inside is defined as not containing the South Pole -- the South Pole is always outside.
* The polygon is formed of great circle segments if geodesic is true, and of rhumb
* (loxodromic) segments otherwise.
*/
public static boolean containsLocation(LatLng point, List<LatLng> polygon, boolean geodesic) {
final int size = polygon.size();
if (size == 0) {
return false;
}
double lat3 = toRadians(point.latitude);
double lng3 = toRadians(point.longitude);
LatLng prev = polygon.get(size - 1);
double lat1 = toRadians(prev.latitude);
double lng1 = toRadians(prev.longitude);
int nIntersect = 0;
for (LatLng point2 : polygon) {
double dLng3 = wrap(lng3 - lng1, -PI, PI);
// Special case: point equal to vertex is inside.
if (lat3 == lat1 && dLng3 == 0) {
return true;
}
double lat2 = toRadians(point2.latitude);
double lng2 = toRadians(point2.longitude);
// Offset longitudes by -lng1.
if (intersects(lat1, lat2, wrap(lng2 - lng1, -PI, PI), lat3, dLng3, geodesic)) {
++nIntersect;
}
lat1 = lat2;
lng1 = lng2;
}
return (nIntersect & 1) != 0;
}
use of com.google.android.gms.maps.model.LatLng in project cw-omnibus by commonsguy.
the class PolyUtil method encode.
/**
* Encodes a sequence of LatLngs into an encoded path string.
*/
public static String encode(final List<LatLng> path) {
long lastLat = 0;
long lastLng = 0;
final StringBuffer result = new StringBuffer();
for (final LatLng point : path) {
long lat = Math.round(point.latitude * 1e5);
long lng = Math.round(point.longitude * 1e5);
long dLat = lat - lastLat;
long dLng = lng - lastLng;
encode(dLat, result);
encode(dLng, result);
lastLat = lat;
lastLng = lng;
}
return result.toString();
}
use of com.google.android.gms.maps.model.LatLng in project cw-omnibus by commonsguy.
the class PolyUtil method decode.
/**
* Decodes an encoded path string into a sequence of LatLngs.
*/
public static List<LatLng> decode(final String encodedPath) {
int len = encodedPath.length();
// For speed we preallocate to an upper bound on the final length, then
// truncate the array before returning.
final List<LatLng> path = new ArrayList<LatLng>();
int index = 0;
int lat = 0;
int lng = 0;
for (int pointIndex = 0; index < len; ++pointIndex) {
int result = 1;
int shift = 0;
int b;
do {
b = encodedPath.charAt(index++) - 63 - 1;
result += b << shift;
shift += 5;
} while (b >= 0x1f);
lat += (result & 1) != 0 ? ~(result >> 1) : (result >> 1);
result = 1;
shift = 0;
do {
b = encodedPath.charAt(index++) - 63 - 1;
result += b << shift;
shift += 5;
} while (b >= 0x1f);
lng += (result & 1) != 0 ? ~(result >> 1) : (result >> 1);
path.add(new LatLng(lat * 1e-5, lng * 1e-5));
}
return path;
}
use of com.google.android.gms.maps.model.LatLng in project cw-omnibus by commonsguy.
the class SphericalUtil method isCCW.
/**
* Compute the ordering of 3 points in a triangle:
* Counter ClockWise (CCW) vs ClockWise (CW).
* Results are indeterminate for triangles of area 0.
* @return +1 if CCW, -1 if CW.
*/
static int isCCW(LatLng a, LatLng b, LatLng c) {
// Convert the 3 points to 3 unit vectors on the sphere.
LatLng[] points = new LatLng[] { a, b, c };
double[][] pointsR3 = new double[3][];
for (int i = 0; i < 3; ++i) {
LatLng latLng = points[i];
double lat = toRadians(latLng.latitude);
double lng = toRadians(latLng.longitude);
double[] r3 = new double[3];
r3[0] = cos(lat) * cos(lng);
r3[1] = cos(lat) * sin(lng);
r3[2] = sin(lat);
pointsR3[i] = r3;
}
// Compute the determinant of the matrix formed by the 3 unit vectors.
double det = pointsR3[0][0] * pointsR3[1][1] * pointsR3[2][2] + pointsR3[1][0] * pointsR3[2][1] * pointsR3[0][2] + pointsR3[2][0] * pointsR3[0][1] * pointsR3[1][2] - pointsR3[0][0] * pointsR3[2][1] * pointsR3[1][2] - pointsR3[1][0] * pointsR3[0][1] * pointsR3[2][2] - pointsR3[2][0] * pointsR3[1][1] * pointsR3[0][2];
// Threshold to sign
return det > 0 ? 1 : -1;
}
use of com.google.android.gms.maps.model.LatLng in project cw-omnibus by commonsguy.
the class PolyUtilTest method containsCase.
private static void containsCase(List<LatLng> poly, List<LatLng> yes, List<LatLng> no) {
for (LatLng point : yes) {
Assert.assertTrue(PolyUtil.containsLocation(point, poly, true));
Assert.assertTrue(PolyUtil.containsLocation(point, poly, false));
}
for (LatLng point : no) {
Assert.assertFalse(PolyUtil.containsLocation(point, poly, true));
Assert.assertFalse(PolyUtil.containsLocation(point, poly, false));
}
}
Aggregations