简体   繁体   English

在真正的大矩形内保存一组坐标的最佳数据结构?

[英]Best data structure to hold a group of coordinates inside a really big rectangle?

I have an abstract representation of a map with, lets say, 5.000.000 different integer coordinates on the X and Y coordinates, so it is a really big 2d rectangle. 我有一个地图的抽象表示,可以说在X和Y坐标上有5.000.000个不同的整数坐标,所以它是一个非常大的2d矩形。

And then, inside that variable-sized rectangle, I have several objects (characters, monsters, npcs). 然后,在该可变大小的矩形内,我有几个对象(字符,怪物,npc)。 A player can select a position of this rectangle, and I have to check if there is a monster or a character on said position. 玩家可以选择该矩形的位置,我必须检查该位置上是否有怪物或角色。

So far, I made a custom class called GameMatrix with columns and rows, and said columns has 3000x2000 positions (the area view of a character). 到目前为止,我创建了一个名为GameMatrix的自定义类,该类具有列和行,并且所说的列具有3000x2000位置(角色的区域视图)。

When a player of my game clicks on said coordinate, I have to do a foreach() of every element inside the matrix, and most of the times it is empty. 当我的游戏玩家点击所述坐标时,我必须对矩阵内的每个元素进行一次foreach() ,并且大多数情况下它为空。

Is there a better way to solve this? 有没有更好的方法来解决这个问题? Specifically, I am asking on what is the best way to, having a really big rectangle and a coordinate, check if there are objects inside said coordinate in an efficient way. 具体来说,我在问什么是最好的方法,即拥有一个非常大的矩形和一个坐标,以一种有效的方式检查在所述坐标内是否有对象。

Forgot to mention, but this is done in the server-side several times per miliseconds. 忘了提一下,但这是在服务器端每毫秒完成几次的。 So I need a lot of performance. 所以我需要很多表现。

Edit: Forgot to mention, I am using C#. 编辑:忘了提,我正在使用C#。

You should use a quad-tree implementation, unless the number of items in the grid is really small (like a couple of dozens items), in which case a linear search (brute force inspecting all of them at the same time) is probably the best choice. 您应该使用四叉树实现,除非网格中的项目数确实很少(例如几十个项目),在这种情况下,线性搜索(同时用力检查所有项目)可能是最好的选择。

See here 看这里

http://en.wikipedia.org/wiki/Quadtree http://en.wikipedia.org/wiki/Quadtree

Note that a quad tree can be queried very quickly and efficiently, but updates are a bit more costly, that is if your items move a lot across your map, this becomes more complicated to do with a good performance. 请注意,可以快速高效地查询四叉树,但更新的成本更高,也就是说,如果您的项目在地图上移动很多,则要获得良好的性能就变得更加复杂。

I would recommend just trying the simplest solution first and worry about the optimization later. 我建议先尝试最简单的解决方案,然后再担心优化。

Try using an array. 尝试使用数组。 Improve your solution as you go. 随时随地改善您的解决方案。 A list or hash map might be interesting too depending on your setup. 列表或哈希图可能也很有趣,具体取决于您的设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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