0

有没有一种有效的方法来查看 N 个 (x,y) 点是否在 K 个矩形内?现在我正在做一个蛮力方法并遍历所有点和矩形,但它需要大约 2 分 30 秒,有 200,000 个点和 44 个矩形。

我正在使用谷歌地图并创建一个程序来检查点是否靠近地图上的路线。我沿路径计算多个矩形和圆形并测试现有点是否位于这些矩形和圆形内。

1.矩形可以重叠,具体取决于路线的性质。
2.该点只能在一个矩形中
3.如果该点位于矩形的边缘,我想让它算在矩形内(但如果它更容易不计算,那么我不会计算它)
4.矩形取决于我想在路线外搜索的区域。通常,它们将高 2 英里(从点的每个方向 1 英里)和从点 1 到点 2 的距离宽。

4

2 回答 2

1

In theory at the very best you'll have to iterate through all 200,000 points -- and in the worst case you'll have to check all of those points against all 44 rectangles (which is what you're doing right now).

Since you know you'll have to loop through all 200,000 points the best you can do is attempt to not have to check all 44 rectangles.

In order to do this you'll have to do some calculations on the rectangles, you find the closest two rectangles and form a larger rectangle which encloses both of them (a cluster if you will). Then you find the next closest rectangle to the rectangle you just formed form another cluster rectangle. You keep doing this until you enclose all of the rectangles (You'll end up having 43 cluster rectangles).

Now loop through the points and check against the largest rectangle, if the point falls within that rectangle, then you check the next largest rectangle, if it doesn't fall within that rectangle then you only need to check the to see if it falls within the rectangle which was used to form that rectangle. If it doesn't fall within that rectangle, then you can move onto the next point because that point doesn't fall within any rectangles (and you discovered this with only 3 checks).

于 2011-06-10T15:09:43.730 回答
1

以下是一些可能的想法:

  • 模糊匹配 - 如果您的点不必 100% 准确地标记为在特定矩形内,您可以编写一个算法,使“最佳猜测”更有效,但牺牲 100% 准确
  • 先模糊,后准确 - 快速给出近似答案,可能只需计算给定点与矩形左上角或圆心之间的距离。这将给出一个可能不是 100% 准确的近似答案,但随后允许您异步继续计算、对其进行细化,并在一段时间后使用 100% 准确的数据更新显示
  • 对点进行分组 - 当它创建的点时,让它用一个矩形“注册”自己,如果可以的话,基本上预先计算一个点是否在给定的矩形中。
  • 预先计算+缓存 - 然后在点本身中缓存特定点适合的矩形列表。然后它就变成了一个简单的查找,而不必每次都重新计算
  • 异步加载 - 您可以在计算答案时开始显示答案吗?如果整个批次需要 2.5 分钟,您能否在计算时以 1,000 点块显示结果?通过这种方式,用户在计算完成工作时快速开始获得一些反馈。2.5 分钟,即 150 秒。如果您可以以 1,000 点块的形式提供结果(一次大约是数据的 1/200),那么您可以在结果可用时每秒更新一次点图。
于 2011-06-10T14:37:55.713 回答