Check if an array of points is inside an array of rectangles?

六眼飞鱼酱① 提交于 2019-12-23 04:53:07

问题


I have a list of vertices and a list of regions (which are square/rectangle) shaped. Vertex has x and y coordinates, and a region has (x, y, height and width). How can I efficiently check which vertex lies in which region for every vertex/region?

EDIT:

This is the code I wrote to do this.

                if (!g.getVertices().isEmpty()) {

                for (int i = 0; i < g.getVertices().size(); i++) {

                    Vertex v = g.getVertices().get(i);
                    Point vertexPoint = new Point(v.getX(), v.getY());

                    for (int j = 0; j < g.getNumberOfRegions(); j++) {

                        int x = g.getRegions().get(j).getX();
                        int y = g.getRegions().get(j).getY();
                        int height = g.getRegions().get(j).getHeight();
                        int width = g.getRegions().get(j).getWidth();

                        Grid regionGrid = new Grid(j+1, x, y, height, width);

                        Rectangle regionRectangle = new Rectangle(x, y, height, width);
                        if (regionRectangle.contains(vertexPoint)) {
                            System.out.println("Vertex " + v + " lies inside region " + regionGrid.getRegionID());
                        }
                    }

                }
            }

EDIT 2: I used this to generate the regions, but I need a way to assign each region in the grid a regionID from left to right. For example:

1 - 2 - 3
4 - 5 - 6 
7 - 8 - 9

for a 3x3 grid. At the moment it is in the following form:

1 - 1 - 1
2 - 2 - 2
3 - 3 - 3

                for (int i = 0; i < rowValue; i++) {

                for (int j = 0; j < columnValue; j++) {

                    Grid r = new Grid(0, 20 + i * size, 20 + j * size, size, size);
                    r.setRegionID(j + 1);
                    g.addRegion(r);
                }

            }

回答1:


checking if a vertex is inside a square or a circle can be done in O(1). you can do it with library function or elementary math. so the works algorithm you can create is O(#vertices * #regions). you can try to optimise by sorting the vertices and regions by X-axis and then by Y-axis and try to eliminate checking that for sure return false. but seems that in pessimistic scenario you will still have O(#vertices * #regions) time.




回答2:


You can probably use the Core Java libraries itself:

    List<Rectangle2D.Double> rectangles = Arrays.asList(
                                                new Rectangle2D.Double(0d, 0d, 100d, 100d),
                                                new Rectangle2D.Double(100d, 0d, 100d, 100d),
                                                new Rectangle2D.Double(0d, 100d, 100d, 100d),
                                                new Rectangle2D.Double(100d, 100d, 100d, 100d));

    Point2D.Double aPoint = new Point2D.Double(30d, 40d);

    for (Rectangle2D.Double rectangle:rectangles){
        if (rectangle.contains(aPoint)){
            System.out.println(rectangle + " has the point " + aPoint);
        }
    }



回答3:


Working with plane geometry is extremely easy while using JTS. You can try convert the objects you are using to JTS-specific.



来源:https://stackoverflow.com/questions/11817227/check-if-an-array-of-points-is-inside-an-array-of-rectangles

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!