I am trying to see if a point is contained within a polygon using Path2D.
The last line, System.out.println(poly.contains(lat1, lon1)), prints "false" even though I know the coordinates(lat1, lon1) are within the polygon sapecified in "testBound". Is the ".contain()" not working? Am i missing something?
package poly;
import java.awt.Polygon;
import java.awt.geom.Path2D;
import java.util.ArrayList;
import java.util.Arrays;
public class Polygon3 {
public static final double lat1 = 40.1032946;
public static final double lon1 = -84.5110052;
public static final String testBound = "40.203294,-84.521005;40.203294,-84.501005;40.003294,-84.521005;40.003294,-84.501005";
public static void main(String[] args) {
String[] test = testBound.split(";");
ArrayList<Double> latList = new ArrayList<Double>();
ArrayList<Double> lonList = new ArrayList<Double>();
for(String t : test) {
String[] latlng = t.split(",");
latList.add(Double.parseDouble(latlng[0]));
lonList.add(Double.parseDouble(latlng[1]));
}
System.out.println(latList);
System.out.println(lonList);
Double latpoints[] = latList.toArray(new Double[latList.size()]);
Double lonpoints[] = lonList.toArray(new Double[lonList.size()]);
System.out.println(latpoints);
System.out.println(lonpoints);
Path2D poly = new Path2D.Double();
for(int i = 0; i < latpoints.length; i++) {
poly.moveTo(latpoints[i], lonpoints[i]);
}
poly.closePath();
String testing = poly.toString();
System.out.println(testing);
System.out.println(poly.contains(lat1, lon1));
}
}