I,m trying to draw a polygon shape of any kind using multiple vertices with path2d and I want to later on see if a determinate point is within its area using java.awt.geom.Area
public static boolean is insideRegion(Region region, Coordinate coord){
Geopoint lastGeopoint = null;
GeoPoint firstGeopoint = null;
final Path2D boundary = new Path2D.Double();
for(GeoPoint geoponto : region.getGeoPoints()){
if(firstGeopoint == null) firstGeopoint = geoponto;
if(lastGeopoint != null){
boundary.moveTo(lastGeopoint.getLatitude(),lastGeopoint.getLongitude());
boundary.lineTo(geoponto.getLatitude(),geoponto.getLongitude());
}
lastGeopoint = geoponto;
}
boundary.moveTo(lastGeopoint.getLatitude(),lastGeopoint.getLongitude());
boundary.lineTo(firstGeopoint.getLatitude(),firstGeopoint.getLongitude());
final Area area = new Area(boundary);
Point2D point = new Point2D.Double(coord.getLatitude(),coord.getLongitude());
if (area.contains(point)) {
return true;
}
return false
}
So I put together this really quick test.
This generates a random number of points at random locations.
It then uses the mouse click to determine if the mouse click falls within that shape
UPDATED
(Note, I changed the
g2d.draw
tog2d.fill
to make it easier to see the content area)Note, everything in red returns "true", everything else returns "false"...