Exemplo n.º 1
0
        protected Shape GenerateRandomTier2Shape(int[] cases, int index)
        {
            //generates a shape based on "cases" unique, random set of values
            Shape Temp = null;

            switch (cases[index])
            {
            case 0: Temp = new LobsterClawShape();
                break;

            case 1: Temp = new PlusShape();
                break;

            case 2: Temp = new VShape();
                break;

            case 3: Temp = new BucketShape();
                break;

            case 4: Temp = new StairShape();
                break;
            }

            return(Temp);
        }
Exemplo n.º 2
0
        // ReSharper disable once UnusedParameter.Local :  for consistency with ShaperH and future usage
        static Func <int, int, float> ShaperV(VShape vShape, int rows, int cols)
        {
            switch (vShape)
            {
            case VShape.Flat: return((_, __) => 0.5F);

            case VShape.Top: return((_, __) => 0F);

            case VShape.Bottom: return((_, __) => 1F);

            case VShape.Rightdown: if (cols <= 1)
                {
                    return((_, __) => 0.5F);
                }
                return((_, c) => c / ((float)cols - 1));

            case VShape.Rightup: if (cols <= 1)
                {
                    return((_, __) => 0.5F);
                }
                return((_, c) => 1 - c / ((float)cols - 1));

            case VShape.Benddown:
                if (cols <= 2)
                {
                    return((_, __) => 0F);
                }
                if (cols % 2 == 1)
                {
                    return((_, c) => 1 - Math.Abs(2 * c / ((float)cols - 1) - 1));
                }
                return((_, c) => c < cols / 2
                        ? 2 * c / ((float)cols - 2)
                        : 2 * (cols - c - 1) / ((float)cols - 2));

            case VShape.Bendup:
                if (cols <= 2)
                {
                    return((_, __) => 1F);
                }
                if (cols % 2 == 1)
                {
                    return((_, c) => Math.Abs(2 * c / ((float)cols - 1) - 1));
                }
                return((_, c) => c < cols / 2
                        ? 1 - 2 * c / ((float)cols - 2)
                        : 1 - 2 * (cols - c - 1) / ((float)cols - 2));

            default:
                throw new NotImplementedException($"Invalid ShaperV value {vShape}");
            }
        }
Exemplo n.º 3
0
        static IEnumerable <Rectangle> DoPosition(
            int rows,
            int cols,
            HShape hShape,
            VShape vShape,
            float margin,
            float spacing,
            Rectangle clientArea,
            IEnumerable <Rectangle> rectangles
            )
        {
            var scaleX  = (clientArea.Width - 2 * margin) / cols;
            var scaleY  = (clientArea.Height - 2 * margin) / rows;
            var shaperH = ShaperH(hShape, rows, cols);
            var shaperV = ShaperV(vShape, rows, cols);
            var grid    = Enumerable.Range(0, rows)
                          .SelectMany(r => Enumerable.Range(0, cols)
                                      .Select(c => new
            {
                area   = new Rectangle(margin + c * scaleX, margin + r * scaleY, scaleX, scaleY),
                hShape = shaperH(r, c),
                vShape = shaperV(r, c),
            }
                                              )
                                      );

            return(grid
                   .ZipLongest(rectangles, (area, rectangle) => new { area, rectangle })
                   .Where(x => x.rectangle != null && x.area != null)
                   .Select(x => x.rectangle.FitIn(x.area.area, x.area.hShape, x.area.vShape, spacing))
                   .ToArray());

            /*
             * if (spacing == 0) return draft;
             * var y0 = draft.GetAverageSpacing();
             * var delta = Math.Sign(spacing) * .05f;
             * var candidate = draft;
             * for (var i=0; i<10; i++)
             * {
             *  candidate = draft.IncreaseSpacing(delta).CheapToArray();
             *  var y1 = candidate.GetAverageSpacing();
             *  Trace.WriteLine($"spacing:{spacing}, y0:{y0}, y1:{y1}, delta:{delta}.");
             *  if (Math.Abs(y1 - spacing) < 0.01f)
             *  {
             *      return candidate;
             *  }
             *  var dy = (y1 - y0) / delta;
             *  delta = (spacing - y0) / dy;
             * }
             * return candidate;
             */
        }
Exemplo n.º 4
0
        public static Shape ConvertToShape(string s)
        {
            Shape  shape = null;
            string name  = "";

            string[] ss = new string[0];        //array of all the data parts
            int      numBlocks;                 //counting variables
            bool     extraData = false;         //Grab extra data from the string for shape shifting shape

            try
            {
                //Get name
                ss = s.Split('[');
                for (int i = 0; i < ss.Length; i++)
                {
                    ss[i] = ss[i].Trim('[', ']');
                }

                //Set up initial shape design
                name = ss[0];
                switch (name)
                {
                case "IShape":
                    shape = new IShape();
                    break;

                case "JShape":
                    shape = new JShape();
                    break;

                case "LShape":
                    shape = new LShape();
                    break;

                case "OShape":
                    shape = new OShape();
                    break;

                case "SShape":
                    shape = new SShape();
                    break;

                case "TShape":
                    shape = new TShape();
                    break;

                case "ZShape":
                    shape = new ZShape();
                    break;

                case "Corner Shape":
                    shape = new CornerShape();
                    break;

                case "Easy Diagonal Shape":
                    shape = new EasyDiagonalShape();
                    break;

                case "Hockey Stick Shape":
                    shape = new HockeyStickShape();
                    break;

                case "Oklahoma Shape":
                    shape = new OklahomaShape();
                    break;

                case "Reverse Hockey Stick Shape":
                    shape = new ReverseHockeyStickShape();
                    break;

                case "Bucket Shape":
                    shape = new BucketShape();
                    break;

                case "Lobster Claw Shape":
                    shape = new LobsterClawShape();
                    break;

                case "Plus Shape":
                    shape = new PlusShape();
                    break;

                case "Stair Shape":
                    shape = new StairShape();
                    break;

                case "VShape":
                    shape = new VShape();
                    break;

                case "Hard Diagonal Shape":
                    shape = new HardDiagonalShape();
                    break;

                case "Hook Shape":
                    shape = new HookShape();
                    break;

                case "Kite Shape":
                    shape = new KiteShape();
                    break;

                case "Tonfu Shape":
                    shape = new TonfuShape();
                    break;

                case "Shape Shifting Shape":
                    shape     = new ShapeShiftingShape();
                    extraData = true;
                    break;

                default:
                    shape = new OShape();
                    break;
                }

                //Set up blocks
                numBlocks = int.Parse(ss[1]);
                for (int i = 0; i < numBlocks; i++)
                {
                    int blockIndex = int.Parse(ss[2 + i]);
                    shape.blocks[i].blockColor = BlockColors.AllBlocks[blockIndex];
                }

                //If shapeshifting shape, get state relative positions of the different forms
                if (extraData)
                {
                    shape.stateRelativePositions.Clear();
                    int index = 2 + numBlocks;
                    for (int i = 0; i < 4; i++)
                    {
                        shape.stateRelativePositions.Add(new List <Vector2>());
                        int numFormBlocks = int.Parse(ss[index]);
                        index++;
                        for (int j = 0; j < numFormBlocks; j++)
                        {
                            int x = int.Parse(ss[index]);
                            int y = int.Parse(ss[index + 1]);
                            index += 2;

                            shape.stateRelativePositions[i].Add(new Vector2(x, y));
                        }
                    }
                }
            }
            catch
            {
                Console.WriteLine("String could not be converted to shape: {0}", s);
            }

            return(shape);
        }