我有一个ICollection<MapNode>
。每个MapNode
有一个Position
属性,这是一个Point
。我想先按Y值排序这些点,然后按X值排序这些点,然后将它们放入多维数组(MapNode[,]
)。c#:干净的方式来适应集合到多维数组?
收集会是这个样子:
(30, 20)
(20, 20)
(20, 30)
(30, 10)
(30, 30)
(20, 10)
和最终产品:
(20, 10) (20, 20) (20, 30)
(30, 10) (30, 20) (30, 30)
下面是代码,我拿出来做到这一点。这是不可读的吗?我觉得它比想要的更黑。
private Map createWorldPathNodes()
{
ICollection<MapNode> points = new HashSet<MapNode>();
Rectangle worldBounds = WorldQueryUtils.WorldBounds();
for (float x = worldBounds.Left; x < worldBounds.Right; x += PATH_NODE_CHUNK_SIZE)
{
for (float y = worldBounds.Y; y > worldBounds.Height; y -= PATH_NODE_CHUNK_SIZE)
{
// default is that everywhere is navigable;
// a different function is responsible for determining the real value
points.Add(new MapNode(true, new Point((int)x, (int)y)));
}
}
int distinctXValues = points.Select(node => node.Position.X).Distinct().Count();
int distinctYValues = points.Select(node => node.Position.Y).Distinct().Count();
IList<MapNode[]> mapNodeRowsToAdd = new List<MapNode[]>();
while (points.Count > 0) // every iteration will take a row out of points
{
// get all the nodes with the greatest Y value currently in the collection
int currentMaxY = points.Select(node => node.Position.Y).Max();
ICollection<MapNode> ythRow = points.Where(node => node.Position.Y == currentMaxY).ToList();
// remove these nodes from the pool we're picking from
points = points.Where(node => ! ythRow.Contains(node)).ToList(); // ToList() is just so it is still a collection
// put the nodes with max y value in the array, sorting by X value
mapNodeRowsToAdd.Add(ythRow.OrderByDescending(node => node.Position.X).ToArray());
}
MapNode[,] mapNodes = new MapNode[distinctXValues, distinctYValues];
int xValuesAdded = 0;
int yValuesAdded = 0;
foreach (MapNode[] mapNodeRow in mapNodeRowsToAdd)
{
xValuesAdded = 0;
foreach (MapNode node in mapNodeRow)
{
// [y, x] may seem backwards, but mapNodes[y] == the yth row
mapNodes[yValuesAdded, xValuesAdded] = node;
xValuesAdded++;
}
yValuesAdded++;
}
return pathNodes;
}
上述功能似乎工作得很好,但它还没有经过防弹测试。
如果你已经写好了,我可以读VB一点。 – 2010-03-26 15:21:14