コード例 #1
0
        public Maze_diplay_prim()
        {
            InitializeComponent();

            int start_col;

            height = Form1.rows;
            width  = Form1.cols;

            cal_matrix();
            Cell.rows = rows;
            Cell.cols = cols;
            grid      = new Cell[rows][];
            start_col = rnd.Next(0, cols);



            for (int i = 0; i < rows; i++)
            {
                grid[i] = new Cell[cols];
                for (int j = 0; j < cols; j++)
                {
                    grid[i][j] = new Cell(i, j);
                }
            }
            create_maze(start_col);

            Bitmap bit = new Bitmap(height, width);

            create_bitmap(ref bit);
            //REZOLVARE--------------------------------------------------------------------
            Depth_solve depth_solve = new Depth_solve(rows, cols, grid, nr_nodes, start_col);
            //Dijkstra_solve dij = new Dijkstra_solve(rows, cols, grid, nr_nodes, start_col);

            Stack <KeyValuePair <int, int> > road = depth_solve.get_road();

            int t = 255;
            int q = 0;
            int w = 255;
            int e = 0;

            double rate_of_change_colour = (double)255 / road.Count;
            double cont_rate_change      = rate_of_change_colour;

            KeyValuePair <int, int> temp1 = road.Pop();

            while (road.Count > 0)
            {
                KeyValuePair <int, int> temp = road.Pop();

                int x1 = temp.Key;
                int x2 = temp1.Key;
                int y1 = temp.Value;
                int y2 = temp1.Value;

                if (x1 == x2)
                {
                    for (int k = 0; k < Math.Abs(y1 - y2); k++)
                    {
                        bit.SetPixel(x1 * 3 + 1, (Math.Min(y1, y2) + k) * 3 + 1, Color.FromArgb(t, q, w, e));
                        bit.SetPixel(x1 * 3 + 1, (Math.Min(y1, y2) + k) * 3 + 2, Color.FromArgb(t, q, w, e));
                        bit.SetPixel(x1 * 3 + 2, (Math.Min(y1, y2) + k) * 3 + 1, Color.FromArgb(t, q, w, e));
                        bit.SetPixel(x1 * 3 + 2, (Math.Min(y1, y2) + k) * 3 + 2, Color.FromArgb(t, q, w, e));
                        bit.SetPixel(x1 * 3 + 1, (Math.Min(y1, y2) + k) * 3 + 3, Color.FromArgb(t, q, w, e));
                        bit.SetPixel(x1 * 3 + 2, (Math.Min(y1, y2) + k) * 3 + 3, Color.FromArgb(t, q, w, e));
                    }
                    bit.SetPixel(x1 * 3 + 1, Math.Max(y1, y2) * 3 + 1, Color.FromArgb(t, q, w, e));
                    bit.SetPixel(x1 * 3 + 2, Math.Max(y1, y2) * 3 + 2, Color.FromArgb(t, q, w, e));
                    bit.SetPixel(x1 * 3 + 1, Math.Max(y1, y2) * 3 + 2, Color.FromArgb(t, q, w, e));
                    bit.SetPixel(x1 * 3 + 2, Math.Max(y1, y2) * 3 + 1, Color.FromArgb(t, q, w, e));
                }
                else
                {
                    for (int k = 0; k < Math.Abs(x1 - x2); k++)
                    {
                        bit.SetPixel((Math.Min(x1, x2) + k) * 3 + 1, y2 * 3 + 1, Color.FromArgb(t, q, w, e));
                        bit.SetPixel((Math.Min(x1, x2) + k) * 3 + 1, y2 * 3 + 2, Color.FromArgb(t, q, w, e));
                        bit.SetPixel((Math.Min(x1, x2) + k) * 3 + 2, y2 * 3 + 1, Color.FromArgb(t, q, w, e));
                        bit.SetPixel((Math.Min(x1, x2) + k) * 3 + 2, y2 * 3 + 2, Color.FromArgb(t, q, w, e));
                        bit.SetPixel((Math.Min(x1, x2) + k) * 3 + 3, y2 * 3 + 2, Color.FromArgb(t, q, w, e));
                        bit.SetPixel((Math.Min(x1, x2) + k) * 3 + 3, y2 * 3 + 1, Color.FromArgb(t, q, w, e));
                    }
                }


                temp1 = temp;

                if (rate_of_change_colour > 1)
                {
                    q += (int)rate_of_change_colour;
                    w -= (int)rate_of_change_colour;
                }
                else
                {
                    if (cont_rate_change > 1)
                    {
                        cont_rate_change %= 1;
                    }
                    cont_rate_change += rate_of_change_colour;
                    q += (int)cont_rate_change;
                    w -= (int)cont_rate_change;
                }

                //rate_of_change_colour += rate_of_change_colour;
                if (q > 255)
                {
                    q = 255;
                }
                if (w < 0)
                {
                    w = 0;
                }
            }
            //END HERE--------------------------------------------------------------------

            ///color start box
            t = 255;
            q = 0;
            w = 255;
            e = 0;

            bit.SetPixel(1, start_col * 3 + 1, Color.FromArgb(t, q, w, e));
            bit.SetPixel(1, start_col * 3 + 2, Color.FromArgb(t, q, w, e));
            bit.SetPixel(2, start_col * 3 + 1, Color.FromArgb(t, q, w, e));
            bit.SetPixel(2, start_col * 3 + 2, Color.FromArgb(t, q, w, e));

            ///set finish colour
            t = 255;
            q = 255;
            w = 0;
            e = 0;

            bit.SetPixel(height - 3, width - 3, Color.FromArgb(t, q, w, e));
            bit.SetPixel(height - 2, width - 3, Color.FromArgb(t, q, w, e));
            bit.SetPixel(height - 2, width - 2, Color.FromArgb(t, q, w, e));
            bit.SetPixel(height - 3, width - 2, Color.FromArgb(t, q, w, e));

            if (height < pictureBox1.Height && width < pictureBox1.Width)
            {   ///INTERPOLATION IF FOR HOW TO MAXIMIZE THE BITMAP IT WAS USING BLEN WUHC BLEND WITHE WITH BLACK -> GREY
                Bitmap   resized = new Bitmap(bit.Height * (int)(pictureBox1.Height / height), bit.Width * (int)(pictureBox1.Width / width));
                Graphics g       = Graphics.FromImage(resized);
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
                g.DrawImage(bit, 0, 0, bit.Height * (int)(pictureBox1.Height / height), bit.Width * (int)(pictureBox1.Width / width));
                bit = new Bitmap(resized.Height, resized.Width);
                bit = resized;
            }


            bit.RotateFlip(RotateFlipType.Rotate90FlipNone);


            bit.Save("D:\\Img_maze\\Img.png");

            pictureBox1.Image = bit;
        }
        public Maze_display_depth()
        {
            InitializeComponent();

            height = Form1.rows;
            width  = Form1.cols;

            int [] wall            = new int[4];
            int    size_wall_array = 0;

            cal_matrix();
            Cell.rows = rows;
            Cell.cols = cols;
            grid      = new Cell[rows][];



            for (int i = 0; i < rows; i++)
            {
                grid[i] = new Cell[cols];
                for (int j = 0; j < cols; j++)
                {
                    grid[i][j] = new Cell(i, j);
                }
            }

            Stack <Cell> stk = new Stack <Cell>();

            //i'll pick the start on the top row, but on a random coloumn;
            int start_col;

            start_col = rnd.Next(0, cols);
            grid[0][start_col].visited = 1;

            //push the start on the stack
            stk.Push(grid[0][start_col]);


            while (stk.Count > 0)
            {
                Cell temp = stk.Peek();
                initiate_vect(wall);
                cell_update(ref temp);
                get_available_walls_of_cell(wall, ref size_wall_array, temp);

                int rand_wall = get_random_wall(wall, size_wall_array);

                if (rand_wall == -1)
                {
                    stk.Pop();
                }

                else
                {
                    switch (rand_wall)
                    {
                    //up
                    case 0:
                    {
                        temp.delete_wall(0);
                        Cell to_go = grid[temp.x - 1][temp.y];
                        to_go.delete_wall(1);
                        to_go.visited = 1;
                        stk.Push(to_go);
                    }
                    break;

                    //down
                    case 1:
                    {
                        temp.delete_wall(1);
                        Cell to_go = grid[temp.x + 1][temp.y];
                        to_go.delete_wall(0);
                        to_go.visited = 1;
                        stk.Push(to_go);
                    }
                    break;

                    //left
                    case 2:
                    {
                        temp.delete_wall(2);
                        Cell to_go = grid[temp.x][temp.y - 1];
                        to_go.delete_wall(3);
                        to_go.visited = 1;
                        stk.Push(to_go);
                    }
                    break;

                    //right
                    case 3:
                    {
                        temp.delete_wall(3);
                        Cell to_go = grid[temp.x][temp.y + 1];
                        to_go.delete_wall(2);
                        to_go.visited = 1;
                        stk.Push(to_go);
                    }
                    break;
                    }
                }
            }

            Bitmap bit = new Bitmap(height, width);

            create_bitmap(ref bit);

            //REZOLVARE--------------------------------------------------------------------
            Depth_solve depth_solve = new Depth_solve(rows, cols, grid, nr_nodes, start_col);

            //Dijkstra_solve dij = new Dijkstra_solve(rows, cols, grid, nr_nodes,start_col);

            if (!depth_solve.solved)
            {
                System.Windows.Forms.MessageBox.Show("The maze wasn't solved");
            }

            Stack <KeyValuePair <int, int> > road = depth_solve.get_road();

            int t = 255;
            int q = 0;
            int w = 255;
            int e = 0;

            double rate_of_change_colour = (double)255 / road.Count;
            double cont_rate_change      = rate_of_change_colour;

            KeyValuePair <int, int> temp1 = road.Pop();

            while (road.Count > 0)
            {
                KeyValuePair <int, int> temp = road.Pop();

                int x1 = temp.Key;
                int x2 = temp1.Key;
                int y1 = temp.Value;
                int y2 = temp1.Value;

                if (x1 == x2)
                {
                    for (int k = 0; k < Math.Abs(y1 - y2); k++)
                    {
                        bit.SetPixel(x1 * 3 + 1, (Math.Min(y1, y2) + k) * 3 + 1, Color.FromArgb(t, q, w, e));
                        bit.SetPixel(x1 * 3 + 1, (Math.Min(y1, y2) + k) * 3 + 2, Color.FromArgb(t, q, w, e));
                        bit.SetPixel(x1 * 3 + 2, (Math.Min(y1, y2) + k) * 3 + 1, Color.FromArgb(t, q, w, e));
                        bit.SetPixel(x1 * 3 + 2, (Math.Min(y1, y2) + k) * 3 + 2, Color.FromArgb(t, q, w, e));
                        bit.SetPixel(x1 * 3 + 1, (Math.Min(y1, y2) + k) * 3 + 3, Color.FromArgb(t, q, w, e));
                        bit.SetPixel(x1 * 3 + 2, (Math.Min(y1, y2) + k) * 3 + 3, Color.FromArgb(t, q, w, e));
                    }
                    bit.SetPixel(x1 * 3 + 1, Math.Max(y1, y2) * 3 + 1, Color.FromArgb(t, q, w, e));
                    bit.SetPixel(x1 * 3 + 2, Math.Max(y1, y2) * 3 + 2, Color.FromArgb(t, q, w, e));
                    bit.SetPixel(x1 * 3 + 1, Math.Max(y1, y2) * 3 + 2, Color.FromArgb(t, q, w, e));
                    bit.SetPixel(x1 * 3 + 2, Math.Max(y1, y2) * 3 + 1, Color.FromArgb(t, q, w, e));
                }
                else
                {
                    for (int k = 0; k < Math.Abs(x1 - x2); k++)
                    {
                        bit.SetPixel((Math.Min(x1, x2) + k) * 3 + 1, y2 * 3 + 1, Color.FromArgb(t, q, w, e));
                        bit.SetPixel((Math.Min(x1, x2) + k) * 3 + 1, y2 * 3 + 2, Color.FromArgb(t, q, w, e));
                        bit.SetPixel((Math.Min(x1, x2) + k) * 3 + 2, y2 * 3 + 1, Color.FromArgb(t, q, w, e));
                        bit.SetPixel((Math.Min(x1, x2) + k) * 3 + 2, y2 * 3 + 2, Color.FromArgb(t, q, w, e));
                        bit.SetPixel((Math.Min(x1, x2) + k) * 3 + 3, y2 * 3 + 2, Color.FromArgb(t, q, w, e));
                        bit.SetPixel((Math.Min(x1, x2) + k) * 3 + 3, y2 * 3 + 1, Color.FromArgb(t, q, w, e));
                    }
                }


                temp1 = temp;

                if (rate_of_change_colour > 1)
                {
                    q += (int)rate_of_change_colour;
                    w -= (int)rate_of_change_colour;
                }
                else
                {
                    if (cont_rate_change > 1)
                    {
                        cont_rate_change %= 1;
                    }
                    cont_rate_change += rate_of_change_colour;
                    q += (int)cont_rate_change;
                    w -= (int)cont_rate_change;
                }

                //rate_of_change_colour += rate_of_change_colour;
                if (q > 255)
                {
                    q = 255;
                }
                if (w < 0)
                {
                    w = 0;
                }
            }
            //END HERE--------------------------------------------------------------------

            ///color start box
            t = 255;
            q = 0;
            w = 255;
            e = 0;

            bit.SetPixel(1, start_col * 3 + 1, Color.FromArgb(t, q, w, e));
            bit.SetPixel(1, start_col * 3 + 2, Color.FromArgb(t, q, w, e));
            bit.SetPixel(2, start_col * 3 + 1, Color.FromArgb(t, q, w, e));
            bit.SetPixel(2, start_col * 3 + 2, Color.FromArgb(t, q, w, e));

            ///set finish colour

            if (depth_solve.solved)
            {
                t = 255;
                q = 255;
                w = 0;
                e = 0;

                bit.SetPixel(height - 3, width - 3, Color.FromArgb(t, q, w, e));
                bit.SetPixel(height - 2, width - 3, Color.FromArgb(t, q, w, e));
                bit.SetPixel(height - 2, width - 2, Color.FromArgb(t, q, w, e));
                bit.SetPixel(height - 3, width - 2, Color.FromArgb(t, q, w, e));
            }
            else
            {
                t = 255;
                q = 255;
                w = 0;
                e = 0;

                KeyValuePair <int, int> false_finish = new KeyValuePair <int, int>(depth_solve.cell_case_lab_hasnt_solve.x, depth_solve.cell_case_lab_hasnt_solve.y);

                bit.SetPixel(false_finish.Key * 3 + 1, false_finish.Value * 3 + 1, Color.FromArgb(t, q, w, e));
                bit.SetPixel(false_finish.Key * 3 + 1, false_finish.Value * 3 + 2, Color.FromArgb(t, q, w, e));
                bit.SetPixel(false_finish.Key * 3 + 2, false_finish.Value * 3 + 1, Color.FromArgb(t, q, w, e));
                bit.SetPixel(false_finish.Key * 3 + 2, false_finish.Value * 3 + 2, Color.FromArgb(t, q, w, e));
            }


            if (height < pictureBox1.Height && width < pictureBox1.Width)
            {   ///INTERPOLATION IF FOR HOW TO MAXIMIZE THE BITMAP IT WAS USING BLEN WUHC BLEND WITHE WITH BLACK -> GREY
                Bitmap   resized = new Bitmap(bit.Height * (int)(pictureBox1.Height / height), bit.Width * (int)(pictureBox1.Width / width));
                Graphics g       = Graphics.FromImage(resized);
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
                g.DrawImage(bit, 0, 0, bit.Height * (int)(pictureBox1.Height / height), bit.Width * (int)(pictureBox1.Width / width));
                bit = new Bitmap(resized.Height, resized.Width);
                bit = resized;
            }


            bit.RotateFlip(RotateFlipType.Rotate90FlipNone);


            bit.Save("D:\\Img_maze\\Img.png");

            pictureBox1.Image = bit;
        }