存储用于通过x,y坐标定位的对象 [英] Storing objects for locating by x,y coordinates

查看:144
本文介绍了存储用于通过x,y坐标定位的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试确定一种存储一组对象的快速方法,每个对象都有一个x和y坐标值,这样我就可以快速检索某个矩形或圆形内的所有对象。
对于小型对象集(~100),简单地将它们存储在列表中并迭代它的简单方法相对较快。但是,对于规模更大的群体来说,这预计会很慢。
我已经尝试将它们存储在一对TreeMaps中,一个在x坐标上排序,一个在y坐标上排序,使用以下代码:

I'm trying to determine a fast way of storing a set of objects, each of which have an x and y coordinate value, such that I can quickly retrieve all objects within a certain rectangle or circle. For small sets of objects (~100) the naive approach of simply storing them in a list, and iterating through it, is relatively quick. However, for much larger groups, that is expectedly slow. I've tried storing them in a pair of TreeMaps as well, one sorted on the x coordinate, and one sorted on the y coordinate, using this code:

xSubset = objectsByX.subSet( minX, maxX );
ySubset = objectsByY.subSet( minY, maxY );
result.addAll( xSubset );
result.retainAll( ySubset );

这也适用,对于较大的对象集更快,但仍然比我想要的慢。
部分问题还在于这些对象四处移动,需要插回到此存储中,这意味着将它们从树中删除并重新添加到树/列表中。
我不禁想到必须有更好的解决方案。
我在Java中实现这个,如果它有任何区别,虽然我希望任何解决方案都会以有用的模式/算法的形式出现。

This also works, and is faster for larger sets of objects, but is still slower than I would like. Part of the problem is also that these objects move around, and need to be inserted back into this storage, which means removing them from and re-adding them to the trees/lists. I can't help but think there must be better solutions out there. I'm implementing this in Java, if it makes any difference, though I expect any solution will be more in the form of a useful pattern/algorithm.

推荐答案

四叉树似乎解决了我提出的具体问题。 Kd-Trees 是一种更通用的形式,适用于任意数量的维度,而不仅仅是两个维度。

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 可能如果存储的对象有一个边界矩形,而不仅仅是一个简单的点,也会很有用。

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.

有一个Java实现四叉树 R-Tree

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

这篇关于存储用于通过x,y坐标定位的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆