示例#1
0
        public void InitDrawGrid(TableObject tableObj = null)
        {
            try
            {
                ThreadedInfoBox TinfoBox = new ThreadedInfoBox();
                TinfoBox.Canceled += (o) => {
                    if (o == Operation.Creating)
                    {
                        this.Dispatcher.BeginInvoke(new Action(() => this.Close()));
                    }
                };
                TinfoBox.StartNewThreadInfoBox(Operation.Creating, "Creating table structure ...", this.Title);

                DrawGrid();
                if (tableObj != null)
                {
                    FillSavedMapFields(tableObj);
                }
                this.Show();

                TinfoBox.EndNewThreadInfoBox();
            }
            catch { }
        }
示例#2
0
        private void AStarAlgorithm()
        {
            #region "Initiate"

            CancelProcessing       = false;
            ApplyingAStarAlgorithm = true;
            bool             AtLeastOnePathOpen = false;
            List <Rectangle> OpenList           = new List <Rectangle>();
            List <Rectangle> ClosedList         = new List <Rectangle>();
            ClearYellowAndCarTracks();
            ClearSearchPaths();
            List <List <Rectangle> > MasterPath = new List <List <Rectangle> >();

            Rectangle MasterStartPoint = (myGrid.Children.OfType <Rectangle>()).Where(r => (r.Fill as ImageBrush).ImageSource == GreenFlag.ImageSource).FirstOrDefault();
            Rectangle StartPoint       = MasterStartPoint;
            Rectangle Current          = StartPoint;

            List <Rectangle> AccumulatedEndPointList = new List <Rectangle>();
            List <Rectangle> CheckedEndPoints        = new List <Rectangle>();
            List <Rectangle> EndPointList            = CreateEndPointList(StartPoint, CheckedEndPoints);

            int       NumberOfEndPoints = EndPointList.Count;
            Rectangle EndPoint          = EndPointList.FirstOrDefault();

            #endregion

            #region "Starto nje ThreadedInfoBox me tregue procesimin"

            ThreadedInfoBox TinfoBox = new ThreadedInfoBox(WindowStartupLocation.Manual, 50, 250);
            TinfoBox.Canceled += (o) =>
            {
                if (o == Operation.Processing)
                {
                    CancelProcessing = true;
                }
            };
            TinfoBox.StartNewThreadInfoBox(Operation.Processing, "Processing path ...", this.Title);

            #endregion

            #region "Algoritmi"

MoveNext:

            OpenList.Add(Current);

            while (OpenList.Count != 0)
            {
                if (CancelProcessing)
                {
                    TinfoBox.EndNewThreadInfoBox(); return;
                }

                Current = OpenList.ApplyUncertainty(UncertaintyLevel);           // Merre Current rectangle amo me nji uncertainty

                if (NumberOfEndPoints != 0)
                {
                    EndPointList = CreateEndPointList(Current, CheckedEndPoints);     // Checkirati distancat me endpoints qe jane me afer Current
                    EndPoint     = EndPointList.FirstOrDefault();
                }

                if ((Current.Tag as RectangleParameter).GridPosition.Location == (EndPoint.Tag as RectangleParameter).GridPosition.Location)
                {
                    #region "EndPoint Found"

                    AtLeastOnePathOpen = true;
                    List <Rectangle> Path = new List <Rectangle>();
                    Path = ReconstructPath(StartPoint, EndPoint);

                    if (!MasterPath.Contains(Path))
                    {
                        MasterPath.Add(Path);
                    }

                    if (!AccumulatedEndPointList.Contains(EndPoint))
                    {
                        AccumulatedEndPointList.Add(EndPoint);
                    }

                    NumberOfEndPoints--;
                    if (NumberOfEndPoints > 0)
                    {
                        if (!CheckedEndPoints.Contains(EndPoint))
                        {
                            CheckedEndPoints.Add(EndPoint);
                        }

                        StartPoint = Current;

                        ClosedList.Clear();
                        OpenList.Clear();
                        goto MoveNext;
                    }
                    else if (NumberOfEndPoints == 0 && BackPathType == BackPathType.Shortest)
                    {
                        StartPoint  = Current;
                        EndPoint    = MasterStartPoint;
                        IsGoingBack = true;

                        ClosedList.Clear();
                        OpenList.Clear();
                        goto MoveNext;
                    }

                    IsGoingBack = false;
                    DrawPath(MasterPath, AccumulatedEndPointList, MasterStartPoint, TinfoBox, PathType.StartToStop);
                    return;

                    #endregion
                }

                OpenList.Remove(Current);
                ClosedList.Add(Current);

                List <Rectangle> NeighborRectangles = ((myGrid.Children.OfType <Rectangle>()).CalculateHValue(EndPoint)).
                                                      Where(r => IsNodeAdjacent(r, Current) &&
                                                            IsNodeWalkable(r)).ToList();

                foreach (Rectangle rect in NeighborRectangles.Except(ClosedList))
                {
                    if (IsNodeDiagonal(rect, Current))
                    {
                        continue;           // Mos i merr parasysh levizjen ne diagonale
                    }
                    if ((rect.Fill as ImageBrush).ImageSource != GreenFlag.ImageSource && (rect.Fill as ImageBrush).ImageSource != RedFlag.ImageSource && (rect.Fill as ImageBrush).ImageSource != RedFlagChecked.ImageSource)
                    {
                        rect.Fill = IsGoingBack ? VioletSpace : CyanSpace;
                        AllowUIToUpdate();
                    }

                    if (!OpenList.Contains(rect))
                    {
                        OpenList.Add(rect);
                        (rect.Tag as RectangleParameter).NodeParameters.Parent = Current;
                        rect.CalculateGValue().CalculateFValue();
                    }
                    else
                    {
                        Rectangle temp = new Rectangle();
                        temp.Tag = new RectangleParameter()
                        {
                            GridPosition = new GridPosition((rect.Tag as RectangleParameter).GridPosition), NodeParameters = new NodeParameters(Current)
                        };
                        temp.CalculateGValue();

                        if ((temp.Tag as RectangleParameter).NodeParameters.G_Value < (rect.Tag as RectangleParameter).NodeParameters.G_Value)
                        {
                            (rect.Tag as RectangleParameter).NodeParameters.Parent = Current;
                            rect.CalculateFValue();
                        }
                    }
                }
            }

            #endregion

            #region "Handel 'EndPoint not found'"

            NumberOfEndPoints--;
            if (NumberOfEndPoints > 0)
            {
                if (!CheckedEndPoints.Contains(EndPoint))
                {
                    CheckedEndPoints.Add(EndPoint);
                }

                Current = StartPoint;
                ClosedList.Clear();
                ClosedList.Add(MasterStartPoint);
                OpenList.Clear();
                goto MoveNext;
            }
            else if (NumberOfEndPoints == 0 && AtLeastOnePathOpen)
            {
                if (BackPathType == BackPathType.Shortest)
                {
                    StartPoint = AccumulatedEndPointList.LastOrDefault();
                    Current    = StartPoint;
                    EndPoint   = MasterStartPoint;

                    ClosedList.Clear();
                    OpenList.Clear();
                    goto MoveNext;
                }
                else if (BackPathType == BackPathType.Reversed)
                {
                    DrawPath(MasterPath, AccumulatedEndPointList, MasterStartPoint, TinfoBox, PathType.StartToStop);
                    return;
                }
            }

            ApplyingAStarAlgorithm = false;
            TinfoBox.EndNewThreadInfoBox();
            ShowMessage(false, PathType.StartToStop);

            #endregion
        }