public ExplicitOutlineShape(OutlineShape template, InsideShapeDelegate isReserved)
     : this(template.XSize, template.YSize)
 {
     for (int x = 0; x < this.XSize; x++)
     {
         for (int y = 0; y < this.YSize; y++)
         {
             this.SetValue(x, y, (template[x, y] && (isReserved == null || !isReserved(x, y))));
         }
     }
 }
 public ExplicitOutlineShape(OutlineShape template)
     : this(template.XSize, template.YSize)
 {
     for (int x = 0; x < this.XSize; x++)
     {
         for (int y = 0; y < this.YSize; y++)
         {
             this.SetValue(x, y, template[x, y]);
         }
     }
 }
Beispiel #3
0
        /// <summary>
        /// Creates a shape using a random OutlineShapeBuilder.
        /// If applicable, the shape may be distorted.
        /// </summary>
        /// <param name="r"></param>
        /// <param name="xSize"></param>
        /// <param name="ySize"></param>
        /// <param name="offCenter"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        public static OutlineShape RandomInstance(Random r, int xSize, int ySize, double offCenter, double size)
        {
            OutlineShapeBuilder outlineShapeBuilder = RandomOutlineShapeBuilder(r);

            OutlineShape result = RandomInstance(r, outlineShapeBuilder, xSize, ySize, offCenter, size);

            // If applicable, replace the shape with a distorted version.
            if (r.Next(100) < result.DistortedPercentage(33))
            {
                result = result.DistortedCopy(r);
            }

            return(result);
        }
Beispiel #4
0
        /// <summary>
        /// Creates a shape using the given OutlineShapeBuilder.
        /// This shape will not be distorted.
        /// </summary>
        /// <param name="r"></param>
        /// <param name="outlineShapeBuilder"></param>
        /// <param name="xSize"></param>
        /// <param name="ySize"></param>
        /// <param name="offCenter"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        public static OutlineShape RandomInstance(Random r, OutlineShapeBuilder outlineShapeBuilder, int xSize, int ySize, double offCenter, double size)
        {
            double centerX = 0.5, centerY = 0.5;

            double dx = r.NextDouble() - 0.5, dy = r.NextDouble() - 0.5;

            centerX += offCenter * dx;
            centerY += offCenter * dy;

            // Reduce size when we are closer to the center than requested.
            double f = 1.0 - offCenter * 2.0 * (0.5 - Math.Max(Math.Abs(dx), Math.Abs(dy)));

            OutlineShape result = outlineShapeBuilder(r, xSize, ySize, centerX, centerY, size * f);

            return(result);
        }
        /// <summary>
        /// Returns the largest subset of the template shape whose squares are all connected to each other.
        /// </summary>
        /// <param name="template"></param>
        /// <param name="isReserved">defines the maze's reserved areas</param>
        /// <returns></returns>
        public static OutlineShape ConnectedSubset(OutlineShape template, InsideShapeDelegate isReserved)
        {
            ExplicitOutlineShape result = new ExplicitOutlineShape(template, isReserved);

            #region Scan the shape for connected areas.

            byte subsetId        = 1;
            int  largestAreaSize = 0;
            byte largestAreaId   = 0;

            for (int x = 0; x < result.XSize; x++)
            {
                for (int y = 0; y < result.YSize; y++)
                {
                    if (result.squares[x, y] == 1 && subsetId < byte.MaxValue)
                    {
                        int areaSize = result.FillSubset(x, y, ++subsetId);
                        if (areaSize > largestAreaSize)
                        {
                            largestAreaSize = areaSize;
                            largestAreaId   = subsetId;
                        }
                    }
                }
            }

            #endregion

            #region Leave only the largest subset, eliminate all others.

            for (int x = 0; x < result.XSize; x++)
            {
                for (int y = 0; y < result.YSize; y++)
                {
                    result.SetValue(x, y, (result.squares[x, y] == largestAreaId));
                }
            }

            #endregion

            return(result);
        }
        /// <summary>
        /// Returns the template shape, augmented by all totally enclosed areas.
        /// </summary>
        /// <param name="template"></param>
        /// <param name="isReserved">defines the maze's reserved areas</param>
        /// <returns></returns>
        public static OutlineShape Closure(OutlineShape template, InsideShapeDelegate isReserved)
        {
            ExplicitOutlineShape result = new ExplicitOutlineShape(template.Inverse());

            #region Scan and mark the reserved areas.

            if (isReserved != null)
            {
                byte reservedId = 3;

                for (int x = 0; x < result.XSize; x++)
                {
                    for (int y = 0; y < result.YSize; y++)
                    {
                        if (isReserved(x, y))
                        {
                            result.squares[x, y] = reservedId;
                        }
                    }
                }
            }

            #endregion

            #region Scan all outside areas.

            byte outsideId = 2;
            int  x0 = 0, x1 = result.XSize - 1, y0 = 0, y1 = result.YSize - 1;

            for (int x = 0; x < result.XSize; x++)
            {
                if (result.squares[x, y0] == 1)
                {
                    result.FillSubset(x, y0, outsideId);
                }
                if (result.squares[x, y1] == 1)
                {
                    result.FillSubset(x, y1, outsideId);
                }
            }
            for (int y = 0; y < result.YSize; y++)
            {
                if (result.squares[x0, y] == 1)
                {
                    result.FillSubset(x0, y, outsideId);
                }
                if (result.squares[x1, y] == 1)
                {
                    result.FillSubset(x1, y, outsideId);
                }
            }

            #endregion

            #region Add the areas which were not reached.

            for (int x = 0; x < result.XSize; x++)
            {
                for (int y = 0; y < result.YSize; y++)
                {
                    // 0: square is part of the template (not part of its inverse)
                    // 1: square is not part of the template, but was not reached
                    result.SetValue(x, y, (result.squares[x, y] <= 1));
                }
            }

            #endregion

            return(result);
        }