Exemple #1
0
 static void PrintShapeDetails(IShape shape)
 {
     //Circle circle = (Circle)shape; // throws invalidcastexception when a rectangle is passed
     // downcasting can fail!
     Console.WriteLine($"Sides: {shape.Sides}");
     Console.WriteLine($"Area: {shape.CalculateArea()}");
 }
Exemple #2
0
        static void Main(string[] args)
        {
            ShapeFactory shapeFactory = new ShapeFactory();
            double       sum          = 0;

            for (int i = 0; i < 10; i++)
            {
                IShape shape = shapeFactory.CreateRandomShape();
                if (shape.IsApproved())
                {
                    Console.Write("合法图形。");
                    shape.Describe();
                    Console.WriteLine("面积:" + shape.CalculateArea());
                    sum += shape.CalculateArea();
                }
                else
                {
                    Console.Write("不合法图形。");
                }
            }
            Console.WriteLine("面积之和:" + sum);
        }
        //Sets mouse x and y coordinates. Gets cursor position. Removes ouline of the selected shape if any.
        //Tries to find if cursor is inside a shape when clicked.
        //If its not - deselectes selected shape if any and sets selected area to 0.0
        //If it is - sets new selected shape. Sets selected shape area. Increaces current max layer and sets shape layer
        //And draws shape.
        //Note: always selects the top most shape even if 2 or more shapes are on top of one another
        private void Canvas_MouseDown(object sender, MouseEventArgs e)
        {
            this.mouseDownXCoordinate = e.X;
            this.mouseDownYCoordinate = e.Y;

            if (e.Button == MouseButtons.Left)
            {
                PointF cursorPosition = new PointF(e.X, e.Y);

                if (this.selectedShape != null)
                {
                    this.selectedShape.Outline(this.graphics, this.selectedShape.Color);
                }

                bool cursorIsOnAnyShape = false;

                foreach (IShape shape in shapes.OrderByDescending(s => s.CurrentLayer))
                {
                    bool isInside = shape.Contains(cursorPosition);

                    if (isInside)
                    {
                        this.currentMaxLayer += 1;

                        this.selectedShape = shape;
                        this.SelectedShapeAreaNumber.Text = $"{selectedShape.CalculateArea():f1}";

                        this.selectedShape.ChangeCurrentLayer(this.currentMaxLayer);

                        this.selectedShape.Draw(this.graphics);

                        cursorIsOnAnyShape = true;

                        break;
                    }
                }

                if (!cursorIsOnAnyShape)
                {
                    this.selectedShape = null;
                    this.SelectedShapeAreaNumber.Text = "0.0";
                }
            }
        }
Exemple #4
0
 public static double CalculateShapeArea(IShape shape)
 {
     return(shape.CalculateArea());
 }
Exemple #5
0
 public string OutputAsJSON(IShape shape)
 {
     return($"{{ \"Area\" : {shape.CalculateArea()}, \"Perimeter\" : {shape.CalculatePerimeter()} }}");
 }
Exemple #6
0
 public string OutputAsHTML(IShape shape)
 {
     return($"<p>Area : {shape.CalculateArea()}</p><br /><p>Perimeter : {shape.CalculatePerimeter()}</p>");
 }
Exemple #7
0
 static bool AreTheSameSize(IShape s1, IShape s2)
 {
     return(s1.CalculateArea() == s2.CalculateArea());
 }
Exemple #8
0
 static void PrintAreaForShape(IShape shape)
 {
     Console.WriteLine();
     Console.WriteLine($"The area for the {shape.GetShapeName()} is {shape.CalculateArea()}");
 }
Exemple #9
0
 private void ShowShape(IShape sh)
 {
     sh.Display();
     sh.CalculateArea();
 }
Exemple #10
0
 public static void PrintShapeInfo(IShape obj)
 {
     Console.Write("Area: " + obj.CalculateArea());
 }
Exemple #11
0
 private static void  Display(IShape s)
 {
     Console.WriteLine(s.CalculateArea());
 }
Exemple #12
0
 public void CalculateArea(IShape shape)
 {
     ResultArea = shape.CalculateArea();
 }