Пример #1
0
        /// <summary>
        /// 加网格
        /// </summary>
        private void AddGrid()
        {
            grid = new Grid()
            {
                ShowGridLines = IsShowGrid.IsChecked.Value,
                Width         = width,
                Height        = height,
            };

            int cols = (int)(grid.Width) / gridwidth + 1;
            int rows = (int)(grid.Height) / gridheight + 1;

            for (int y = 0; y < cols; y++)
            {
                ColumnDefinition col = new ColumnDefinition()
                {
                    Width = new GridLength(gridwidth)
                };
                grid.ColumnDefinitions.Add(col);
            }
            for (int x = 0; x < rows; x++)
            {
                RowDefinition row = new RowDefinition()
                {
                    Height = new GridLength(gridheight)
                };
                grid.RowDefinitions.Add(row);
            }
            ObstructionViewer.Content = grid;

            //初始化地图数据
            //看数据是否为空,来渲染障碍物
            if (Matrix != null)
            {
                for (int x = 0; x <= Matrix.GetUpperBound(1); x++)
                {
                    for (int y = 0; y <= Matrix.GetUpperBound(0); y++)
                    {
                        if (Matrix[y, x] == obstruct)
                        {
                            //为障碍物
                            grid.UnregisterName("rect" + x + "_" + y);
                        }
                    }
                }
            }
            if (startPos != null)
            {
                grid.UnregisterName("startPos");
            }
            if (endPos != null)
            {
                grid.UnregisterName("endPos");
            }
            if (paths != null)
            {
                foreach (var item in paths)
                {
                    grid.UnregisterName("path" + item.x + "_" + item.y);
                }
            }

            startPos = null;
            endPos   = null;
            paths    = null;
            //初始化障碍物数据
            Matrix = new byte[rows, cols];
            for (int y = 0; y < rows; y++)
            {
                for (int x = 0; x < cols; x++)
                {
                    Matrix[y, x] = 1;
                }
            }
        }
Пример #2
0
        /// <summary>
        /// 设置网格颜色
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="value"></param>
        private void SetMatrixGrid(int x, int y, byte value)
        {
            if (x < 0 || y < 0 || y > Matrix.GetUpperBound(0) || x > Matrix.GetUpperBound(1))
            {
                return;
            }
            //先获取原先这个格子的数据
            int old_value = Matrix[y, x];

            if (value == 0)
            {
                //设置障碍物
                if (old_value == 0)
                {
                    Matrix[y, x] = 1;
                    //先清除障碍物
                    Rectangle temp_rect = grid.FindName("rect" + x + "_" + y) as Rectangle;
                    grid.UnregisterName("rect" + x + "_" + y);
                    grid.Children.Remove(temp_rect);
                }
                else
                {
                    Matrix[y, x] = 0;
                    Rectangle rect = new Rectangle()
                    {
                        Width  = gridwidth,
                        Height = gridheight,
                        Fill   = new SolidColorBrush(Colors.Gray)
                    };
                    grid.Children.Add(rect);
                    grid.RegisterName("rect" + x + "_" + y, rect);
                    rect.SetValue(Grid.ColumnProperty, x);
                    rect.SetValue(Grid.RowProperty, y);
                }
            }
            else if (value == 2)
            {
                //设置起点
                if (startPos != null)
                {
                    //先清除起点
                    Rectangle temp_rect = grid.FindName("startPos") as Rectangle;
                    grid.UnregisterName("startPos");
                    grid.Children.Remove(temp_rect);
                }
                Rectangle rect = new Rectangle()
                {
                    Width  = gridwidth,
                    Height = gridheight,
                    Fill   = new SolidColorBrush(Colors.Red)
                };
                grid.Children.Add(rect);
                grid.RegisterName("startPos", rect);
                rect.SetValue(Grid.ColumnProperty, x);
                rect.SetValue(Grid.RowProperty, y);
                startPos = new Pos(x, y);
                SearchPath();
            }
            else if (value == 3 && old_value != 3)
            {
                //设置终点
                if (endPos != null)
                {
                    //先清除终点
                    Rectangle temp_rect = grid.FindName("endPos") as Rectangle;
                    grid.UnregisterName("endPos");
                    grid.Children.Remove(temp_rect);
                }
                Rectangle rect = new Rectangle()
                {
                    Width  = gridwidth,
                    Height = gridheight,
                    Fill   = new SolidColorBrush(Colors.Blue)
                };
                grid.Children.Add(rect);
                grid.RegisterName("endPos", rect);
                rect.SetValue(Grid.ColumnProperty, x);
                rect.SetValue(Grid.RowProperty, y);
                endPos = new Pos(x, y);
                SearchPath();
            }
            else if (value == 4)
            {
                //画路径
                Rectangle rect = new Rectangle()
                {
                    Width  = gridwidth,
                    Height = gridheight,
                    Fill   = new SolidColorBrush(Colors.Green)
                };
                grid.Children.Add(rect);
                grid.RegisterName("path" + x + "_" + y, rect);
                rect.SetValue(Grid.ColumnProperty, x);
                rect.SetValue(Grid.RowProperty, y);
                paths.Add(new Pos(x, y));
            }
        }