Пример #1
0
        private void MainForm_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                /// Source cell selection.
                this.sourceCell = this.grid[e.X / CELL_SIZE, e.Y / CELL_SIZE];
            }
            else if (e.Button == MouseButtons.Right)
            {
                /// Target cell selection.
                if (this.sourceCell == null)
                {
                    return;
                }
                this.targetCell = this.grid[e.X / CELL_SIZE, e.Y / CELL_SIZE];

                /// Execute the high-level pathfinding.
                PathfindingAlgorithm <MC.Region> pathfinding =
                    new PathfindingAlgorithm <MC.Region>(this.sourceCell.GetRegion(OBJECT_SIZE), new GridTopologyGraph(this.targetCell, OBJECT_SIZE));
                this.lastHighLevelResult = pathfinding.Run();
                this.currentRegionIndex  = 0;
                this.currentStartCell    = this.sourceCell;

                /// Draw the result.
                this.resultImageGC.Clear(Color.FromArgb(0, Color.White));
                foreach (MC.Region currentRegion in this.lastHighLevelResult.ExploredNodes)
                {
                    this.DrawRegion(currentRegion, Color.LightGray);
                }
                foreach (MC.Region currentRegion in this.lastHighLevelResult.Path)
                {
                    this.DrawRegion(currentRegion, Color.Gray);
                }

                Console.WriteLine("Search time: {0} ms", this.lastHighLevelResult.ElapsedTime);
                Console.WriteLine("Explored nodes: {0}", this.lastHighLevelResult.ExploredNodes.Count);

                this.Invalidate();
            }
            else if (e.Button == MouseButtons.Middle)
            {
                if (this.currentRegionIndex == this.lastHighLevelResult.Path.Count)
                {
                    return;
                }

                /// Execute the low-level pathfinding for the current region index.
                IGraph <MC.Cell> graph = null;
                if (this.currentRegionIndex < this.lastHighLevelResult.Path.Count - 1)
                {
                    graph = new TransitRegionGraph(this.lastHighLevelResult.Path[this.currentRegionIndex], this.lastHighLevelResult.Path[this.currentRegionIndex + 1]);
                }
                else
                {
                    graph = new TargetRegionGraph(this.lastHighLevelResult.Path[this.currentRegionIndex], this.targetCell);
                }
                PathfindingAlgorithm <MC.Cell> pathfinding =
                    new PathfindingAlgorithm <MC.Cell>(this.currentStartCell, graph);
                this.lastLowLevelResult = pathfinding.Run();
                this.currentRegionIndex++;
                this.currentStartCell = this.lastLowLevelResult.Path.Last();

                /// Draw the result.
                foreach (MC.Cell currentCell in this.lastLowLevelResult.ExploredNodes)
                {
                    this.resultImage.SetPixel(currentCell.Coords.X, currentCell.Coords.Y, Color.Red);
                }
                foreach (MC.Cell currentCell in this.lastLowLevelResult.Path)
                {
                    this.resultImage.SetPixel(currentCell.Coords.X, currentCell.Coords.Y, Color.Yellow);
                }

                Console.WriteLine("Search time: {0} ms", this.lastLowLevelResult.ElapsedTime);
                Console.WriteLine("Explored nodes: {0}", this.lastLowLevelResult.ExploredNodes.Count);

                this.Invalidate();
            }
        }