Esempio n. 1
0
        private void button1_Click(object sender, EventArgs e)
        {
            var runs = (int)numericUpDown1.Value;


            var search    = new AStarSearch(_grid);
            var timer     = new Stopwatch();
            var times     = new List <long>();
            var stepTimes = new List <double>();

            for (var runIndex = 0; runIndex < runs; runIndex++)
            {
                var start  = new Vector2Int((int)numericUpDown2.Value, (int)numericUpDown3.Value);
                var finish = new Vector2Int((int)numericUpDown4.Value, (int)numericUpDown5.Value);

                timer.Start();

                var greedyPath = search.GreedyFind(start, finish);
                var path       = search.Find(start, finish);

                timer.Stop();

                if (!greedyPath.Any())
                {
                    throw new Exception("Any");
                }
                if (!greedyPath.Last().Location.Equals(finish))
                {
                    finish = greedyPath.Last().Location;
                }
                if (!greedyPath.First().Location.Equals(start))
                {
                    throw new Exception("Start");
                }
                RenderPath(_width, _height, start, finish, greedyPath, path, search, _grid, runIndex, timer.Elapsed);

                times.Add(timer.ElapsedMilliseconds);
                stepTimes.Add((double)timer.ElapsedMilliseconds / greedyPath.Length);

                timer.Reset();
            }
        }
Esempio n. 2
0
        private void button4_Click(object sender, EventArgs e)
        {
            panel1.MaximumSize = panel1.Size;

            var runs = 1;

            var image = (Bitmap)Image.FromFile("cavern.gif");

            var width  = image.Width;
            var height = image.Height;

            _grid = new Grid(width, height);

            if (!Directory.Exists("Paths"))
            {
                Directory.CreateDirectory("Paths");
            }

            var timer = new Stopwatch();

            var times     = new List <long>();
            var stepTimes = new List <double>();

            var search = new AStarSearch(_grid);

            for (var runIndex = 0; runIndex < runs; runIndex++)
            {
                if (numericUpDown2.Value > _grid.Width)
                {
                    numericUpDown2.Value = _grid.Width - 10;
                }

                if (numericUpDown3.Value > _grid.Width)
                {
                    numericUpDown3.Value = _grid.Height - 10;
                }

                if (numericUpDown4.Value > _grid.Width)
                {
                    numericUpDown4.Value = _grid.Width - 10;
                }

                if (numericUpDown5.Value > _grid.Width)
                {
                    numericUpDown5.Value = _grid.Height - 10;
                }

                var start  = new Vector2Int((int)numericUpDown2.Value, (int)numericUpDown3.Value);
                var finish = new Vector2Int((int)numericUpDown4.Value, (int)numericUpDown5.Value);

                for (var x = 0; x < width; x++)
                {
                    for (var y = 0; y < height; y++)
                    {
                        _grid[x, y].Blocked = (image.GetPixel(x, y).R + image.GetPixel(x, y).G + image.GetPixel(x, y).B) < 120;
                    }
                }

                timer.Start();

                var greedyPath = search.GreedyFind(start, finish);

                var path = search.Find(start, finish);

                timer.Stop();

                if (!greedyPath.Any() || !greedyPath.Last().Location.Equals(finish) || !greedyPath.First().Location.Equals(start))
                {
                    MessageBox.Show(
                        "Скорее всего вы указали стену, алгоритм не смог найти заданную точку",
                        "Введите новое значение",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Error,
                        MessageBoxDefaultButton.Button1,
                        MessageBoxOptions.DefaultDesktopOnly);
                }
                else
                {
                    RenderPath(width, height, start, finish, greedyPath, path, search, _grid, runIndex, timer.Elapsed); //greedy

                    times.Add(timer.ElapsedMilliseconds);
                    stepTimes.Add((double)timer.ElapsedMilliseconds / greedyPath.Length);

                    timer.Reset();
                }
            }
            if (times.Count > 0)
            {
                Console.WriteLine("Elapsed: Avg: {0:000}ms Min: {1:000}ms Max: {2:000}ms StepAvg:{3:0.000000}ms",
                                  times.Average(), times.Min(), times.Max(), stepTimes.Average());
            }
        }