Esempio n. 1
0
        private void Deixtr_Click(object sender, EventArgs e)
        {
            int width  = pictureBox1.Width / set + 1;
            int height = pictureBox1.Height / set + 1;

            path[] PathL    = new path[40000 * width * height + 1];
            path   TruePath = new path();
            path   First    = new path();

            First.Count  = 0;
            First.lenght = 0;
            First.x      = CircleX / set;
            First.y      = CircleY / set;
            First.h      = h(First.x, First.y);


            int[,] NewMap = new int[width, height];
            NewMap        = MapBuild();

            PathL[1] = First;

            Open.Clear();
            Close.Clear();
            Open.Add(First);

            int cc = 0;


            Graphics ob = pictureBox1.CreateGraphics();

            ob.Clear(Color.White);

            while (Open.Count != 0)
            {
                cc++;
                List <path> OpenSorted   = (from t in Open orderby t.lenght select t).ToList <path>();
                path        CurrentPoint = OpenSorted[0];

                //Rectangle rect = new Rectangle(CurrentPoint.x*set,CurrentPoint.y*set,set,set);
                //ob.FillRectangle(Brushes.Green, rect);

                if (CurrentPoint.x == FinX && CurrentPoint.y == FinY)
                {
                    TruePath = CurrentPoint;
                    break;
                }
                for (int i = -1; i <= 1; i++)
                {
                    for (int j = -1; j <= 1; j++)
                    {
                        if (i != 0 || j != 0)
                        {
                            path NewP  = CreateNewPath(CurrentPoint, i, j);
                            int  tempx = NewP.x;
                            int  tempy = NewP.y;
                            if (NewMap[NewP.x, NewP.y] == 1)
                            {
                                AddToOpen(NewP);
                            }
                        }
                    }
                }
                AddToClose(CurrentPoint);
                NewMap[CurrentPoint.x, CurrentPoint.y] = 0;
            }

            labelLen.Text   = (TruePath.lenght * set).ToString();
            labelCount.Text = cc.ToString();
            path[]  TrueP     = new path[TruePath.Count + 1];
            Point[] PathPoint = new Point[TruePath.Count + 1];
            TrueP[TruePath.Count] = TruePath;
            int error = 0;

            PathPoint[TruePath.Count].X = TruePath.x;
            PathPoint[TruePath.Count].Y = TruePath.y;
            for (int i = TruePath.Count - 1; i >= 0; i--)
            {
                TrueP[i]       = TrueP[i + 1].PrevPath;
                PathPoint[i].X = TrueP[i].x;
                PathPoint[i].Y = TrueP[i].y;
                if (PathPoint[i].X == CircleX && PathPoint[i].Y == CircleY)
                {
                    error = i;
                    break;
                }
            }



            int CurrentX = CircleX;
            int CurrentY = CircleY;

            for (int t = error * set; t < TruePath.Count * set; t++)
            {
                int i    = t / set;
                int XMot = PathPoint[i + 1].X - PathPoint[i].X;
                int YMot = PathPoint[i + 1].Y - PathPoint[i].Y;
                ob.FillEllipse(Brushes.Black, CurrentX, CurrentY, CircleR, CircleR);
                CurrentX += XMot;
                CurrentY += YMot;
                foreach (Obstacles obst in SqObst)
                {
                    PointF   P1    = new PointF(obst.p1.X, obst.p1.Y);
                    PointF   P2    = new PointF(obst.p2.X, obst.p2.Y);
                    PointF   P3    = new PointF(obst.p3.X, obst.p3.Y);
                    PointF   P4    = new PointF(obst.p4.X, obst.p4.Y);
                    PointF[] ArrOb =
                    {
                        P1,
                        P2,
                        P3,
                        P4,
                        P1
                    };
                    Pen BluePen = new Pen(Color.Blue, 3);
                    ob.DrawPolygon(BluePen, ArrOb);
                }

                //ob.Clear(Color.White);
            }
        }
Esempio n. 2
0
 private void AddToOpen(path p)
 {
     Open.Add(p);
 }