Rectangle rect;
Private Ipathfinder Pathfinder = Null ;
Private Byte [,] Matrix = New Byte [ 1024 , 1024 ]; // Two-dimensional routing Matrix
Private Int Gridsize = 20 ; // Unit grid size
Point start = New Point ( 0 , 0 ); // Move the Starting Point Coordinate
Point end; // Move the terminal Coordinate
Construct the initialization Matrix
Privatevoid initializematrix ()
{
For ( Int Y = 0 ; Y <matrix. getupperbound ( 1 ); Y ++)
{
For ( Int X = 0 ; X <matrix. getupperbound ( 0 ); X ++)
{
// The default value can be expressed by 1 in the matrix (non-obstacle ).
Matrix [x, y] = 1 ;
}
}
// Build obstacles (example)
For ( Int I = 0 ; I < 18 ; I ++)
{
// The obstacle is represented by 0 in the matrix.
Matrix [I, 12 ] = 0 ;
Rect = New Rectangle ();
Rect. Fill = New Solidcolorbrush (colors. Red );
Rect. width = gridsize;
Rect. Height = gridsize;
Carrier. Children. Add (rect );
Canvas. setleft (rect, I * gridsize );
Canvas. settop (rect, 12 * Gridsize );
}
For ( Int I = 12 ; I < 20 ; I ++)
{
// The obstacle is represented by 0 in the matrix.
Matrix [ 18 , I] = 0 ;
Rect = New Rectangle ();
Rect. Fill = New Solidcolorbrush (colors. Red );
Rect. width = gridsize;
Rect. Height = gridsize;
Carrier. Children. Add (rect );
Canvas. setleft (rect, 18 * Gridsize );
Canvas. settop (rect, I * gridsize );
}
For ( Int I = 12 ; I < 19 ; I ++)
{
// The obstacle is represented by 0 in the matrix.
Matrix [I, 20 ] = 0 ;
Rect = New Rectangle ();
Rect. Fill = New Solidcolorbrush (colors. Red );
Rect. width = gridsize;
Rect. Height = gridsize;
Carrier. Children. Add (rect );
Canvas. setleft (rect, I * gridsize );
Canvas. settop (rect, 20 * Gridsize );
}
}
Path finding method call: we usually use the left-click mouse to determine the mouse position.
Private Void Carrier_mouseleftbuttondown ( Object Sender, mousebuttoneventargs E)
{
Point P = E. getposition (carrier );
Int X = ( Int ) P. x/gridsize;
Int Y = ( Int ) P. Y/gridsize;
End =New Point (x, y ); // Calculate the end Coordinate
Pathfinder = New Pathfinderfast (matrix );
Pathfinder. heavydiagonals = True ; // Deep diagonal Movement
// Pathfinder. diagonals = true; // Diagonal Line
Pathfinder. formula = heuristicformula. Manhattan; // Use what I personally think is the fastest Manhattan *Algorithm
Pathfinder. searchlimit = 2000 ; // The path range, that is, the scope of the path to be searched.
List <pathfindernode> Path = Pathfinder. findpath (START, end ); // Start path searching
If (Path = Null )
{
MessageBox. Show ( " Path does not exist! " );
}
Else
{
String Output = String . Empty;
For (Int I = path. Count- 1 ; I> = 0 ; I --)
{
Output = String . Format (output
+ " {0} "
+ Path [I]. X. tostring ()
+ " {1} "
+ Path [I]. Y. tostring ()
+" {2} " ,
" ( " , " , " , " ) " );
Rect = New Rectangle ();
Rect. Fill = New Solidcolorbrush (colors. Green );
Rect. width = gridsize;
Rect. Height = gridsize;
Carrier. Children. Add (rect );
Canvas. setleft (rect, path [I]. x * gridsize );
Canvas. settop (rect, path [I]. y * gridsize );
}
MessageBox. Show ( " The path coordinates are: " + Output );
}
}
Of course, we need to call the Most Important Path-finding DLL here. This is the essence of path-finding, and the source code/files/caceolevels/silvergame.zip is attached.