Exemplo n.º 1
0
 public MazeWindow(DrawMazeParameters dmp)
 {
     InitializeComponent();
     PrintCommand.InputGestures.Add(new KeyGesture(Key.P, ModifierKeys.Control));
     RefreshCommand.InputGestures.Add(new KeyGesture(Key.F5));
     CopyCommand.InputGestures.Add(new KeyGesture(Key.C, ModifierKeys.Control));
     _dmp = dmp;
     DrawMaze(_dmp);
 }
Exemplo n.º 2
0
        private void DrawMaze(DrawMazeParameters dmp)
        {
            SetUpCanvas(dmp.CanvasSize.HorizontalPixels, dmp.CanvasSize.VerticalPixels);
            Maze maze = dmp.MazeAlgorithm.Create(dmp.MazeSize.Rows, dmp.MazeSize.Cols);

            if (dmp.Braid)
            {
                maze.Braid(dmp.BraidProbability);
            }
            Distances d = null;

            if (dmp.ColourCells)
            {
                d = maze[dmp.PathStartRow, dmp.PathStartCol].Distances();
            }
            double hCellSize = MazeCanvas.Width / maze.Cols;
            double vCellSize = MazeCanvas.Height / maze.Rows;

            // (0, 0) is top-left
            for (int row = 0; row < maze.Rows; row++)
            {
                // (hOffset, vOffset) is the top-left of the current cell
                double vOffset = row * vCellSize;
                for (int col = 0; col < maze.Cols; col++)
                {
                    double hOffset  = col * hCellSize;
                    Cell   thisCell = maze[row, col];
                    if (dmp.ColourCells)
                    {
                        if (dmp.ColourCells)
                        {
                            ColourCell(d.Max.Distance, d, thisCell, hCellSize, vCellSize);
                        }
                    }
                    if (dmp.DrawWalls)
                    {
                        if (!thisCell.Linked(thisCell.South))
                        {
                            DrawLine(hOffset, vOffset + vCellSize, hOffset + hCellSize, vOffset + vCellSize);
                        }
                        if (!thisCell.Linked(thisCell.East))
                        {
                            DrawLine(hOffset + hCellSize, vOffset, hOffset + hCellSize, vOffset + vCellSize);
                        }
                    }
                }
                if (dmp.DrawWalls)
                {
                    DrawLine(0, 0, MazeCanvas.Width, 0);
                    DrawLine(0, 0, 0, MazeCanvas.Height);
                }
            }
            if (dmp.DrawDistances)
            {
                DrawDistances(d, hCellSize, vCellSize);
            }
            if (dmp.DrawLocations)
            {
                DrawLocations(maze, hCellSize, vCellSize);
            }
            if (dmp.DrawLongest)
            {
                DrawPath(maze.LongestPath, hCellSize, vCellSize);
            }
            if (dmp.DrawStartStop)
            {
                // TODO AYS - Draw start and stop icons at the two ends of the longest path
                double      iconSize    = Math.Min(hCellSize - 4, vCellSize - 4);
                List <Cell> longestPath = maze.LongestPath;
                Cell        start       = longestPath.First();
                double      hOffset     = start.Col * hCellSize + (hCellSize - iconSize) / 2;
                double      vOffset     = start.Row * vCellSize - (vCellSize - iconSize) / 2 + 4;
                Image       icon        = new Image {
                    Width  = iconSize,
                    Height = iconSize,
                    Margin = new Thickness(hOffset, vOffset, iconSize, iconSize),
                    Source = new BitmapImage(new Uri("pack://application:,,,/Mazes.UI;component/Images/startbw.png"))
                };
                MazeCanvas.Children.Add(icon);
                Cell stop = longestPath.Last();
                hOffset = stop.Col * hCellSize + (hCellSize - iconSize) / 2;
                vOffset = stop.Row * vCellSize - (vCellSize - iconSize) / 2 + 4;
                icon    = new Image {
                    Width  = iconSize,
                    Height = iconSize,
                    Margin = new Thickness(hOffset, vOffset, iconSize, iconSize),
                    Source = new BitmapImage(new Uri("pack://application:,,,/Mazes.UI;component/Images/stop.png"))
                };
                MazeCanvas.Children.Add(icon);
            }
        }