Exemplo n.º 1
0
        private void SaveMazeAsImagePath32Bit(String fileName, ImageFormat imageFormat, List <MazePoint> path)
        {
            /*
             * Are you 32 bit or 64 bit? Generally images use a single block of contiguous memory.
             * Getting 1 gig in 32 bit block of contiguous memory can be a challenge. – TNT 1 hour ago
             *
             *@user1149816: See for example this page. The basic trick is to call Bitmap.LockBits
             * to get a BitmapData, which can then be accessed as a pointer to raw data (using either
             * an IntPtr or a byte*, the latter of which tends to be much faster). – Brian 44 mins ago
             */
            int cursize = 1;

            Bitmap   objBmpImage = new Bitmap(cursize * (Width - 1), cursize * (Height - 1), PixelFormat.Format32bppArgb);
            Graphics objGraphics = Graphics.FromImage(objBmpImage);

            // Add the colors to the new bitmap.
            objGraphics = Graphics.FromImage(objBmpImage);

            // Set Background color
            objGraphics.Clear(Color.Black);
            objGraphics.SmoothingMode = SmoothingMode.None;
            //objGraphics.TextRenderingHint = TextRenderingHint.AntiAlias;
            //objGraphics.DrawString(sImageText, objFont, new SolidBrush(Color.FromArgb(102, 102, 102)), 0, 0);

            for (int y = 0; y < Height - 1; y++)
            {
                for (int x = 0; x < Width - 1; x++)
                {
                    if (innerMap[x, y] == true)
                    {
                        objGraphics.FillRegion(Brushes.White, new Region(new Rectangle(x * cursize, y * cursize, cursize, cursize)));
                    }
                }
            }

            for (int i = 0; i < path.Count; i++)
            {
                MazePoint n = path[i];

                int formulathing = (int)((double)i / (double)path.Count * 255.0);

                //rgb
                Color brushColor = Color.FromArgb(formulathing, 255 - formulathing, 0);

                objGraphics.FillRegion(new SolidBrush(brushColor), new Region(new Rectangle(n.X * cursize, n.Y * cursize, cursize, cursize)));
            }


            objGraphics.Flush();
            objGraphics.Dispose();

            objBmpImage.Save(fileName, imageFormat);
        }
Exemplo n.º 2
0
        private void SaveMazeAsImagePath4Bit(String fileName, ImageFormat imageFormat, List <MazePoint> path)
        {
            using (Bitmap objBmpImage = new Bitmap(innerMap.Width - 1, innerMap.Height - 1, PixelFormat.Format4bppIndexed))
            {
                Rectangle rect = new Rectangle(0, 0, objBmpImage.Width, objBmpImage.Height);
                System.Drawing.Imaging.BitmapData bmpData =
                    objBmpImage.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
                                         objBmpImage.PixelFormat);

                IntPtr ptr = bmpData.Scan0;

                int    bytes     = Math.Abs(bmpData.Stride) * objBmpImage.Height;
                byte[] rgbValues = new byte[bytes];

                System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);

                //BitArreintjeFast[] pathding = new BitArreintjeFast[width];
                //for (int x = 0; x < width; x++)
                //{
                //    pathding[x] = new BitArreintjeFast(height);
                //    for (int y = 0; y < height; y++)
                //    {
                //        pathding[x][y] = false;
                //    }
                //}

                //foreach (MazePoint p in path)
                //{
                //    pathding[p.X][p.Y] = true;
                //}

                for (int y = 0; y < innerMap.Height - 1; y++)
                {
                    int counterdeluxe = bmpData.Stride * y;
                    int x             = 0;
                    for (int i = 0; i < bmpData.Stride; i++)
                    {
                        if (x > objBmpImage.Width)
                        {
                            break;
                        }

                        BitArray bitar = new BitArray(8);

                        for (int j = 4; j >= 0; j = j - 4)
                        {
                            if (innerMap[x, y])
                            {
                                bitar[j + 3] = true;
                                bitar[j + 2] = true;
                                bitar[j + 1] = true;
                                bitar[j + 0] = true;
                            }
                            else
                            {
                                bitar[j + 3] = false;
                                bitar[j + 2] = false;
                                bitar[j + 1] = false;
                                bitar[j + 0] = false;
                            }
                            x++;
                        }
                        rgbValues[counterdeluxe] = (byte)GetIntFromBitArray(bitar);
                        counterdeluxe++;
                    }
                }

                for (int i = 0; i < path.Count; i++)
                {
                    long      percent = 100L * (long)(i + 1) / (long)path.Count;
                    MazePoint point   = path[i];
                    int       xrest   = point.X % 2;

                    int pos = bmpData.Stride * point.Y + point.X / 2;
                    //Console.WriteLine(pos);
                    BitArray bitar = GetBitArrayFromByte(rgbValues[pos]);

                    int xtra = 4;

                    if (xrest >= 1)
                    {
                        xtra = 0;
                    }

                    if (percent < 20)
                    {
                        bitar[xtra + 3] = true;
                        bitar[xtra + 2] = false;
                        bitar[xtra + 1] = true;
                        bitar[xtra + 0] = false;
                    }
                    else if (percent < 40)
                    {
                        bitar[xtra + 3] = false;
                        bitar[xtra + 2] = false;
                        bitar[xtra + 1] = true;
                        bitar[xtra + 0] = false;
                    }
                    else if (percent < 60)
                    {
                        bitar[xtra + 3] = false;
                        bitar[xtra + 2] = false;
                        bitar[xtra + 1] = true;
                        bitar[xtra + 0] = true;
                    }
                    else if (percent < 80)
                    {
                        bitar[xtra + 3] = false;
                        bitar[xtra + 2] = false;
                        bitar[xtra + 1] = false;
                        bitar[xtra + 0] = true;
                    }
                    else
                    {
                        bitar[xtra + 3] = true;
                        bitar[xtra + 2] = false;
                        bitar[xtra + 1] = false;
                        bitar[xtra + 0] = true;
                    }


                    rgbValues[pos] = (byte)GetIntFromBitArray(bitar);
                }

                System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes);

                objBmpImage.UnlockBits(bmpData);

                objBmpImage.Save(fileName, imageFormat);
            }

            //return (objBmpImage);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Finds the path between the start and the endpoint in a maze
        /// </summary>
        /// <param name="start">The start point</param>
        /// <param name="end">The end point</param>
        /// <param name="map">The maze.InnerMap</param>
        /// <param name="callBack">The callback that can be used to see what the pathfinder is doing (or null), the boolean true = a new path find thingy or false when it determined that path is not correct</param>
        /// <returns>The shortest path in a list of points</returns>
        public static List <MazePoint> GoFind(MazePoint start, MazePoint end, InnerMap map, Action <int, int, Boolean> callBack)
        {
            if (callBack == null)
            {
                callBack = (x, y, z) => { };
            }


            //Callback won't work nice with this since it will find its path from back to front
            //Swap them so we don't have to reverse at the end ;)
            //MazePoint temp = start;
            //start = end;
            //end = temp;



            int width  = map.Width;
            int height = map.Height;

            InnerMap visitedMap = new BitArreintjeFastInnerMap(width, height);

            List <MazePoint> pointlist = new List <MazePoint>();



            //@todo Controleer dit
            InnerMap visited = new BitArreintjeFastInnerMap(width, height);

            for (int x = 0; x < width; x++)
            {
                //visited[x] = new BitArreintjeFast(height);
                for (int y = 0; y < height; y++)
                {
                    if (x == 0 || y == 0 || x == width || y == height)
                    {
                        visited[x, y] = true;
                    }
                    //else
                    //{
                    //    visited[x][y] = false;
                    //}
                }
            }


            //Hier begint het gedoe
            Stack <MazePoint> stackje = new Stack <MazePoint>();

            stackje.Push(start);
            visited[start.X, start.Y] = true;
            callBack.Invoke(start.X, start.Y, true);
            //form.pixelDraw(x, y, Brushes.White);
            while (stackje.Count != 0)
            {
                MazePoint cur = stackje.Peek();
                int       x   = cur.X;
                int       y   = cur.Y;

                if (end.X == x && end.Y == y)
                {
                    callBack.Invoke(x, y, true);
                    break;
                }

                MazePoint target = new MazePoint(-1, -1);
                if (isValid(x + 1, y, map, visited, width, height))
                {
                    target = new MazePoint(x + 1, y);
                }
                else if (isValid(x, y + 1, map, visited, width, height))
                {
                    target = new MazePoint(x, y + 1);
                }
                else if (isValid(x - 1, y, map, visited, width, height))
                {
                    target = new MazePoint(x - 1, y);
                }
                else if (isValid(x, y - 1, map, visited, width, height))
                {
                    target = new MazePoint(x, y - 1);
                }
                //Thread.Sleep(1000);

                if (target.X != -1)
                {
                    callBack.Invoke(x, y, true);
                    //var target = targets[r.Next(targets.Count)];
                    stackje.Push(target);
                    visited[target.X, target.Y] = true;
                    //form.pixelDraw(target.X, target.Y, Brushes.Blue);
                    //Thread.Sleep(200);

                    //if (target.X < x)
                    //{
                    //    visited[x - 1][y] = true;
                    //    //form.pixelDraw(x - 1, y, Brushes.White);
                    //}
                    //else if (target.X > x)
                    //{
                    //    visited[x + 1][y] = true;
                    //    //form.pixelDraw(x + 1, y, Brushes.White);
                    //}
                    //else if (target.Y < y)
                    //{
                    //    visited[x][y - 1] = true;
                    //    //form.pixelDraw(x, y - 1, Brushes.White);
                    //}
                    //else if (target.Y > y)
                    //{
                    //    visited[x][y + 1] = true;
                    //    //form.pixelDraw(x, y + 1, Brushes.White);
                    //}
                }
                else
                {
                    callBack.Invoke(x, y, false);
                    stackje.Pop();
                }
            }

            pointlist.AddRange(stackje);

            pointlist.Reverse();

            return(pointlist);
        }