Example #1
0
 private int heuritic(Spot neighbor, Spot end)
 {
     return((int)Math.Sqrt(Math.Pow(Math.Abs(end.I - neighbor.I), 2) + Math.Pow(Math.Abs(end.J - neighbor.J), 2)));
 }
Example #2
0
        private void Bw_DoWork(object sender, DoWorkEventArgs e)
        {
            while (_storage.Loop)
            {
                Thread.Sleep(1000 / _storage.Framerate);
                Application.Current.Dispatcher.Invoke((Action)(() =>
                {
                    Spot current = null;
                    if (_storage.OpenSet.Count > 0)
                    {
                        int winner = 0;
                        for (int i = 0; i < _storage.OpenSet.Count; i++)
                        {
                            if (_storage.OpenSet.ElementAt(i).F < _storage.OpenSet.ElementAt(winner).F)
                            {
                                winner = i;
                            }
                        }

                        current = _storage.OpenSet.ElementAt(winner);

                        if (current == _storage.End)
                        {
                            if (!_storage.Path.Contains(current))
                            {
                                _storage.Path.Enqueue(current);
                            }
                            _storage.Loop = false;
                        }

                        _storage.OpenSet.Remove(current);
                        _storage.ClosedSet.Add(current);

                        List <Spot> neighbors = current.Neighbors;
                        for (int i = 0; i < neighbors.Count; i++)
                        {
                            Spot neighbor = neighbors.ElementAt(i);
                            if (!_storage.ClosedSet.Contains(neighbor) && !neighbor.Wall)
                            {
                                int tempG = current.G + 1;

                                bool newPath = false;
                                if (_storage.OpenSet.Contains(neighbor))
                                {
                                    if (tempG < neighbor.G)
                                    {
                                        neighbor.G = tempG;
                                        newPath = true;
                                    }
                                }
                                else
                                {
                                    neighbor.G = tempG;
                                    newPath = true;
                                    _storage.OpenSet.Add(neighbor);
                                }

                                if (newPath)
                                {
                                    neighbor.H = heuritic(neighbor, _storage.End);
                                    neighbor.F = neighbor.G + neighbor.H;
                                    neighbor.Previous = current;
                                }
                            }
                        }
                    }
                    else
                    {
                        Console.WriteLine("No solution!");
                        txtNoSolution.Visibility = Visibility.Visible;
                        _storage.Loop = false;
                    }

                    for (int i = 0; i < _storage.Cols; i++)
                    {
                        for (int j = 0; j < _storage.Rows; j++)
                        {
                            _storage.Grid[i, j].show(Storage.WHITE);
                        }
                    }

                    for (int i = 0; i < _storage.ClosedSet.Count; i++)
                    {
                        _storage.ClosedSet.ElementAt(i).show(Storage.RED);
                    }

                    for (int i = 0; i < _storage.OpenSet.Count; i++)
                    {
                        _storage.OpenSet.ElementAt(i).show(Storage.GREEN);
                    }

                    Spot temp = current;
                    if (temp != null)
                    {
                        _storage.Path.Clear();
                        _storage.Path.Enqueue(temp);
                        while (temp.Previous != null)
                        {
                            temp.show(Storage.BLUE);
                            _storage.Path.Enqueue(temp.Previous);
                            temp = temp.Previous;
                        }
                    }

                    for (int i = 0; i < _storage.Path.Count; i++)
                    {
                        _storage.Path.ElementAt(i).show(Storage.TRANSPARENT);
                    }


                    for (int i = 0; i < _storage.Lines.Count; i++)
                    {
                        mainCanvas.Children.Remove(_storage.Lines.ElementAt(i));
                    }

                    for (int i = 0; i < mainCanvas.Children.Count; i++)
                    {
                        if (mainCanvas.Children[i] is Line)
                        {
                            mainCanvas.Children.RemoveAt(i);
                        }
                    }

                    Thread.Sleep(5);
                    _storage.Lines.Clear();
                    for (int i = 0; i < _storage.Path.Count; i++)
                    {
                        if (i < _storage.Path.Count - 1)
                        {
                            Line line = new Line()
                            {
                                X1 = _storage.Path.ElementAt(i).I *_storage.WidthOfOneSpot + _storage.WidthOfOneSpot / 2,
                                Y1 = _storage.Path.ElementAt(i).J *_storage.HeigthOfOneSpot + _storage.HeigthOfOneSpot / 2,
                                X2 = _storage.Path.ElementAt(i + 1).I *_storage.WidthOfOneSpot + _storage.WidthOfOneSpot / 2,
                                Y2 = _storage.Path.ElementAt(i + 1).J *_storage.HeigthOfOneSpot + _storage.HeigthOfOneSpot / 2,
                                Stroke = Storage.PURPLE,
                                StrokeThickness = _storage.LineThickness,
                                StrokeStartLineCap = PenLineCap.Round,
                                StrokeEndLineCap = PenLineCap.Round
                            };
                            _storage.Lines.Add(line);
                            mainCanvas.Children.Add(line);
                            Console.WriteLine("i = " + i + " | " + line.X1 + ", " + line.Y1 + "   ->    " + line.X2 + ", " + line.Y2 + " | ");
                        }
                    }
                    Console.WriteLine(); Console.WriteLine();
                }));
            }
        }