Untitled

 avatar
unknown
plain_text
a year ago
7.2 kB
8
Indexable
using System;
using System.Drawing;
using System.Windows.Forms;

// Корневой класс фигур
public abstract class Shape
{
    protected int x;
    protected int y;

    public Shape(int x, int y)
    {
        this.x = x;
        this.y = y;
    }

    public abstract void Draw(Graphics graphics);
    public abstract void MoveTo(int newX, int newY);
}

// Дочерний класс эллипсов
public class Ellipse : Shape
{
    protected int radiusX;
    protected int radiusY;

    public Ellipse(int x, int y, int radiusX, int radiusY) : base(x, y)
    {
        this.radiusX = radiusX;
        this.radiusY = radiusY;
    }

    public override void Draw(Graphics graphics)
    {
        graphics.DrawEllipse(Pens.Black, x - radiusX, y - radiusY, 2 * radiusX, 2 * radiusY);
    }

    public override void MoveTo(int newX, int newY)
    {
        // Проверка на выход за границы области рисования
        if (newX >= 0 && newY >= 0)
        {
            x = newX;
            y = newY;
        }
        else
        {
            MessageBox.Show("Cannot move the ellipse outside the drawing area");
        }
    }

    // Уникальный метод для изменения радиуса окружности
    public void ChangeRadius(int newRadiusX, int newRadiusY)
    {
        radiusX = newRadiusX;
        radiusY = newRadiusY;
    }
}

// Дочерний класс прямоугольников
public class Rectangle : Shape
{
    protected int width;
    protected int height;

    public Rectangle(int x, int y, int width, int height) : base(x, y)
    {
        this.width = width;
        this.height = height;
    }

    public override void Draw(Graphics graphics)
    {
        graphics.DrawRectangle(Pens.Black, x, y, width, height);
    }

    public override void MoveTo(int newX, int newY)
    {
        // Проверка на выход за границы области рисования
        if (newX >= 0 && newY >= 0)
        {
            x = newX;
            y = newY;
        }
        else
        {
            MessageBox.Show("Cannot move the rectangle outside the drawing area");
        }
    }

    // Уникальный метод для изменения линейных размеров прямоугольника
    public void ChangeSize(int newWidth, int newHeight)
    {
        width = newWidth;
        height = newHeight;
    }
}

// Дочерний класс окружностей
public class Circle : Ellipse
{
    public Circle(int x, int y, int radius) : base(x, y, radius, radius)
    {

    }

    // Уникальный метод для изменения радиуса окружности
    public void ChangeRadius(int newRadius)
    {
        radiusX = newRadius;
        radiusY = newRadius;
        
   }
}

// Дочерний класс квадратов
public class Square : Rectangle
{
   public Square(int x,int y,int side):base(x,y,side ,side )
   {

   }

    // Уникальный метод для изменения линейных размеров квадрата
    public void ChangeSize(int newSide)
    {
        width = newSide;
        height = newSide;
        
    }
}

// Дочерний класс многоугольников
public class Polygon : Shape
{
    protected int sides;

    public Polygon(int x, int y, int sides) : base(x, y)
    {
        this.sides = sides;
    }

    public override void Draw(Graphics graphics)
    {
        Point[] points = new Point[sides];
        double angle = 2 * Math.PI / sides;

        for (int i = 0; i < sides; i++)
        {
            int pointX = (int)(x + Math.Cos(i * angle) * 50);
            int pointY = (int)(y + Math.Sin(i * angle) * 50);
            points[i] = new Point(pointX, pointY);
        }

        graphics.DrawPolygon(Pens.Black, points);
    }

    public override void MoveTo(int newX, int newY)
    {
        // Проверка на выход за границы области рисования
        if (newX >= 0 && newY >= 0)
        {
            x = newX;
            y = newY;
            
       }
       else
       {
           MessageBox.Show("Cannot move the polygon outside the drawing area");
       }
   }
}

// Дочерний класс треугольников
public class Triangle : Polygon
{
   public Triangle(int x,int y):base(x,y ,3 )
   {

   }
}

// Класс сложной фигуры
public class ComplexShape : Shape
{
   private Shape[] shapes;

   public ComplexShape(int x,int y ,Shape[] shapes):base(x,y )
   {
       this.shapes = shapes;
   }

    public override void Draw(Graphics graphics)
    {
        foreach (Shape shape in shapes)
        {
            shape.Draw(graphics);
        }
    }

    public override void MoveTo(int newX, int newY)
    {
        // Проверка на выход за границы области рисования для каждой фигуры в составе сложной фигуры
        bool canMove = true;
        foreach (Shape shape in shapes)
        {
            if (newX < 0 || newY < 0)
            {
                canMove = false;
                break;
            }
        }

        if (canMove)
        {
            x = newX;
            y = newY;

            foreach (Shape shape in shapes)
            {
                shape.MoveTo(newX, newY);
            }
            
       }
       else
       {
           MessageBox.Show("Cannot move the complex shape outside the drawing area");
       }
   }
}

// Пример использования библиотеки классов для графических примитивов в Windows Forms
public class MainForm : Form
{
   private Shape[] shapes;

   public MainForm()
   {
       // Создание экземпляров различных фигур
       Ellipse ellipse = new Ellipse(50, 50, 30, 20);
       Rectangle rectangle = new Rectangle(100, 100, 80, 60);
       Circle circle = new Circle(200, 200, 40);
       Square square = new Square(300, 300, 70);
       Polygon polygon = new Polygon(400, 400, 5);
       Triangle triangle = new Triangle(500,500);

      // Создание сложной фигуры из простых фигур
       shapes = new Shape[] { ellipse, rectangle, circle, square, polygon, triangle };

       // Установка размеров формы
       Size = new Size(600, 600);
   }

   protected override void OnPaint(PaintEventArgs e)
   {
       base.OnPaint(e);

       Graphics graphics = e.Graphics;

       foreach (Shape shape in shapes)
       {
           shape.Draw(graphics);
       }
   }

   protected override void OnMouseDown(MouseEventArgs e)
   {
       base.OnMouseDown(e);

      // Перемещение фигуры по щелчку мыши
      foreach (Shape shape in shapes)
      {
          shape.MoveTo(shape.x + 10, shape.y + 10);
      }

      Invalidate();
  }
}

// Запуск приложения
public class Program
{
   public static void Main()
   {
       Application.Run(new MainForm());
   }
}
Editor is loading...
Leave a Comment