示例#1
0
        //Metoden slumpar fram hur många figurer som ska presenteras i ett intervall mellan 5 och 21. Sedan skapas arrayen shapes utifrån det antalet.
        //För varje figur slumpas en längd och en bredd mellan 5 och 99.9. Det slumpas också fram vilken typ av figur som ska skapas, Ellips eller rektangel.
        //Metoden returneras arrayen shapes.
        private static Shape[] RandomizeShapes()
        {
            Random rnd = new Random(_randomSeed.Next());
            int randomShapeCount = rnd.Next(5, 21);
            Shape[] shapes = new Shape[randomShapeCount];
            Shape shape = null;

            for (int i = 0; i < randomShapeCount; i++)
            {

                Random rnd2 = new Random(_randomSeed.Next());
                double length = Math.Round((5 + rnd2.NextDouble() * (99.9 - 5)), 1);

                Random rnd3 = new Random(_randomSeed.Next());
                double width = Math.Round((5 + rnd3.NextDouble() * (99.9 - 5)), 1);

                Random rnd4 = new Random(_randomSeed.Next());
                ShapeType shapeType = (ShapeType)rnd4.Next(0, 2);

                switch (shapeType)
                {
                    case (ShapeType)0:
                        shape = new Ellipse(length, width);
                        break;
                    case (ShapeType)1:
                        shape = new Rectangle(length, width);
                        break;
                }

                shapes[i] = shape;
            }
            return shapes;
        }
示例#2
0
 //Presenterar figurerna med figurnamn vänsterjusterad och resten högerjusterade.
 private static void ViewShapes(Shape[] shapes)
 {
     Console.WriteLine("-----------------------------------------------------------");
     Console.WriteLine("{0, -10}{1, 10}{2, 10}{3, 10}{4, 10}", "Figur", "Längd", "Bredd", "Omkrets", "Area");
     Console.WriteLine("-----------------------------------------------------------");
     for (int i = 0; i < shapes.Length; i++)
     {
         Console.WriteLine(shapes[i].ToString());
     }
 }