+ /// <summary>\r
+ /// Get a shortest path by putting the task on the thread pool.\r
+ /// </summary>\r
+ /// <param name="start">The cell to start at.</param>\r
+ /// <param name="finish">The desired destination.</param>\r
+ /// <returns>The async task object.</returns>\r
+ public AsyncTask GetPathAsync(Point start, Point finish)\r
+ {\r
+ AsyncTask task = new AsyncTask(this, start, finish, GetManhattanDistance, GetCost);\r
+ ThreadPool.QueueUserWorkItem(new WaitCallback(task.Run));\r
+ return task;\r
+ }\r
+\r
+\r
+ /// <summary>\r
+ /// Find the closest open cell that is near another cell.\r
+ /// </summary>\r
+ /// <param name="coordinates">The coordinates.</param>\r
+ /// <returns>An open cell at or near the given coordinates,\r
+ /// or null if no open nearby cell could be found.</returns>\r
+ public Point? GetNearbyOpenCell(Point coordinates)\r
+ {\r
+ if (0 <= coordinates.X && coordinates.X < mGridWidth && 0 <= coordinates.Y && coordinates.Y < mGridHeight &&\r
+ mGrid[coordinates.X, coordinates.Y])\r
+ {\r
+ return coordinates;\r
+ }\r
+\r
+ mFringe = new BinaryHeap<Cell>();\r
+ mCells = new Cell[mGridWidth, mGridHeight];\r
+\r
+ Cell startCell = new Cell(coordinates, 0, 0);\r
+ mFringe.Add(startCell);\r
+ mCells[coordinates.X, coordinates.Y] = startCell;\r
+ while (mFringe.Count > 0)\r
+ {\r
+ Cell cell = mFringe.GetNext();\r
+\r
+ List<Point> neighbors = new List<Point>(8);\r
+ neighbors.Add(new Point(cell.Point.X, cell.Point.Y - 1));\r
+ neighbors.Add(new Point(cell.Point.X - 1, cell.Point.Y));\r
+ neighbors.Add(new Point(cell.Point.X + 1, cell.Point.Y));\r
+ neighbors.Add(new Point(cell.Point.X, cell.Point.Y + 1));\r
+ neighbors.Add(new Point(cell.Point.X - 1, cell.Point.Y - 1));\r
+ neighbors.Add(new Point(cell.Point.X + 1, cell.Point.Y - 1));\r
+ neighbors.Add(new Point(cell.Point.X - 1, cell.Point.Y + 1));\r
+ neighbors.Add(new Point(cell.Point.X + 1, cell.Point.Y + 1));\r
+ foreach (Point point in neighbors)\r
+ {\r
+ if (0 <= point.X && point.X < mGridWidth && 0 <= point.Y && point.Y < mGridHeight)\r
+ {\r
+ if (mGrid[point.X, point.Y])\r
+ {\r
+ return point;\r
+ }\r
+ else\r
+ {\r
+ int cost = cell.G + GetCost(cell.Point, point);\r
+\r
+ Cell inQueue = mCells[point.X, point.Y];\r
+ if (inQueue == null)\r
+ {\r
+ Cell neighbor = new Cell(point, cost, 0);\r
+ mFringe.Add(neighbor);\r
+ mCells[point.X, point.Y] = neighbor;\r
+ }\r
+ }\r
+ }\r
+ }\r
+ }\r
+\r
+ return null;\r
+ }\r
+\r
+\r