-using System;\r
+\r
+// Uncomment this to disable diagonal movemet.\r
+//#define ALLOW_DIAGONAL_MOVEMENT\r
+\r
+using System;\r
using System.Collections.Generic;\r
using System.Linq;\r
using System.Text;\r
/// <param name="start">The cell to start at.</param>\r
/// <param name="finish">The desired destination.</param>\r
/// <returns>A list of points representing the path through the grid,\r
- /// ends points not included, or null if no path could be found.</return>\r
+ /// starting point not included, or null if no path could be found.</return>\r
public List<Point> GetPath(Point start, Point finish)\r
{\r
return GetPath(start, finish, GetManhattanDistance, GetCost);\r
/// <param name="finish">The desired destination.</param>\r
/// <param name="heuristic">The heuristic function.</param>\r
/// <returns>A list of points representing the path through the grid,\r
- /// ends points not included, or null if no path could be found.</return>\r
+ /// starting point not included, or null if no path could be found.</return>\r
public List<Point> GetPath(Point start, Point finish, Heuristic heuristic)\r
{\r
return GetPath(start, finish, heuristic, GetCost);\r
/// <param name="finish">The desired destination.</param>\r
/// <param name="costFunction">The cost function</param>\r
/// <returns>A list of points representing the path through the grid,\r
- /// ends points not included, or null if no path could be found.</return>\r
+ /// starting point not included, or null if no path could be found.</return>\r
public List<Point> GetPath(Point start, Point finish, CostFunction costFunction)\r
{\r
return GetPath(start, finish, GetManhattanDistance, costFunction);\r
/// <param name="heuristic">The heuristic function.</param>\r
/// <param name="costFunction">The cost function.</param>\r
/// <returns>A list of points representing the path through the grid,\r
- /// ends points not included, or null if no path could be found.</return>\r
+ /// starting point not included, or null if no path could be found.</return>\r
public List<Point> GetPath(Point start, Point finish, Heuristic heuristic, CostFunction costFunction)\r
{\r
mFringe = new BinaryHeap<Cell>();\r
mCells = new Cell[mGridWidth, mGridHeight];\r
\r
- Cell startCell = new Cell(start, 0, GetManhattanDistance(start, finish));\r
+ Cell startCell = new Cell(start, 0, heuristic(start, finish));\r
mFringe.Add(startCell);\r
mCells[start.X, start.Y] = startCell;\r
while (mFringe.Count > 0)\r
if (cell.Point == finish)\r
{\r
List<Point> list = new List<Point>();\r
+ list.Add(cell.Point);\r
\r
cell = cell.Parent;\r
- while (cell.Point != start)\r
- {\r
- list.Add(cell.Point);\r
- cell = cell.Parent;\r
- }\r
+ if (cell != null) for (; cell.Point != start; cell = cell.Parent) list.Add(cell.Point);\r
\r
list.Reverse();\r
return list;\r
}\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
+#if ALLOW_DIAGONAL_MOVEMENT\r
neighbors.Add(new Point(cell.Point.X - 1, cell.Point.Y - 1));\r
- neighbors.Add(new Point(cell.Point.X + 0, 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 + 0));\r
- neighbors.Add(new Point(cell.Point.X + 1, cell.Point.Y + 0));\r
neighbors.Add(new Point(cell.Point.X - 1, cell.Point.Y + 1));\r
- neighbors.Add(new Point(cell.Point.X + 0, cell.Point.Y + 1));\r
neighbors.Add(new Point(cell.Point.X + 1, cell.Point.Y + 1));\r
+#endif\r
foreach (Point point in neighbors)\r
{\r
- Cell inQueue = mCells[point.X, point.Y];\r
-\r
if (0 <= point.X && point.X < mGridWidth && 0 <= point.Y && point.Y < mGridHeight &&\r
mGrid[point.X, point.Y])\r
{\r
- int cost = cell.G + GetCost(cell.Point, point);\r
+ int cost = cell.G + costFunction(cell.Point, point);\r
\r
+ Cell inQueue = mCells[point.X, point.Y];\r
if (inQueue == null)\r
{\r
- Cell neighbor = new Cell(point, cost, GetManhattanDistance(point, finish), cell);\r
+ Cell neighbor = new Cell(point, cost, heuristic(point, finish), cell);\r
mFringe.Add(neighbor);\r
mCells[point.X, point.Y] = neighbor;\r
}\r