Загрузка данных
using System;
namespace CW5.Variant01
{
public class Task1
{
// Структура двумерной точки
public struct Point : IPoint
{
private int _x;
private int _y;
public int X => _x;
public int Y => _y;
// Расстояние от точки до центра координат: $L = \sqrt{x^2 + y^2}$
public double Length => Math.Sqrt(_x * _x + _y * _y);
// Конструктор, принимающий координаты
public Point(int x, int y)
{
_x = x;
_y = y;
}
// Вычисление расстояния до другой точки: $D = \sqrt{\Delta x^2 + \Delta y^2}$
public double GetDistance(IPoint point)
{
if (point == null) return 0;
double dx = _x - point.X;
double dy = _y - point.Y;
return Math.Sqrt(dx * dx + dy * dy);
}
public void UpdateX(int x) => _x = x;
public void UpdateY(int y) => _y = y;
public void UpdateXY(int x, int y)
{
_x = x;
_y = y;
}
}
}
}
using System;
namespace CW5.Variant01
{
public class Task2
{
// Преобразование структуры Point из Task1 в класс
public class Point : IPoint
{
protected int _x;
protected int _y;
public int X => _x;
public int Y => _y;
public double Length => Math.Sqrt(_x * _x + _y * _y);
public Point(int x, int y)
{
_x = x;
_y = y;
}
public double GetDistance(IPoint point)
{
if (point == null) return 0;
double dx = _x - point.X;
double dy = _y - point.Y;
return Math.Sqrt(dx * dx + dy * dy);
}
public void UpdateX(int x) => _x = x;
public void UpdateY(int y) => _y = y;
public void UpdateXY(int x, int y)
{
_x = x;
_y = y;
}
}
// Класс-наследник цветной точки
public class ColorPoint : Point, IColorPoint
{
private char _color;
public char Color => _color;
// Конструктор с параметром цвета по умолчанию ('0' - черный)
public ColorPoint(int x, int y, char color = '0') : base(x, y)
{
UpdateColor(color);
}
// Метод обновления цвета с валидацией допустимых значений
public void UpdateColor(char color)
{
if (color == 'r' || color == 'g' || color == 'b' || color == '0' || color == '1')
{
_color = color;
}
else
{
_color = '0'; // Сброс в черный при некорректном значении
}
}
}
// Класс матрицы изображения
public class Picture : IPicture
{
private ColorPoint[,] _image;
// Свойство возвращает массив: [количество по горизонтали, количество по вертикали]
public int[] Size => new int[] { _image.GetLength(0), _image.GetLength(1) };
public ColorPoint[,] Image => _image;
// Конструктор: построчное заполнение черными точками ('0') сверху вниз
public Picture(int width, int height)
{
_image = new ColorPoint[width, height];
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
_image[x, y] = new ColorPoint(x, y, '0');
}
}
}
// Изменение цвета конкретной точки изображения по ее координатам
public void Add(IPoint point)
{
if (point == null || _image == null) return;
int x = point.X;
int y = point.Y;
// Проверка попадания в границы матрицы холста
if (x >= 0 && x < _image.GetLength(0) && y >= 0 && y < _image.GetLength(1))
{
char targetColor = '0';
if (point is IColorPoint colorPoint)
{
targetColor = colorPoint.Color;
}
_image[x, y].UpdateColor(targetColor);
}
}
// Добавление массива точек на изображение
public void Add(IPoint[] points)
{
if (points == null) return;
foreach (IPoint p in points)
{
Add(p);
}
}
}
}
}