简体   繁体   中英

Point in Circle with 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?

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();
}

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 + ky^2 .

You can simply buffer the circle center with a positive value like so:

Point centerPoint = ...;
Polygon circle = (Polygon) centerPoint.buffer(0.1);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM