C# 从大量类列表中选择大量类的最有效方法是什么?
基本上,我正在尝试在XNA2D中创建一个tile引擎,目前我正在使用一个大的tile列表,我的类中包含了一个tile的所有数据,并在我的视图范围内选择这些数据,然后将它们显示在屏幕上 我的问题是,很明显,我的瓷砖总列表越大,我在尝试挑选范围内的瓷砖时所经历的延迟就越大。我目前正在for循环中使用Linq来选择瓷砖,如下所示:C# 从大量类列表中选择大量类的最有效方法是什么?,c#,xna-4.0,C#,Xna 4.0,基本上,我正在尝试在XNA2D中创建一个tile引擎,目前我正在使用一个大的tile列表,我的类中包含了一个tile的所有数据,并在我的视图范围内选择这些数据,然后将它们显示在屏幕上 我的问题是,很明显,我的瓷砖总列表越大,我在尝试挑选范围内的瓷砖时所经历的延迟就越大。我目前正在for循环中使用Linq来选择瓷砖,如下所示: //loop from the maximum view at the top of the screen to the maximum view at the botto
//loop from the maximum view at the top of the screen to the maximum view at the bottom
for (int height = topView; height < bottomView; height++)
{
//loop from the maximum view to the left of the screen to the maximum view of the right
for (int width = leftView; width < rightView; width++)
{
//select the tile at this grid position
Tile t = tileList.Where(x => x.GridPosition == new Vector2(width, height)).FirstOrDefault();
if (t != null)
{
//calculate the screen position and add it to the list of local tiles within range
t.ScreenPosition = new Vector2(screenWidth * 30, screenHeight * 30);
localList.Add(t);
}
else
{
//the tile wasn't found, add a random blank one to fill the gap.
Tile brokenTile = new Tile(game, new Vector2(width, height), 9001);
brokenTile.ScreenPosition = new Vector2(screenWidth * 30, screenHeight * 30);
localList.Add(brokenTile);
}
//increment the screen width used to calculate the screen position
screenWidth++;
}
//increment the screen height used to calculate the screen position and reset the width
screenHeight++;
screenWidth = 1;
}
我想知道是否有一种方法可以更有效地做到这一点,理想情况下可以减少在增加“地图”的总体大小和选择范围内的瓷砖时所经历的延迟
我能想到的唯一一件事是,当地图加载时,用某种方法将整个列表分成“块”,并且只查看每个块以拉出分幅。。但是,我不太确定我该如何做,因为如果我需要从多个“块”中提取节点,可能会很麻烦
任何一个好方法的帮助都将是伟大的
非常感谢
编辑:这里有一些截图:与KD树相比,KD树是一个允许在k维空间中快速查找对象的集合 请参阅维基百科和 我将KD树的Java实现移植到C。请参见RoboWiki。你可以在那里下载代码 K-D树有一个Olog n搜索时间 更新: 您的屏幕截图显示瓷砖放置在矩形网格中。为什么不使用二维数组来存储您的磁贴呢
Tile[,] grid = new Tile[m,n];
选择平铺现在变得非常自然,因为您可以从屏幕位置计算它们在网格中的位置。这样,您就可以通过x-y索引直接访问它们,而无需在lagre列表中搜索它们。单个瓦片的存取时间是O1,即与瓦片总数无关。 < P>我会考虑使用某种形式的,例如A.
这意味着根据对象的位置将对象指定给规则大小的四边形。您将能够有效地确定哪些四边形是可见的,并且每个四边形都知道它包含哪些对象,尽管您需要在对象移动时对其进行管理 想想你的算法,它需要在^3上吗?你能缓存一些结果吗?我想这可能是唯一的方法,谢谢你的回复,我会研究它。感谢你的回复,KD树是一个有趣的概念,但似乎比2D需要的更复杂,尽管我可能错了,所以我会先尝试四叉树:如果瓷砖在平面中以任意方式放置,那么K-D树是可以的,因为它执行空间分区,但是如果您的分片是在网格中排序的,那么使用矩阵,即二维数组。