Exemplo n.º 1
0
        internal static void Main()
        {
            /*Grid test = new Grid(10, 10);
             * test = BinaryTree.Maze(test);
             * var output = test.ToUgly();
             * Console.WriteLine(output);*/


            //var distGrid = new DistanceGrid(10, 10);
            //BinaryTree.Maze(distGrid);
            //var start = distGrid[0, 0];
            //var distances = start.Distances;
            //distGrid.Distances = distances;
            //Console.WriteLine(distGrid);

            var longestGrid = new DistanceGrid(10, 10);

            BinaryTree.Maze(longestGrid);

            var start     = longestGrid[0, 0];
            var distances = start.Distances;

            var(newStart, distance) = distances.Max;
            var newDistances = newStart.Distances;

            var(goal, distance2)  = newDistances.Max;
            longestGrid.Distances = newDistances.PathTo(goal);

            var output = longestGrid.ToUgly(goal);

            Console.WriteLine(output);
            Console.WriteLine("The exit cell(and the farthest from the start) coordinates: x=" + goal.Column + " y=" + goal.Row);
        }
Exemplo n.º 2
0
 private void button4_Click(object sender, EventArgs e)
 {
     if (cbAlgorithm.SelectedItem != null)
     {
         Image img         = null;
         var   longestGrid = new DistanceGrid(10, 10);
         if ((string)cbAlgorithm.SelectedItem == "BinaryTree")
         {
             BinaryTree.Maze(longestGrid, (int)numericUpDown1.Value);
         }
         else if ((string)cbAlgorithm.SelectedItem == "Sidewinder")
         {
             Sidewinder.Maze(longestGrid, (int)numericUpDown1.Value);
         }
         var start     = longestGrid[0, 0];
         var distances = start.Distances;
         var(newStart, distance) = distances.Max;
         var newDistances = newStart.Distances;
         var(goal, distance2)  = newDistances.Max;
         longestGrid.Distances = newDistances.PathTo(goal);
         var        output = longestGrid.ToUgly(goal);
         TextWriter ts     = new StreamWriter(@"C:\1.txt");
         ts.Write(output);
         ts.Close();
         var robot = new Robot {
             Location = new PointBot {
                 X = 2 * newStart.Column + 1, Y = 2 * newStart.Row + 1
             }
         };
         var robot2 = new Robot {
             Location = new PointBot {
                 X = 2 * newStart.Column + 1, Y = 2 * newStart.Row + 1
             }
         };
         var robot3 = new Robot {
             Location = new PointBot {
                 X = 2 * newStart.Column + 1, Y = 2 * newStart.Row + 1
             }
         };
         var maze = Maze.Load(@"C:\1.txt");
         var ai   = new AI {
             Robot2 = robot2, Maze = maze, Robot = robot, Robot3 = robot3
         };
         var a = true;
         var b = true;
         var c = true;
         do
         {
             pictureBox1.Image = ai.ToImg(maze, robot, robot2, robot3);
             pictureBox1.Update();
             a = ai.MakeStep();
             b = ai.MakeStep2();
             c = ai.MakeStep3();
             Thread.Sleep(400);
         } while (a || b || c);
     }
 }
 public MultiPolygonShape(Vector2D[][] polygons, Scalar gridSpacing, Scalar momentOfInertiaMultiplier)
     : base(ConcatVertexes(polygons), momentOfInertiaMultiplier)
 {
     if (gridSpacing <= 0)
     {
         throw new ArgumentOutOfRangeException("gridSpacing");
     }
     this.polygons = polygons;
     this.Normals  = CalculateNormals();
     this.grid     = new DistanceGrid(this, gridSpacing);
 }
Exemplo n.º 4
0
        private void button4_Click(object sender, EventArgs e)
        {
            Image img         = null;
            var   longestGrid = new DistanceGrid(10, 10);

            // BinaryTree.Maze(longestGrid);
            Sidewinder.Maze(longestGrid);
            var start     = longestGrid[0, 0];
            var distances = start.Distances;

            var(newStart, distance) = distances.Max;
            var newDistances = newStart.Distances;

            var(goal, distance2)  = newDistances.Max;
            longestGrid.Distances = newDistances.PathTo(goal);
            var        output = longestGrid.ToUgly(goal);
            TextWriter ts     = new StreamWriter(@"C:\Users\nikit\Documents\LOLi_tex\1.txt");

            ts.Write(output);
            ts.Close();
            var robot = new Robot {
                Location = new PointBot {
                    X = 1, Y = 1
                }
            };
            var robot2 = new Robot {
                Location = new PointBot {
                    X = 1, Y = 1
                }
            };
            var robot3 = new Robot {
                Location = new PointBot {
                    X = 2 * newStart.Column + 1, Y = 2 * newStart.Row + 1
                }
            };
            var maze = Maze.Load(@"C:\Users\nikit\Documents\LOLi_tex\1.txt");
            var ai   = new AI {
                Robot2 = robot2, Maze = maze, Robot = robot, Robot3 = robot3
            };
            var a = true;
            var b = true;
            var c = true;

            do
            {
                pB.Image = ai.ToImg(maze, robot, robot2, robot3);
                a        = ai.MakeStep();
                b        = ai.MakeStep2();
                c        = ai.MakeStep3();
                Thread.Sleep(1500);
            } while (a || b || c);
            //timer1.Enabled = true;
        }
Exemplo n.º 5
0
 /// <summary>
 /// Creates a new Polygon Instance.
 /// </summary>
 /// <param name="vertexes">the vertexes that make up the shape of the Polygon</param>
 /// <param name="gridSpacing">
 /// How large a grid cell is. Usualy you will want at least 2 cells between major vertexes.
 /// The smaller this is the better precision you get, but higher cost in memory. 
 /// The larger the less precision and if it's to high collision detection may fail completely.
 public PolygonShape(Vector2D[] vertexes, Scalar gridSpacing)
 {
     if (vertexes == null) { throw new ArgumentNullException("vertexes"); }
     if (vertexes.Length < 3) { throw new ArgumentException("too few", "vertexes"); }
     if (gridSpacing <= 0) { throw new ArgumentOutOfRangeException("gridSpacing"); }
     this.vertexes = vertexes;
     this.grid = new DistanceGrid(this, gridSpacing);
     this.vertexNormals = VertexHelper.GetVertexNormals(this.vertexes);
     VertexInfo info = VertexHelper.GetVertexInfo(vertexes);
     this.inertia = info.Inertia;
     this.centroid = info.Centroid;
     this.area = info.Area;
 }
Exemplo n.º 6
0
        public static void Main(string[] args)
        {
            var longestGrid = new DistanceGrid(10, 10);

            // BinaryTree.Maze(longestGrid);
            Sidewinder.Maze(longestGrid);
            var start     = longestGrid[0, 0];
            var distances = start.Distances;

            var(newStart, distance) = distances.Max;
            var newDistances = newStart.Distances;

            var(goal, distance2)  = newDistances.Max;
            longestGrid.Distances = newDistances.PathTo(goal);

            var output = longestGrid.ToUgly(goal);

            Console.WriteLine(output);
            Console.WriteLine("The exit cell(and the farthest from the start) coordinates: x=" + goal.Column + " y=" + goal.Row);

            TextWriter ts = new StreamWriter(@"C:\1.txt");

            ts.Write(output);
            ts.Close();
            var robot = new Robot {
                Location = new Point {
                    X = newStart.Column, Y = newStart.Row
                }
            };
            var robot2 = new Robot2 {
                Location = new Point {
                    X = newStart.Column, Y = newStart.Row
                }
            };
            var robot3 = new Robot3 {
                Location = new Point {
                    X = newStart.Column, Y = newStart.Row
                }
            };
            var maze = Maze.Load(@"C:\1.txt");
            var ai   = new AI {
                Robot2 = robot2, Maze = maze, Robot = robot, Robot3 = robot3
            };

            ai.StartBot(ai);
        }
Exemplo n.º 7
0
 /// <summary>
 /// Creates a new Polygon Instance.
 /// </summary>
 /// <param name="vertexes">the vertexes that make up the shape of the Polygon</param>
 /// <param name="gridSpacing">
 /// How large a grid cell is. Usualy you will want at least 2 cells between major vertexes.
 /// The smaller this is the better precision you get, but higher cost in memory.
 /// The larger the less precision and if it's to high collision detection may fail completely.
 /// </param>
 /// <param name="momentOfInertiaMultiplier">
 /// How hard it is to turn the shape. Depending on the construtor in the
 /// Body this will be multiplied with the mass to determine the moment of inertia.
 /// </param>
 public PolygonShape(Vector2D[] vertexes, Scalar gridSpacing, Scalar momentOfInertiaMultiplier)
     : base(vertexes, momentOfInertiaMultiplier)
 {
     if (vertexes == null)
     {
         throw new ArgumentNullException("vertexes");
     }
     if (vertexes.Length < 3)
     {
         throw new ArgumentException("too few", "vertexes");
     }
     if (gridSpacing <= 0)
     {
         throw new ArgumentOutOfRangeException("gridSpacing");
     }
     this.Normals = ShapeHelper.CalculateNormals(this.Vertexes);
     this.grid    = new DistanceGrid(this, gridSpacing);
 }
Exemplo n.º 8
0
        static void Main(string[] args)
        {
            IGrid grid = new DistanceGrid(5, 5);
            new BinaryTree().Run(ref grid);

            var start = grid[0, 0];
            var distances = start.Distances;

            var distanceGrid = (DistanceGrid)grid; //casting to a distance grid to take advantage of the "distances" property
            distanceGrid.CellDistances = distances;
            Console.WriteLine(grid.ToString(false));

            Console.WriteLine("Path from NW corner to SW corner:");
            distanceGrid.CellDistances = distances.PathToGoal(grid[grid.Rows-1, 0]);
            Console.WriteLine(grid.ToString(false));

            Console.ReadKey();
        }
Exemplo n.º 9
0
        static void Main(string[] args)
        {
            IGrid grid = new DistanceGrid(5, 5);

            new BinaryTree().Run(ref grid);

            var start     = grid[0, 0];
            var distances = start.Distances;

            var distanceGrid = (DistanceGrid)grid; //casting to a distance grid to take advantage of the "distances" property

            distanceGrid.CellDistances = distances;
            Console.WriteLine(grid.ToString(false));

            Console.WriteLine("Path from NW corner to SW corner:");
            distanceGrid.CellDistances = distances.PathToGoal(grid[grid.Rows - 1, 0]);
            Console.WriteLine(grid.ToString(false));

            Console.ReadKey();
        }
Exemplo n.º 10
0
        static void Main(string[] args)
        {
            IGrid grid = new DistanceGrid(5, 5);
            new BinaryTree().Run(ref grid);

            var start = grid[0, 0];
            Distances distances = start.Distances;

            var newStart = distances.Max.Cell;
            var distance = distances.Max.Distance;

            var newDistances = newStart.Distances;
            MaxResult result = newDistances.Max;

            DistanceGrid distanceGrid = (DistanceGrid)grid;
            distanceGrid.CellDistances = newDistances.PathToGoal(result.Cell);

            Console.WriteLine(distanceGrid.ToString(false));

            Console.ReadKey();
        }
        //GenerateConsolePath method applys generation to grid, finds longest path between and outputs result to console.
        public static void GenerateConsolePath(IGenerator _generator, int _width, int _height, out DistanceGrid _grid)
        {
            //Create a grid.
            DistanceGrid grid = new DistanceGrid(_width, _height);

            //Apply maze algorithm.
            _generator.Generate(grid);

            //Find the distances.
            grid.FindDistances(grid[0, 0]);

            //Find the longest path.
            Cell startingCell = grid.FindLongestPath();

            grid.distances = grid.distances.PathTo(startingCell);

            //Output result to console.
            MazeDisplay.ASCIIDisplay(grid);

            //Output the grid.
            _grid = grid;
        }
Exemplo n.º 12
0
        /// <summary>
        /// Creates a new Polygon Instance.
        /// </summary>
        /// <param name="vertexes">the vertexes that make up the shape of the Polygon</param>
        /// <param name="gridSpacing">
        /// How large a grid cell is. Usualy you will want at least 2 cells between major vertexes.
        /// The smaller this is the better precision you get, but higher cost in memory.
        /// The larger the less precision and if it's to high collision detection may fail completely.
        public PolygonShape(Vector2D[] vertexes, Scalar gridSpacing)
        {
            if (vertexes == null)
            {
                throw new ArgumentNullException("vertexes");
            }
            if (vertexes.Length < 3)
            {
                throw new ArgumentException("too few", "vertexes");
            }
            if (gridSpacing <= 0)
            {
                throw new ArgumentOutOfRangeException("gridSpacing");
            }
            this.vertexes      = vertexes;
            this.grid          = new DistanceGrid(this, gridSpacing);
            this.vertexNormals = VertexHelper.GetVertexNormals(this.vertexes);
            VertexInfo info = VertexHelper.GetVertexInfo(vertexes);

            this.inertia  = info.Inertia;
            this.centroid = info.Centroid;
            this.area     = info.Area;
        }
Exemplo n.º 13
0
        static void Main(string[] args)
        {
            IGrid grid = new DistanceGrid(5, 5);

            new BinaryTree().Run(ref grid);

            var       start     = grid[0, 0];
            Distances distances = start.Distances;

            var newStart = distances.Max.Cell;
            var distance = distances.Max.Distance;

            var       newDistances = newStart.Distances;
            MaxResult result       = newDistances.Max;

            DistanceGrid distanceGrid = (DistanceGrid)grid;

            distanceGrid.CellDistances = newDistances.PathToGoal(result.Cell);

            Console.WriteLine(distanceGrid.ToString(false));

            Console.ReadKey();
        }
        //GenerateColourPNG method applys generation to grid, finds longest path and outputs result to coloured png.
        public static void GenerateColourImage(IGenerator _generator, int _width, int _height, string _path, IImageEncoder _imageEncoder, out DistanceGrid _grid)
        {
            //Create a grid.
            DistanceGrid grid = new DistanceGrid(_width, _height);

            //Apply maze algorithm.
            _generator.Generate(grid);

            //Find the distances.
            grid.FindDistances(grid[0, 0]);

            //Find the longest path.
            Cell startingCell = grid.FindLongestPath();

            grid.distances = grid.distances.PathTo(startingCell);
            grid.FindLongestPath();

            //Write result to file.
            MazeDisplay.ToImage(40, grid, RENDER_MODE.COLOUR, _path, _imageEncoder);

            //Output the grid.
            _grid = grid;
        }
Exemplo n.º 15
0
        public MultiPolygonShape(Vector2D[][] polygons, Scalar gridSpacing)
        {
            if (gridSpacing <= 0)
            {
                throw new ArgumentOutOfRangeException("gridSpacing");
            }
            if (polygons == null)
            {
                throw new ArgumentNullException("polygons");
            }
            if (polygons.Length == 0)
            {
                throw new ArgumentOutOfRangeException("polygons");
            }
            this.polygons      = polygons;
            this.vertexes      = ConcatVertexes(polygons);
            this.vertexNormals = CalculateNormals();
            this.grid          = new DistanceGrid(this, gridSpacing);
            VertexInfo info = VertexHelper.GetVertexInfoOfRange(polygons);

            this.inertia  = info.Inertia;
            this.centroid = info.Centroid;
            this.area     = info.Area;
        }
Exemplo n.º 16
0
 public MultiPolygonShape(Vector2D[][] polygons, Scalar gridSpacing)
 {
     if (gridSpacing <= 0) { throw new ArgumentOutOfRangeException("gridSpacing"); }
     if (polygons == null) { throw new ArgumentNullException("polygons"); }
     if (polygons.Length == 0) { throw new ArgumentOutOfRangeException("polygons"); }
     this.polygons = polygons;
     this.vertexes = ConcatVertexes(polygons);
     this.vertexNormals = CalculateNormals();
     this.grid = new DistanceGrid(this, gridSpacing);
     VertexInfo info = VertexHelper.GetVertexInfoOfRange(polygons);
     this.inertia = info.Inertia;
     this.centroid = info.Centroid;
     this.area = info.Area;
 }