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()}"); }
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"; } } }
public static double CalculateShapeArea(IShape shape) { return(shape.CalculateArea()); }
public string OutputAsJSON(IShape shape) { return($"{{ \"Area\" : {shape.CalculateArea()}, \"Perimeter\" : {shape.CalculatePerimeter()} }}"); }
public string OutputAsHTML(IShape shape) { return($"<p>Area : {shape.CalculateArea()}</p><br /><p>Perimeter : {shape.CalculatePerimeter()}</p>"); }
static bool AreTheSameSize(IShape s1, IShape s2) { return(s1.CalculateArea() == s2.CalculateArea()); }
static void PrintAreaForShape(IShape shape) { Console.WriteLine(); Console.WriteLine($"The area for the {shape.GetShapeName()} is {shape.CalculateArea()}"); }
private void ShowShape(IShape sh) { sh.Display(); sh.CalculateArea(); }
public static void PrintShapeInfo(IShape obj) { Console.Write("Area: " + obj.CalculateArea()); }
private static void Display(IShape s) { Console.WriteLine(s.CalculateArea()); }
public void CalculateArea(IShape shape) { ResultArea = shape.CalculateArea(); }