Ejemplo n.º 1
0
        private static void Generate20RandomShapes()
        {
            Console.WriteLine(new string(' ', 33) + "Generating 20 random shapes");
            Console.WriteLine(new string('-', 90));
            float circumference = 0, totalArea = 0, maxVolume = 0;
            int   numberOfShapes = 20;
            var   shapes         = new List <Shape>();
            var   bigShapes      = new List <Shape3D>();
            var   bigShapeVolume = new List <float>();

            shapes.Clear();
            bigShapes.Clear();
            bigShapeVolume.Clear();
            for (int i = 0; i < numberOfShapes; i++)
            {
                shapes.Add(Shape.GenerateShape());
            }
            foreach (var shape in shapes)
            {
                totalArea += shape.Area;
                if (shape is Triangle)
                {
                    var triangle = shape as Triangle;
                    circumference += triangle.Circumference;
                    Console.WriteLine(triangle);
                }
                else if (shape is Shape3D)
                {
                    if (shape is Cuboid)
                    {
                        var cuboid = shape as Cuboid;
                        if (cuboid.Volume > maxVolume)
                        {
                            maxVolume = cuboid.Volume;
                            bigShapeVolume.Add(cuboid.Volume);
                            bigShapes.Add(cuboid);
                        }
                        Console.WriteLine(cuboid);
                    }
                    if (shape is Sphere)
                    {
                        var sphere = shape as Sphere;
                        if (sphere.Volume > maxVolume)
                        {
                            maxVolume = sphere.Volume;
                            bigShapeVolume.Add(sphere.Volume);
                            bigShapes.Add(sphere);
                        }
                        Console.WriteLine(sphere);
                    }
                }
                else
                {
                    Console.WriteLine(shape);
                }
            }
            Console.WriteLine(new string('-', 90));
            Console.WriteLine("Total circumference for all triangle shapes: {0:0.0}", circumference);
            Console.WriteLine(new string('-', 90));
            Console.WriteLine("Average area for all shape objects: {0:0.0}", totalArea / numberOfShapes);
            Console.WriteLine(new string('-', 90));
            if (bigShapes.Count == 0)
            {
                Console.WriteLine("Largest 3D Shape: \"No 3D Shapes were generated this run.\"");
            }
            else
            {
                Console.WriteLine("Largest 3D Shape: {0}", bigShapes[bigShapes.Count - 1]);
                Console.WriteLine("Volume of the largest 3D Shape: {0:0.0}", maxVolume);
            }
            Console.WriteLine(new string('-', 90));
            Console.Write("Press any key to continue... ");
            Console.ReadKey();
            Console.WriteLine();
        }
Ejemplo n.º 2
0
        private static void Generate20RandomShapesAtFixedPositions()
        {
            float xAxis = 0, yAxis = 0, zAxis = 0;

            Console.Write("Input x-coordinate: ");
            bool parsed = false;

            do
            {
                parsed = float.TryParse(Console.ReadLine(), out xAxis);
                if (!parsed || (xAxis ! > 100 ^ xAxis ! < -100))
                {
                    Console.Write("Invalid input. Input a float between -100 and 100: ");
                }
            } while (!parsed || (xAxis ! > 100 ^ xAxis ! < -100));
            Console.Write("Input y-coordinate: ");
            parsed = false;
            do
            {
                parsed = float.TryParse(Console.ReadLine(), out yAxis);
                if (!parsed || (yAxis ! > 100 ^ yAxis ! < -100))
                {
                    Console.Write("Invalid input. Input a float between -100 and 100: ");
                }
            } while (!parsed || (yAxis ! > 100 ^ yAxis ! < -100));
            Console.Write("Input z-coordinate: ");
            parsed = false;
            do
            {
                parsed = float.TryParse(Console.ReadLine(), out zAxis);
                if (!parsed || (zAxis ! > 100 ^ zAxis ! < -100))
                {
                    Console.Write("Invalid input. Input a float between -100 and 100: ");
                }
            } while (!parsed || (zAxis ! > 100 ^ zAxis ! < -100));

            Console.WriteLine(new string('-', 90));
            Console.WriteLine(new string(' ', 20) + "Generating 20 random shapes at predefined positions");
            Console.WriteLine(new string('-', 90));

            float circumference = 0, totalArea = 0, maxVolume = 0;
            int   numberOfShapes = 20;
            var   shapes         = new List <Shape>();
            var   bigShapes      = new List <Shape3D>();
            var   bigShapeVolume = new List <float>();

            shapes.Clear();
            bigShapes.Clear();
            bigShapeVolume.Clear();
            for (int i = 0; i < numberOfShapes; i++)
            {
                shapes.Add(Shape.GenerateShape(new Vector3(xAxis, yAxis, zAxis)));
            }
            foreach (var shape in shapes)
            {
                totalArea += shape.Area;
                if (shape is Triangle)
                {
                    var triangle = shape as Triangle;
                    circumference += triangle.Circumference;
                    Console.WriteLine(triangle);
                }
                else if (shape is Shape3D)
                {
                    if (shape is Cuboid)
                    {
                        var cuboid = shape as Cuboid;
                        if (cuboid.Volume > maxVolume)
                        {
                            maxVolume = cuboid.Volume;
                            bigShapeVolume.Add(cuboid.Volume);
                            bigShapes.Add(cuboid);
                        }
                        Console.WriteLine(cuboid);
                    }
                    if (shape is Sphere)
                    {
                        var sphere = shape as Sphere;
                        if (sphere.Volume > maxVolume)
                        {
                            maxVolume = sphere.Volume;
                            bigShapeVolume.Add(sphere.Volume);
                            bigShapes.Add(sphere);
                        }
                        Console.WriteLine(sphere);
                    }
                }
                else
                {
                    Console.WriteLine(shape);
                }
            }
            Console.WriteLine(new string('-', 90));
            Console.WriteLine("Total circumference for all triangle shapes: {0:0.0}", circumference);
            Console.WriteLine(new string('-', 90));
            Console.WriteLine("Average area for all shape objects: {0:0.0}", totalArea / numberOfShapes);
            Console.WriteLine(new string('-', 90));
            if (bigShapes.Count == 0)
            {
                Console.WriteLine("Largest 3D Shape: \"No 3D Shapes were generated this run.\"");
            }
            else
            {
                Console.WriteLine("Largest 3D Shape: {0}", bigShapes[bigShapes.Count - 1]);
                Console.WriteLine("Volume of the largest 3D Shape: {0:0.0}", maxVolume);
            }
            Console.WriteLine(new string('-', 90));
            Console.Write("Press any key to continue... ");
            Console.ReadKey();
            Console.WriteLine();
        }