ParsePolygon implements Parcelable { public boolean containsPoint(ParseGeoPoint point) { double minX = coordinates.get(0).getLatitude(); double maxX = coordinates.get(0).getLatitude(); double minY = coordinates.get(0).getLongitude(); double maxY = coordinates.get(0).getLongitude(); for (int i = 1; i < coordinates.size(); i += 1) { ParseGeoPoint geoPoint = coordinates.get(i); minX = Math.min(geoPoint.getLatitude(), minX); maxX = Math.max(geoPoint.getLatitude(), maxX); minY = Math.min(geoPoint.getLongitude(), minY); maxY = Math.max(geoPoint.getLongitude(), maxY); } boolean outside = point.getLatitude() < minX || point.getLatitude() > maxX || point.getLongitude() < minY || point.getLongitude() > maxY; if (outside) { return false; } boolean inside = false; for (int i = 0, j = coordinates.size() - 1; i < coordinates.size(); j = i++) { double startX = coordinates.get(i).getLatitude(); double startY = coordinates.get(i).getLongitude(); double endX = coordinates.get(j).getLatitude(); double endY = coordinates.get(j).getLongitude(); boolean intersect = ((startY > point.getLongitude()) != (endY > point.getLongitude()) && point.getLatitude() < (endX - startX) * (point.getLongitude() - startY) / (endY - startY) + startX); if (intersect) { inside = !inside; } } return inside; } ParsePolygon(List<ParseGeoPoint> coords);  ParsePolygon(ParsePolygon polygon); protected  ParsePolygon(Parcel source);   ParsePolygon(Parcel source, ParseParcelDecoder decoder); List<ParseGeoPoint> getCoordinates(); void setCoordinates(List<ParseGeoPoint> coords); boolean containsPoint(ParseGeoPoint point); @Override boolean equals(Object obj); @Override String toString(); @Override int describeContents(); @Override void writeToParcel(Parcel dest, int flags); final static Creator<ParsePolygon> CREATOR; }
@Test public void testContainsPoint() { List<ParseGeoPoint> points = new ArrayList<>(); points.add(new ParseGeoPoint(0, 0)); points.add(new ParseGeoPoint(0, 1)); points.add(new ParseGeoPoint(1, 1)); points.add(new ParseGeoPoint(1, 0)); ParseGeoPoint inside = new ParseGeoPoint(0.5, 0.5); ParseGeoPoint outside = new ParseGeoPoint(10, 10); ParsePolygon polygon = new ParsePolygon(points); assertTrue(polygon.containsPoint(inside)); assertFalse(polygon.containsPoint(outside)); }