Ejemplo n.º 1
0
        private bool GetNextSquareDepth(IntersectionMazeImage maze, IntersectionPoint currentSquare, out IntersectionPoint nextSquare)
        {
            IntersectionPoint nextLocation = null;

            nextSquare = null;
            IntersectionPoint intersection;

            for (var i = 0; i < currentSquare.ConnectedIntersections.Count; i++)
            {
                intersection = currentSquare.ConnectedIntersections[i];
                if (!intersection.HasVisited)
                {
                    nextLocation = intersection;
                    break;
                }
            }
            if (nextLocation != null)
            {
                if (maze.EndPoint.Equals(nextLocation))
                {
                    nextSquare = maze.EndPoint;
                    return(true);
                }
                nextSquare = nextLocation;
                return(true);
            }
            return(false);
        }
Ejemplo n.º 2
0
 private bool CheckAllVisitedSquares(IntersectionMazeImage maze)
 {
     if (maze.TotalVisited < maze.Points.Count())
     {
         return(false);
     }
     if (!maze.EndPoint.HasVisited)
     {
         return(false);
     }
     return(true);
 }
Ejemplo n.º 3
0
        //public bool IntersectionSolve(AStarIntersectionMazeImage maze, out Stack<AStarIntersectionPoint> route)
        //{
        //    bool hasFoundRoute = false;
        //    bool hasSeenAllSquares = false;
        //    AStarIntersectionPoint currentSquare = maze.StartPoint;
        //    bool[,] visitedSquares = new bool[maze.MazeHeight, maze.MazeWidth];
        //    Stack<AStarIntersectionPoint> currentRoute = new Stack<AStarIntersectionPoint>();
        //    currentRoute.Push(maze.StartPoint);
        //    int backtrackCount = 0;
        //    List<long> getNextTimes = new List<long>();
        //    Stopwatch s = new Stopwatch();
        //    while (!hasFoundRoute && !hasSeenAllSquares)
        //    {
        //        s.Start();
        //        bool hasNextSquare = GetNextSquareAStar(maze, currentSquare, visitedSquares, out AStarIntersectionPoint nextSquare);
        //        s.Stop();
        //        getNextTimes.Add(s.ElapsedMilliseconds);
        //        s.Reset();
        //        if (hasNextSquare)
        //        {
        //            if (currentRoute.Count > 0 && !currentRoute.First().Equals(currentSquare))
        //            {
        //                currentRoute.Push(currentSquare);
        //            }
        //            currentRoute.Push(nextSquare);
        //            visitedSquares[nextSquare.Point.Item1, nextSquare.Point.Item2] = true;
        //            currentSquare = nextSquare;
        //            if (nextSquare.Equals(maze.EndPoint))
        //            {
        //                hasFoundRoute = true;
        //            }
        //        }
        //        else
        //        {
        //            hasSeenAllSquares = CheckAllVisitedSquares(visitedSquares, maze);
        //            currentSquare = currentRoute.Pop();
        //            backtrackCount++;
        //        }
        //    }
        //    Console.WriteLine("AStarIntersection Backtracks: " + backtrackCount);
        //    Console.WriteLine("AStarIntersection Average Get Speed " + getNextTimes.Average() + "ms");
        //    Console.WriteLine("AStarIntersection Get Count " + getNextTimes.Count);
        //    if (hasFoundRoute)
        //    {
        //        route = currentRoute;
        //        return true;
        //    }
        //    route = null;
        //    return true;
        //}
        public bool IntersectionSolve(IntersectionMazeImage maze, out Stack <IntersectionPoint> route)
        {
            var hasFoundRoute     = false;
            var hasSeenAllSquares = false;

            maze.StartPoint.HasVisited = true;
            var currentSquare = maze.StartPoint;
            var currentRoute  = new Stack <IntersectionPoint>();

            currentRoute.Push(maze.StartPoint);
            while (!hasFoundRoute && !hasSeenAllSquares)
            {
                var hasNextSquare = GetNextSquareDepth(maze, currentSquare, out IntersectionPoint nextSquare);
                if (hasNextSquare)
                {
                    if (currentRoute.Count > 0 && !currentRoute.First().Equals(currentSquare))
                    {
                        currentRoute.Push(currentSquare);
                    }
                    currentRoute.Push(nextSquare);
                    maze.TotalVisited++;
                    nextSquare.HasVisited = true;
                    currentSquare         = nextSquare;
                    if (nextSquare.Equals(maze.EndPoint))
                    {
                        maze.EndPoint.HasVisited = true;
                        hasFoundRoute            = true;
                    }
                }
                else
                {
                    hasSeenAllSquares = CheckAllVisitedSquares(maze);
                    currentSquare     = currentRoute.Pop();
                    maze.TotalVisited--;
                }
            }
            if (hasFoundRoute)
            {
                route = currentRoute;
                return(true);
            }
            route = null;
            return(false);
        }
Ejemplo n.º 4
0
        public IntersectionMazeImage CreateIntersectionMaze(string filePath)
        {
            var         img         = new Bitmap(filePath);
            PixelFormat pixelFormat = img.PixelFormat;
            // Lock the bitmap's bits.
            var        rect    = new Rectangle(0, 0, img.Width, img.Height);
            BitmapData bmpData = img.LockBits(rect, ImageLockMode.ReadWrite, pixelFormat);

            var newMaze               = new IntersectionMazeImage();
            var hasSeenWallInJ        = false;
            var hasSeenWallInI        = new bool[img.Width];
            var previousIIntersection = new IntersectionPoint[img.Width];
            var previousJIntersection = new IntersectionPoint();
            var newMazePoints         = new List <IntersectionPoint>();
            var hasSetStartConnector  = false;
            var lastSeenEndConnector  = new IntersectionPoint();
            IntersectionPoint currentPoint;
            // Get the address of the first line.
            IntPtr ptr = bmpData.Scan0;

            // Declare an array to hold the bytes of the bitmap.
            int bytes     = Math.Abs(bmpData.Stride) * img.Height;
            var rgbValues = new byte[bytes];

            // Copy the RGB values into the array.
            System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);


            for (var index = 0; index < img.Width; index++)
            {
                if (BitmapReader.IsPixel(index, 0, ref rgbValues, pixelFormat, bmpData.Stride))
                {
                    newMaze.StartPoint = new IntersectionPoint()
                    {
                        ICoord = index,
                        JCoord = 0
                    };
                }
                if (BitmapReader.IsPixel(index, img.Height - 1, ref rgbValues, pixelFormat, bmpData.Stride))
                {
                    newMaze.EndPoint = new IntersectionPoint()
                    {
                        ICoord = index,
                        JCoord = img.Height - 1
                    };
                }
                if (newMaze.StartPoint != null && newMaze.EndPoint != null)
                {
                    break;
                }
            }

            if (newMaze.StartPoint == null || newMaze.EndPoint == null)
            {
                throw new Exception("Could not find start and endpoint");
            }
            var i          = 1;
            var j          = 0;
            var height     = img.Height;
            var width      = img.Width - 1;
            var hasSquares = true;

            while (hasSquares)
            {
                if (BitmapReader.IsPixel(i, j, ref rgbValues, pixelFormat, bmpData.Stride))
                {
                    if (IsIntersection(i, j, ref rgbValues, pixelFormat, bmpData.Stride, img.Width, img.Height))
                    {
                        currentPoint = new IntersectionPoint()
                        {
                            HasVisited = false,
                            ICoord     = i,
                            JCoord     = j
                        };
                        if (!hasSetStartConnector && i == newMaze.StartPoint.ICoord)
                        {
                            hasSetStartConnector = true;
                            newMaze.StartPoint.ConnectedIntersections.Add(currentPoint);
                            currentPoint.ConnectedIntersections.Add(newMaze.StartPoint);
                        }
                        if (i == newMaze.EndPoint.ICoord)
                        {
                            lastSeenEndConnector = currentPoint;
                        }
                        if (!hasSeenWallInJ)
                        {
                            currentPoint.ConnectedIntersections.Add(previousJIntersection);
                            previousJIntersection.ConnectedIntersections.Add(currentPoint);
                        }
                        if (!hasSeenWallInI[j] && previousIIntersection[j] != null)
                        {
                            currentPoint.ConnectedIntersections.Add(previousIIntersection[j]);
                            previousIIntersection[j].ConnectedIntersections.Add(currentPoint);
                        }
                        newMazePoints.Add(currentPoint);
                        hasSeenWallInJ           = false;
                        hasSeenWallInI[j]        = false;
                        previousJIntersection    = currentPoint;
                        previousIIntersection[j] = currentPoint;
                    }
                }
                else
                {
                    hasSeenWallInJ    = true;
                    hasSeenWallInI[j] = true;
                }
                j++;
                if (j >= height)
                {
                    i++;
                    j = 0;
                }
                if (i >= width)
                {
                    hasSquares = false;
                }
            }
            lastSeenEndConnector.ConnectedIntersections.Add(newMaze.EndPoint);
            newMaze.EndPoint.ConnectedIntersections.Add(lastSeenEndConnector);
            newMaze.Points = newMazePoints.ToArray();
            return(newMaze);
        }