///<ummary>Starts the search for the shortest path at the position "start"</summary> public bool FindPath(out PointI[] path, PointI start) { Stopwatch stp = new Stopwatch(); if (printDebug) { stp.Start(); } List <PointI> p = new List <PointI>(); bool result = step(p, start); if (result) { path = p.ToArray(); } else { path = new PointI[0]; } if (printDebug) { stp.Stop(); GD.Print("Found path in ", stp.ElapsedMilliseconds, "ms"); } return(result); }
public override bool Equals(object obj) { if (!(obj is PointI)) { return(false); } PointI point = (PointI)obj; return(point.X == this.X && point.Y == this.Y); }
void loop() { try { PointI[] tmp = new PointI[endPoints.Length]; endPoints.CopyTo(tmp, 0); this.pathFinder.Reset(gridCopy); foreach (PointI end in tmp) { this.pathFinder.CalcHops(end); } Pathversion++; } catch (System.Threading.ThreadAbortException) { } }
///<summary>Makes one step of the Path</summary> bool step(List <PointI> path, PointI point) { // Check if point is outside Grid if (point.X < 0 || point.X >= w || point.Y < 0 || point.Y >= h) { return(false); } // If point is at an endpoint, return true if (this.hops[point.X, point.Y] == 0) { path.Add(point); return(true); } // Search for the Direction with the smallest amount of hops float min_hops = this.hops[point.X, point.Y]; int min_pos = -1; for (int i = 0; i < directionsDiag.Length; i++) { // Add each direction to the current position PointI next = point + directionsDiag[i]; // Check if new position is outside the Grid if (next.X < 0 || next.X >= w || next.Y < 0 || next.Y >= h) { continue; } if (this.hops[next.X, next.Y] < min_hops // Check if path would strip a corner && this.hops[next.X, point.Y] < maxHops + 1 && this.hops[point.X, next.Y] < maxHops + 1) { min_hops = this.hops[next.X, next.Y]; min_pos = i; } } // If there is no smaller path, there is no possible path.false if (min_pos < 0) { return(false); } path.Add(point); return(step(path, point + directionsDiag[min_pos])); }
///<summary>Starts the calculation of the hopcount at the position "end"</summary> public void CalcHops(PointI end) { Stopwatch stp = new Stopwatch(); if (printDebug) { stp.Start(); } setHops(end.X, end.Y, 0); if (printDebug) { stp.Stop(); GD.Print("Calculated hops in ", stp.ElapsedMilliseconds, "ms"); } if (printField) { GD.Print("Field:"); for (int y = 0; y < h; y++) { StringBuilder line = new StringBuilder(); line.Append("|"); for (int x = 0; x < w; x++) { if (grid.FieldBlocked(x, y)) { line.AppendFormat("###|"); } else { line.AppendFormat("{0,3}|", this.hops[x, y]); } } GD.Print(line.ToString()); } } }
public bool FindPath(out PointI[] path, PointI start) { return(pathFinder.FindPath(out path, start)); }