Ejemplo n.º 1
0
        private void backgroundWorker2_ProgressChanged(object sender,
                                                       ProgressChangedEventArgs e)
        {
            int x  = Current.x;
            int y  = Current.y;
            int x1 = x;
            int y1 = y;
            int d  = Current.direction;

            y += -1 * ((d - 1) / 3 - 1);
            x += -1 * ((d - 1) % 3 - 1);

            if (Current.x == 0 && Current.y == 0)
            {
                NeedCloseFinal = true;
                return;
            }

            Pen LightBluePen = new Pen(Color.FromArgb(100, 100, 255), 7);

            try
            {
                g.DrawLine(LightBluePen, x * size + size / 2, y * size + size / 2,
                           x1 * size + size / 2, y1 * size + size / 2);
                this.CreateGraphics().DrawImageUnscaled(backBmp, 0, 0);
                int index = CloseList.FindIndex(v => v.x == x && v.y == y);
                Current = CloseList[index];
            }
            catch (Exception E) { }
            if (x == 0 && y == 0)
            {
                backgroundWorker2.CancelAsync();
            }
        }
Ejemplo n.º 2
0
        private void DrawOpenOnce(Estimate v)//劃出所有OpenList
        {
            int x = v.x;
            int y = v.y;

            if (x == 0 && y == 0 || (width - 1) == x && (height - 1) == y)
            {
                return;
            }
            g.FillRectangle(GreenBrush, x * size, y * size, size - 1, size - 1);
        }
Ejemplo n.º 3
0
        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            try
            {
                if (NeedCloseFlag)
                {
                    return;
                }
                DrawCloseOnce();
                this.CreateGraphics().DrawImageUnscaled(backBmp, 0, 0);

                Previous = Current;     //紀錄前一個點
                Current  = OpenList[0]; //移動當前點  index0是排序後最佳的點
                CloseList.Add(Current); //當前點加入CloseList
            }
            catch (Exception E)
            { }


            if (Current == null)
            {
                return;
            }
            for (int i = -1; i <= 1; i++)//新增OpenPoint
            {
                for (int j = -1; j <= 1; j++)
                {
                    int x             = Current.x + j;
                    int y             = Current.y + i;
                    int OBstacleIndex = Obstacle.FindIndex(v => v.X == x && v.Y == y);
                    int CloseIndex    = CloseList.FindIndex(v => v.x == x && v.y == y);
                    if (OBstacleIndex >= 0 ||
                        CloseIndex >= 0)//如果ObstacleList有點P就跳過
                    {
                        continue;
                    }
                    if (y < 0 || y > height - 1 || x < 0 || x > width - 1)//超出範圍
                    {
                        continue;
                    }
                    int x1 = x;
                    int y1 = y;
                    if (x1 < y1)
                    {
                        Swap <int>(ref x1, ref y1);
                    }
                    int h = ((width - 1 - x) + (height - y - 1)) * 10;
                    int d = (i + 1) * 3 + (j + 1) + 1;//計算移動方向
                    int g;
                    if (i == j || i * -1 == j || j * -1 == i)
                    {
                        g = Current.g + 16;
                    }
                    else
                    {
                        g = Current.g + 10;
                    }
                    int index = OpenList.FindIndex(v => v.x == x && v.y == y);
                    if (index >= 0)                             //OpenList有發現點P
                    {
                        if (OpenList[index].g > g)              //比較g的大小
                        {
                            OpenList[index].Set(x, y, g, h, d); //更新g
                            DrawOpenOnce(new Estimate(x, y, g, h, d));
                        }
                    }
                    else
                    {
                        OpenList.Add(new Estimate(x, y, g, h, d));
                        DrawOpenOnce(new Estimate(x, y, g, h, d));
                        //新增OpenList
                    }
                }
            }

            try
            {
                int index = OpenList.FindIndex(v => v == Previous); //2.刪除前一個當前點
                if (index >= 0)                                     //2.刪除前一個當前點
                {
                    OpenList.RemoveAt(index);                       //2.刪除前一個當前點
                }
                index = OpenList.FindIndex(v => v.x == width - 1 &&
                                           v.y == height - 1); //找看看有沒有到終點
                if (OpenList.Count == 0 || index >= 0)         //判斷結束條件
                {
                    if (index >= 0)
                    {
                        Current     = OpenList[index];
                        label2.Text = "Done for sarch path\nSearching " +
                                      SearchTime.ToString() + " times";
                    }
                    else
                    {
                        label2.Text = "Can't find path";
                    }
                    NeedCloseFlag = true;
                    backgroundWorker1.CancelAsync();

                    g.Clear(Color.Black);
                    DrawOpen();
                    DrawClose();
                    DrawObstacle();

                    return;
                }

                OpenList.Sort((x, y) =>
                {
                    int n = x.f.CompareTo(y.f);
                    if (n == 0)
                    {
                        return(x.h.CompareTo(y.h));
                    }
                    return(n);
                });//對Open的F做排序
            }
            catch (Exception E) { }
            SearchTime++;
        }