Storing objects for locating by x,y coordinates

你离开我真会死。 提交于 2019-11-27 07:53:02

Quadtrees seem to solve the specific problem I asked. Kd-Trees are a more general form, for any number of dimensions, rather than just two.

R-Trees may also be useful if the objects being stored have a bounding rectangle, rather than being just a simple point.

The general term for these type of structures is Spatial Index.

There is a Java implementation of Quadtree and R-Tree.

The general term is a Spatial Index. I guess you should choose according to the existing implementations.

A quadtree is the structure which is usually used for that.

Have a look at Kd-Trees.

Simple QuadTree implementation in C# (easy to translate into java) http://www.codeproject.com/KB/recipes/QuadTree.aspx

You could put all the x cords in a map, and the y cords in another map, and have the map values point to the object.

        TreeMap<Integer, TreeMap<Integer, Point>> xMap = new TreeMap<Integer, TreeMap<Integer, Point>>();
        for (int x = 1; x < 100; x += 2)
            for (int y = 0; y < 100; y += 2)
                {
                    Point p = new Point(x, y);
                    TreeMap<Integer, Point> tempx = xMap.get(x);
                    if (tempx == null)
                        {
                            tempx = new TreeMap<Integer, Point>();
                            xMap.put(x, tempx);
                        }
                    tempx.put(y, p);
                }
        SortedMap<Integer, TreeMap<Integer, Point>> tempq = xMap.subMap(5, 8);
        Collection<Point> result = new HashSet<Point>();
        for (TreeMap<Integer, Point> smaller : tempq.values())
            {
                SortedMap<Integer, Point> smallerYet = smaller.subMap(6, 12);
                result.addAll(smallerYet.values());
            }
        for (Point q : result)
            {
                System.out.println(q);
            }
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!