示例#1
0
        private void draw()
        {
            myg.FillRectangle(new SolidBrush(Color.White), 0, 0, pictureBox1.Width, pictureBox1.Height);
            Image image0 = System.Drawing.Image.FromFile("../../tile.bmp");
            Image image1 = System.Drawing.Image.FromFile("../../ball0.bmp");
            //image0对象的宽度和高度
            int pw = image0.Width;
            int ph = image0.Height;

            if (flag == false)
            {
                for (int i = 0; i < m_maze3.Rows + 2; i++)
                {
                    for (int j = 0; j < m_maze3.Cols + 2; j++)
                    {
                        Rectangle rt1 = new Rectangle(0, 0, pw, ph);
                        Rectangle rt2 = new Rectangle(j * (pw), i * (ph), pw, ph);
                        if (mouseClick.X >= rt2.X && mouseClick.X <= rt2.X + pw && mouseClick.Y >= rt2.Y && mouseClick.Y <= rt2.Y + ph)
                        {
                            if (m_maze3.Getelems(i, j) == -1)
                            {
                                m_maze3.Setelems(i, j, 1);
                            }
                            else
                            {
                                m_maze3.Setelems(i, j, (m_maze3.Getelems(i, j) + 1) % 2);
                            }
                            break;
                        }
                    }
                }
            }
            else
            {
                for (int i = 0; i < m_maze3.Rows + 2; i++)
                {
                    for (int j = 0; j < m_maze3.Cols + 2; j++)
                    {
                        Rectangle rt1 = new Rectangle(0, 0, pw, ph);
                        Rectangle rt2 = new Rectangle(j * (pw), i * (ph), pw, ph);
                        if (mouseClick.X >= rt2.X && mouseClick.X <= rt2.X + pw && mouseClick.Y >= rt2.Y && mouseClick.Y <= rt2.Y + ph)
                        {
                            moved t = new moved();
                            t.initmoved(i, j);
                            if (startORend == false)
                            {
                                m_maze3.Start = t;
                            }
                            else
                            {
                                m_maze3.End = t;
                            }
                            startORend = !startORend;
                        }
                    }
                }
            }
            //m_maze3.ShortPath2();
            //定义矩形区域


            for (int i = 0; i < m_maze3.Rows + 2; i++)
            {
                for (int j = 0; j < m_maze3.Cols + 2; j++)
                {
                    Rectangle rt1 = new Rectangle(0, 0, pw, ph);
                    Rectangle rt2 = new Rectangle(j * (pw), i * (ph), pw, ph);

                    if (m_maze3.Getelems(i, j) == 1)
                    {
                        myg.DrawImage(image0, rt2, rt1, GraphicsUnit.Pixel);
                    }
                    //else if(m_maze3.Getelems(i,j)==-1)
                    //{
                    //   myg.DrawImage(image1,rt2,rt1,GraphicsUnit.Pixel);
                    //}
                }
            }
            Image     startImage = System.Drawing.Image.FromFile("../../p1.bmp");
            Image     endImage   = System.Drawing.Image.FromFile("../../p2.bmp");
            Rectangle rt_1       = new Rectangle((pw) * m_maze3.Start.y, (ph) * m_maze3.Start.x, pw, ph);
            Rectangle rt_2       = new Rectangle(0, 0, pw, ph);

            myg.DrawImage(startImage, rt_1, rt_2, GraphicsUnit.Pixel);
            Rectangle rt_3 = new Rectangle((pw) * m_maze3.End.y, (ph) * m_maze3.End.x, pw, ph);

            myg.DrawImage(endImage, rt_3, rt_2, GraphicsUnit.Pixel);
            //从image0上根据矩形rt1取出来的部分图像,贴到由rt2确定的矩形区域上
            for (int i = 0; i < m_maze3.Rows + 2; i++)
            {
                for (int j = 0; j < m_maze3.Cols + 2; j++)
                {
                    if (m_maze3.Getelems(i, j) == -1)
                    {
                        m_maze3.Setelems(i, j, 0);
                    }
                }
            }
            int i_x = m_maze3.Start.x; int j_y = m_maze3.Start.y;
            CSeqStack <sqtype> m_cstack = new CSeqStack <sqtype>();

            moved[] move = new moved[4];
            move[0].x = 0; move[0].y = +1;
            move[1].x = +1; move[1].y = 0;
            move[2].x = 0; move[2].y = -1;
            move[3].x = -1; move[3].y = 0;
            sqtype temp = new sqtype();

            temp.x = i_x; temp.y = j_y; temp.pre = 0;
            m_cstack.Push(temp);
            while (!(i_x == m_maze3.End.x && j_y == m_maze3.End.y))
            {
                bool flag_1 = false;
                for (int k = 0; k < 4; k++)
                {
                    int i = i_x + move[k].x;
                    int j = j_y + move[k].y;
                    if (m_maze3.Getelems(i, j) == 0)
                    {
                        i_x    = i; j_y = j;
                        temp.x = i_x; temp.y = j_y;
                        m_cstack.Push(temp);
                        Rectangle rt1 = new Rectangle(0, 0, pw, ph);
                        Rectangle rt2 = new Rectangle(j_y * (pw), i_x * (ph), pw, ph);
                        myg.DrawImage(image1, rt2, rt1, GraphicsUnit.Pixel);
                        m_maze3.Setelems(i, j, -1);
                        System.Threading.Thread.Sleep(100);
                        flag_1 = true;
                        break;
                    }
                }
                if (flag_1 == false)
                {
                    sqtype    temp1 = m_cstack.Pop();
                    Rectangle rt1   = new Rectangle(pw * temp1.y, ph * temp1.x, pw, ph);
                    myg.FillRectangle(new SolidBrush(Color.White), rt1);
                    System.Threading.Thread.Sleep(100);
                    i_x = m_cstack.Gettop().x; j_y = m_cstack.Gettop().y;
                }
            }
            //m_maze3.ShortPath();
        }
示例#2
0
        private void DrawMaze(CMaze m_maze, bool flag)
        {
            Brush b1  = new SolidBrush(Color.Gray);
            Brush b2  = new SolidBrush(Color.Yellow);
            Brush b3  = new SolidBrush(Color.Green);
            Brush b4  = new SolidBrush(Color.White);
            Pen   pen = new Pen(Color.Red, 1);

            if (flag == false)
            {
                int width  = pictureBox1.Width;
                int height = pictureBox1.Height;
                myg1.FillRectangle(b4, new Rectangle(0, 0, width, height));
                Point start      = new Point(30, 30);
                int   interval_x = Convert.ToInt16((width - 60) / m_maze.Cols);
                int   interval_y = Convert.ToInt16((height - 60) / m_maze.Rows);

                for (int i = 0; i < m_maze.Rows; i++)
                {
                    for (int j = 0; j < m_maze.Cols; j++)
                    {
                        Point p1 = new Point(start.X + interval_x * j, start.Y + interval_y * i);
                        Point p2 = new Point(p1.X + interval_x, p1.Y + interval_y);
                        if (m_maze.Getelems(i + 1, j + 1) == 1)
                        {
                            myg1.FillRectangle(b1, p1.X, p1.Y, interval_x, interval_y);
                        }
                        else if (m_maze.Getelems(i + 1, j + 1) == 0)
                        {
                            myg1.FillRectangle(b2, p1.X, p1.Y, interval_x, interval_y);
                        }
                    }
                }
                for (int i = 0; i <= m_maze.Rows; i++)
                {
                    Point p1   = new Point(start.X, start.Y + interval_y * i);
                    Point p2   = new Point(start.X + interval_x * m_maze.Cols, start.Y + interval_y * i);
                    Font  font = new Font("华为宋体", 12);
                    if (i < m_maze.Rows)
                    {
                        myg1.DrawString(Convert.ToString(i + 1), font, b3, new Point(p1.X - 20, p1.Y));
                    }
                    myg1.DrawLine(pen, p1, p2);
                }
                for (int i = 0; i <= m_maze.Cols; i++)
                {
                    Point p1   = new Point(start.X + interval_x * i, start.Y);
                    Point p2   = new Point(start.X + interval_x * i, start.Y + interval_y * m_maze.Rows);
                    Font  font = new Font("华为宋体", 12);
                    if (i < m_maze.Cols)
                    {
                        myg1.DrawString(Convert.ToString(i + 1), font, b3, new Point(p1.X, p1.Y - 20));
                    }
                    myg1.DrawLine(pen, p1, p2);
                }
            }
            else
            {
                int width  = pictureBox2.Width;
                int height = pictureBox2.Height;
                myg2.FillRectangle(b4, new Rectangle(0, 0, width, height));
                Point start      = new Point(30, 30);
                int   interval_x = Convert.ToInt16((width - 60) / m_maze.Cols);
                int   interval_y = Convert.ToInt16((height - 60) / m_maze.Rows);

                for (int i = 0; i < m_maze.Rows; i++)
                {
                    for (int j = 0; j < m_maze.Cols; j++)
                    {
                        Point p1 = new Point(start.X + interval_x * j, start.Y + interval_y * i);
                        Point p2 = new Point(p1.X + interval_x, p1.Y + interval_y);
                        if (m_maze.Getelems(i + 1, j + 1) == 1)
                        {
                            myg2.FillRectangle(b1, p1.X, p1.Y, interval_x, interval_y);
                        }
                        else if (m_maze.Getelems(i + 1, j + 1) == 0)
                        {
                            myg2.FillRectangle(b2, p1.X, p1.Y, interval_x, interval_y);
                        }
                        else if (m_maze.Getelems(i + 1, j + 1) == -1)
                        {
                            myg2.FillRectangle(b3, p1.X, p1.Y, interval_x, interval_y);
                        }
                    }
                }
                for (int i = 0; i <= m_maze.Rows; i++)
                {
                    Point p1   = new Point(start.X, start.Y + interval_y * i);
                    Point p2   = new Point(start.X + interval_x * m_maze.Cols, start.Y + interval_y * i);
                    Font  font = new Font("华为宋体", 12);
                    if (i < m_maze.Rows)
                    {
                        myg2.DrawString(Convert.ToString(i + 1), font, b3, new Point(p1.X - 20, p1.Y));
                    }
                    myg2.DrawLine(pen, p1, p2);
                }
                for (int i = 0; i <= m_maze.Cols; i++)
                {
                    Point p1   = new Point(start.X + interval_x * i, start.Y);
                    Point p2   = new Point(start.X + interval_x * i, start.Y + interval_y * m_maze.Rows);
                    Font  font = new Font("华为宋体", 12);
                    if (i < m_maze.Cols)
                    {
                        myg2.DrawString(Convert.ToString(i + 1), font, b3, new Point(p1.X, p1.Y - 20));
                    }
                    myg2.DrawLine(pen, p1, p2);
                }
            }
        }
示例#3
0
        private void draw()
        {
            myg.FillRectangle(new SolidBrush(Color.White), 0, 0, pictureBox1.Width, pictureBox1.Height);
            Image image0 = System.Drawing.Image.FromFile("../../tile.bmp");
            Image image1 = System.Drawing.Image.FromFile("../../ball0.bmp");
            //image0对象的宽度和高度
            int pw = image0.Width;
            int ph = image0.Height;

            if (flag == false)
            {
                for (int i = 0; i < m_maze2.Rows + 2; i++)
                {
                    for (int j = 0; j < m_maze2.Cols + 2; j++)
                    {
                        Rectangle rt1 = new Rectangle(0, 0, pw, ph);
                        Rectangle rt2 = new Rectangle(j * (pw), i * (ph), pw, ph);
                        if (mouseClick.X >= rt2.X && mouseClick.X <= rt2.X + pw && mouseClick.Y >= rt2.Y && mouseClick.Y <= rt2.Y + ph)
                        {
                            if (m_maze2.Getelems(i, j) == -1)
                            {
                                m_maze2.Setelems(i, j, 1);
                            }
                            else
                            {
                                m_maze2.Setelems(i, j, (m_maze2.Getelems(i, j) + 1) % 2);
                            }
                            break;
                        }
                    }
                }
            }
            else
            {
                for (int i = 0; i < m_maze2.Rows + 2; i++)
                {
                    for (int j = 0; j < m_maze2.Cols + 2; j++)
                    {
                        Rectangle rt1 = new Rectangle(0, 0, pw, ph);
                        Rectangle rt2 = new Rectangle(j * (pw), i * (ph), pw, ph);
                        if (mouseClick.X >= rt2.X && mouseClick.X <= rt2.X + pw && mouseClick.Y >= rt2.Y && mouseClick.Y <= rt2.Y + ph)
                        {
                            moved t = new moved();
                            t.initmoved(i, j);
                            if (startORend == false)
                            {
                                m_maze2.Start = t;
                            }
                            else
                            {
                                m_maze2.End = t;
                            }
                            startORend = !startORend;
                        }
                    }
                }
            }
            m_maze2.ShortPath2();
            //定义矩形区域
            for (int i = 0; i < m_maze2.Rows + 2; i++)
            {
                for (int j = 0; j < m_maze2.Cols + 2; j++)
                {
                    Rectangle rt1 = new Rectangle(0, 0, pw, ph);
                    Rectangle rt2 = new Rectangle(j * (pw), i * (ph), pw, ph);

                    if (m_maze2.Getelems(i, j) == 1)
                    {
                        myg.DrawImage(image0, rt2, rt1, GraphicsUnit.Pixel);
                    }
                    else if (m_maze2.Getelems(i, j) == -1)
                    {
                        myg.DrawImage(image1, rt2, rt1, GraphicsUnit.Pixel);
                    }
                }
            }
            Image     startImage = System.Drawing.Image.FromFile("../../p1.bmp");
            Image     endImage   = System.Drawing.Image.FromFile("../../p2.bmp");
            Rectangle rt_1       = new Rectangle((pw) * m_maze2.Start.y, (ph) * m_maze2.Start.x, pw, ph);
            Rectangle rt_2       = new Rectangle(0, 0, pw, ph);

            myg.DrawImage(startImage, rt_1, rt_2, GraphicsUnit.Pixel);
            Rectangle rt_3 = new Rectangle((pw) * m_maze2.End.y, (ph) * m_maze2.End.x, pw, ph);

            myg.DrawImage(endImage, rt_3, rt_2, GraphicsUnit.Pixel);
            //从image0上根据矩形rt1取出来的部分图像,贴到由rt2确定的矩形区域上

            //m_maze2.ShortPath();
        }