Example #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);
        }
        public void CreateSolutionImage(Stack <IntersectionPoint> solution, string mazeFile)
        {
            var img = new Bitmap(mazeFile);
            IntersectionPoint previous = null;

            while (solution.Count > 0)
            {
                var step = solution.Pop();
                img.SetPixel(step.ICoord, step.JCoord, Color.Yellow);
                if (previous != null)
                {
                    if (previous.ICoord != step.ICoord)
                    {
                        if (previous.ICoord > step.ICoord)
                        {
                            for (var i = step.ICoord + 1; i < previous.ICoord; i++)
                            {
                                img.SetPixel(i, step.JCoord, Color.Green);
                            }
                        }
                        else if (previous.ICoord < step.ICoord)
                        {
                            for (var i = previous.ICoord + 1; i < step.ICoord; i++)
                            {
                                img.SetPixel(i, step.JCoord, Color.Green);
                            }
                        }
                    }
                    else if (previous.JCoord != step.JCoord)
                    {
                        if (previous.JCoord > step.JCoord)
                        {
                            for (var j = step.JCoord + 1; j < previous.JCoord; j++)
                            {
                                img.SetPixel(step.ICoord, j, Color.Green);
                            }
                        }
                        else if (previous.JCoord < step.JCoord)
                        {
                            for (var j = previous.JCoord + 1; j < step.JCoord; j++)
                            {
                                img.SetPixel(step.ICoord, j, Color.Green);
                            }
                        }
                    }
                }
                previous = step;
            }
            img.Save("solution-intersection.png");
        }
Example #3
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);
        }