Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Point in Circle with JTS

Tags:

java

jts

I have a huge set of points already loaded within a plane I need to draw a circle/ellipse starting from a given point and a radius distance in meters then check which points are inside the circle.

I've already done this with a polygon with the within() method, but I can't find a way to draw a circle/ellipse without having to specify every point around the polygon.

Is there a way to do this on JTS or do I need another java library?

like image 754
user1132984 Avatar asked Jan 10 '12 17:01

user1132984


2 Answers

If I understood correctly you have the radius and the center, so you can draw a circle with JTS like this:

public static Geometry createCircle(double x, double y, final double RADIUS) {
    GeometricShapeFactory shapeFactory = new GeometricShapeFactory();
    shapeFactory.setNumPoints(32);
    shapeFactory.setCentre(new Coordinate(x, y));
    shapeFactory.setSize(RADIUS * 2);
    return shapeFactory.createCircle();
}
like image 92
EmmanuelMess Avatar answered Nov 14 '22 04:11

EmmanuelMess


You can just verify that the distance from the point is less than the radius. No need to draw the circle to know which points are inside it. For faster run times, compare the square of the distance with the square of the radius; this saves unnecessary square root operations.

For ellipses, the problem is only slightly harder, involving a quadratic form x^2 + k y^2.

like image 21
eh9 Avatar answered Nov 14 '22 02:11

eh9