I have a set of X and Y points that builds a shape and I need to know if an object is inside it or not what is the calculation to it ?
X and Y coords example:
I've always done it like so:
Pick a point you know to be outside the shape.
Make a line between that point and the point you're trying to find whether it's inside the shape or not.
Count the number of sides of the shape the line crosses.
If the count is odd, the point is inside the shape.
If the count is even, the point is outside the shape.
You may start from this: http://en.wikipedia.org/wiki/Point_in_polygon
You also might look into JTS Topology Suite. And in particular use this function.
EDIT: Here is example using JTS:
import java.util.ArrayList;
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.LinearRing;
import com.vividsolutions.jts.geom.Point;
import com.vividsolutions.jts.geom.Polygon;
import com.vividsolutions.jts.geom.impl.CoordinateArraySequence;
public class GeoTest {
public static void main(final String[] args) {
final GeometryFactory gf = new GeometryFactory();
final ArrayList<Coordinate> points = new ArrayList<Coordinate>();
points.add(new Coordinate(-10, -10));
points.add(new Coordinate(-10, 10));
points.add(new Coordinate(10, 10));
points.add(new Coordinate(10, -10));
points.add(new Coordinate(-10, -10));
final Polygon polygon = gf.createPolygon(new LinearRing(new CoordinateArraySequence(points
.toArray(new Coordinate[points.size()])), gf), null);
final Coordinate coord = new Coordinate(0, 0);
final Point point = gf.createPoint(coord);
System.out.println(point.within(polygon));
}
}
Here is example using AWT (which is simpler and is part of Java SE):
import java.awt.Polygon;
public class JavaTest {
public static void main(final String[] args) {
final Polygon polygon = new Polygon();
polygon.addPoint(-10, -10);
polygon.addPoint(-10, 10);
polygon.addPoint(10, 10);
polygon.addPoint(10, -10);
System.out.println(polygon.contains(0, 0));
}
}