private static GsRectangle ComputeBounds(IEnumerable <GridCell> cells, float cellWidth, float cellHeight) { // create a copy of the cells List <GridCell> lst = new List <GridCell>(cells); // sort the list by Y, then X. This will make the cells go left to right and then top to bottom. lst.Sort(); // get the first and last locations GridCell tl = lst[0]; GridCell br = lst[lst.Count - 1]; // finally, return the box return(GsRectangle.FromLTRB(tl.X, tl.Y, br.X + cellWidth, br.Y + cellHeight)); }
private void InitializeGrid() { int midCol = (NumCols - 1) / 2; int midRow = (NumRows - 1) / 2; int midColLeft = midCol - HalfThroughWay; int midColRight = midCol + HalfThroughWay; int midRowUp = midRow - HalfThroughWay; int midRowDown = midRow + HalfThroughWay; int dc = (NumCols - 1) % 2; int dr = (NumRows - 1) % 2; Grid = new GridCell[NumCols, NumRows]; for (int c = 0; c < NumCols; ++c) { for (int r = 0; r < NumRows; ++r) { bool isOuter = (c == 0 || r == 0 || c == (NumCols - 1) || r == (NumRows - 1)); bool isThroughway = isOuter && (GsMath.InRange(c, midColLeft + dc, midColRight) || GsMath.InRange(r, midRowUp + dr, midRowDown)); GridCell cell = new GridCell(c, r, isOuter, isThroughway); cell.Bounds = new GsRectangle(c * CellWidth, r * CellHeight, CellWidth, CellHeight); Grid[c, r] = cell; } } HorzGoalCell = Grid[NumCols - 1, midRow]; VertGoalCell = Grid[midCol, NumRows - 1]; HorzStartCell = Grid[0, midRow]; VertStartCell = Grid[midCol, 0]; gridBounds = GsRectangle.FromLTRB(Grid[0, 0].X, Grid[0, 0].Y, Grid[NumCols - 1, 0].X + CellWidth, Grid[0, NumRows - 1].Y + CellHeight); gridBounds.Offset(MiddleOffset + Position); }