Compare commits

..

2 Commits
master ... lab3

Author SHA1 Message Date
9eb2688a38 work lab3 2024-06-05 11:56:17 +04:00
6d9b87416f work lab 2 2024-06-05 11:54:48 +04:00
24 changed files with 1604 additions and 352 deletions

View File

@ -0,0 +1,69 @@
using AccordionBus.Drawnings;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AccordionBus.CollectionGenericObjects
{
public abstract class AbstractCompany
{
protected readonly int _placeSizeWidth = 180;
protected readonly int _placeSizeHeight = 60;
protected readonly int _pictureWidth;
protected readonly int _pictureHeight;
protected ICollectionGenericObjects<DrawningBus?> _collection = null;
private int GetMaxCount => _pictureWidth * _pictureHeight / (_placeSizeWidth * _placeSizeHeight);
public AbstractCompany(int picWidth, int picHeigth, ICollectionGenericObjects<DrawningBus> collection)
{
_pictureWidth = picWidth;
_pictureHeight = picHeigth;
_collection = collection;
_collection.SetMaxCount = GetMaxCount;
}
public static bool operator +(AbstractCompany company, DrawningBus bus)
{
return company._collection?.Insert(bus) ?? false;
}
public static bool operator -(AbstractCompany company, int position)
{
return company._collection?.Remove(position) ?? false;
}
public DrawningBus? GetRandomObject()
{
Random rnd = new();
return _collection?.Get(rnd.Next(GetMaxCount));
}
public Bitmap? Show()
{
Bitmap bitmap = new(_pictureWidth, _pictureHeight);
Graphics graphics = Graphics.FromImage(bitmap);
DrawBackground(graphics);
for (int i = 0; i < (_collection?.Count ?? 0); i++)
{
DrawningBus? obj = _collection?.Get(i);
SetObjectPosition(i, _collection?.Count ?? 0, obj);
obj?.DrawTransport(graphics);
}
return bitmap;
}
protected abstract void DrawBackground(Graphics g);
protected abstract void SetObjectPosition(int position, int MaxPos, DrawningBus? bus);
}
}

View File

@ -0,0 +1,47 @@
using AccordionBus.Drawnings;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AccordionBus.CollectionGenericObjects
{
public class BusStation : AbstractCompany
{
public BusStation(int picWidth, int picHeight, ICollectionGenericObjects<DrawningBus> collection) : base(picWidth, picHeight, collection)
{
}
Pen black = new Pen(Color.Black);
protected override void DrawBackground(Graphics g)
{
for (int i = _pictureHeight - 1; i >= 0; i -= _placeSizeHeight)
{
g.DrawLine(black, _pictureWidth - ((int)(_pictureWidth / _placeSizeWidth) * _placeSizeWidth), i, _pictureWidth, i);
for (int j = _pictureWidth - 1; j >= 0; j -= _placeSizeWidth)
{
g.DrawLine(black, j, i, j, i - _placeSizeHeight + 20);
}
}
}
protected override void SetObjectPosition(int position, int MaxPos, DrawningBus? bus)
{
if (bus == null) return;
int _levelOfPosition = 0;
int _countPositionInRange = _pictureWidth / _placeSizeWidth;
if (position >= _countPositionInRange)
{
_levelOfPosition = position / _countPositionInRange;
}
if (position >= _countPositionInRange) position %= _countPositionInRange;
bus.SetPosition(_pictureWidth - position * _placeSizeWidth - bus.GetWidth() - (_placeSizeWidth - bus.GetWidth()) / 2,
_pictureHeight - _levelOfPosition * _placeSizeHeight - bus.GetHeigth() - (_placeSizeHeight - bus.GetHeigth()) / 2);
}
}
}

View File

@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AccordionBus.CollectionGenericObjects
{
public interface ICollectionGenericObjects<T>
where T : class
{
/// <summary>
/// кол-во элем
/// </summary>
int Count { get; }
/// <summary>
/// установить макс элем
/// </summary>
int SetMaxCount { set; }
/// <summary>
/// вставить
/// </summary>
/// <param name="obj">добавляемый объект</param>
/// <returns></returns>
bool Insert(T obj);
/// <summary>
/// вставить по позиции
/// </summary>
/// <param name="obj">добавляемый объект</param>
/// <param name="position">индекс</param>
/// <returns></returns>
bool Insert(T obj, int position);
/// <summary>
/// удаление
/// </summary>
/// <param name="position">индекс</param>
/// <returns></returns>
bool Remove(int position);
/// <summary>
/// получение объекта по позиции
/// </summary>
/// <param name="position">индекс</param>
/// <returns></returns>
T? Get(int position);
}
}

View File

@ -0,0 +1,82 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AccordionBus.CollectionGenericObjects
{
public class MassiveGenericObjects<T> : ICollectionGenericObjects<T>
where T : class
{
private T?[] _collection;
public int Count => _collection.Length;
public int SetMaxCount { set { if (value > 0) { _collection = new T?[value]; } } }
public MassiveGenericObjects()
{
_collection = Array.Empty<T?>();
}
public T? Get(int position)
{
if (position < 0 || position >= _collection.Length) return null;
return _collection[position];
}
public bool Insert(T obj)
{
for (int i = 0; i < _collection.Length; i++)
{
if (_collection[i] == null)
{
_collection[i] = obj;
return true;
}
}
return false;
}
public bool Insert(T obj, int position)
{
if (position < 0 || position >= _collection.Length) { return false; }
if (_collection[position] == null)
{
_collection[position] = obj;
return true;
}
else
{
for (int i = position + 1; i < _collection.Length; i++)
{
if (_collection[i] == null)
{
_collection[i] = obj;
return true;
}
}
for (int i = position - 1; i >= 0; i--)
{
if (_collection[i] == null)
{
_collection[i] = obj;
return true;
}
}
}
return false;
}
public bool Remove(int position)
{
if (position < 0 || position >= _collection.Length) { return false;}
_collection[position] = null;
return true;
}
}
}

View File

@ -1,284 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AccordionBus
{
/// <summary>
/// Класс, отвечающий за перемещение и прорисовку объекта-сущности
/// </summary>
public class DrawningAccordionBus
{
/// <summary>
/// Класс-сущность
/// </summary>
public EntityAccordionBus? EntityAccordionBus { get; private set; }
/// <summary>
/// Ширина окна
/// </summary>
private int? _pictureWeight;
/// <summary>
/// Высота окна
/// </summary>
private int? _pictureHeight;
/// <summary>
/// Левая координата прорисовки авто
/// </summary>
private int? _startPosX;
/// <summary>
/// Верхняя координата прорисовки авто
/// </summary>
private int? _startPosY;
/// <summary>
/// Ширина прорисовки авто
/// </summary>
private readonly int _drawningBusWeight = 130;
/// <summary>
/// Высота прорисовки авто
/// </summary>
private readonly int _drawningBusHeight = 20;
/// <summary>
/// Инициализация свойств
/// </summary>
/// <param name="speed"></param>
/// <param name="weight"></param>
/// <param name="bodyColor"></param>
/// <param name="additionalColor"></param>
/// <param name="threeDoors"></param>
/// <param name="fourDoors"></param>
/// <param name="fiveDoors"></param>
public void Init(int speed, double weight, Color bodyColor, Color
additionalColor, bool onePart, bool fiveDoors)
{
EntityAccordionBus = new EntityAccordionBus();
EntityAccordionBus.Init(speed, weight, bodyColor, additionalColor, onePart, fiveDoors);
_pictureWeight = null;
_pictureHeight = null;
_startPosX = null;
_startPosY = null;
}
/// <summary>
/// Установка границ поля
/// </summary>
/// <param name="weight">Ширина</param>
/// <param name="height">Высота</param>
/// <returns>true - границы заданы, false - проверка не пройдена</returns>
public bool SetPictureSize(int weight, int height)
{
if (weight < _drawningBusWeight || height < _drawningBusHeight)
{
return false;
}
_pictureWeight = weight;
_pictureHeight = height;
if (_startPosX.HasValue && _startPosX.Value + _drawningBusWeight > _pictureWeight)
{
_startPosX -= _startPosX.Value + _drawningBusWeight - _pictureWeight;
}
else if (_startPosY.HasValue && _startPosY.Value + _drawningBusHeight > _pictureHeight)
{
_startPosY -= _startPosY.Value + _drawningBusHeight - _pictureHeight;
}
return true;
}
/// <summary>
/// Установка позиции
/// </summary>
/// <param name="x">Координата X</param>
/// <param name="y">Координата Y</param>
public void SetPosition(int x, int y)
{
if (!_pictureHeight.HasValue || !_pictureWeight.HasValue)
{
return;
}
if (x + _drawningBusWeight > _pictureWeight)
{
_startPosX = x - ( x + _drawningBusWeight - _pictureWeight);
}
else if (x < 0)
{
_startPosX = 0;
}
else
{
_startPosX = x;
}
if (y + _drawningBusHeight > _pictureHeight)
{
_startPosY = y - ( y + _drawningBusHeight - _pictureHeight);
}
else if (y < 0)
{
_startPosY = 0;
}
else
{
_startPosY = y;
}
}
/// <summary>
/// Перемещение
/// </summary>
/// <param name="direction">Направление</param>
/// <returns>true - перемещение выполнено, false - перемещение невозможнор</returns>
public bool MoveTransport(DirectionType direction)
{
if (EntityAccordionBus == null || !_startPosX.HasValue ||
!_startPosY.HasValue)
{
return false;
}
switch (direction)
{
case DirectionType.Left:
if (_startPosX.Value - EntityAccordionBus.Step > 0)
{
_startPosX -= (int)EntityAccordionBus.Step;
}
else
{
_startPosX = 0;
}
return true;
case DirectionType.Right:
if (_startPosX.Value + EntityAccordionBus.Step + _drawningBusWeight < _pictureWeight)
{
_startPosX += (int)EntityAccordionBus.Step;
}
else
{
_startPosX = _pictureWeight - _drawningBusWeight;
}
return true;
case DirectionType.Up:
if (_startPosY.Value - EntityAccordionBus.Step > 0)
{
_startPosY -= (int)EntityAccordionBus.Step;
}
else
{
_startPosY = 0;
}
return true;
case DirectionType.Down:
if (_startPosY.Value + EntityAccordionBus.Step + _drawningBusHeight < _pictureHeight)
{
_startPosY += (int)EntityAccordionBus.Step;
}
else
{
_startPosY = _pictureHeight - _drawningBusHeight;
}
return true;
default:
return false;
}
}
/// <summary>
/// Отрисовка транспорта
/// </summary>
/// <param name="g"></param>
public void DrawTransport(Graphics g)
{
if (EntityAccordionBus == null || !_startPosX.HasValue ||
!_startPosY.HasValue)
{
return;
}
Pen pen = new(Color.Black);
Brush additionalBrush = new SolidBrush(EntityAccordionBus.AdditionalColor);
//корпус
Brush br = new SolidBrush(EntityAccordionBus.BodyColor);
g.FillRectangle(br, _startPosX.Value, _startPosY.Value, 60, 15);
g.DrawRectangle(pen, _startPosX.Value, _startPosY.Value, 60, 15);
//колёса
Brush brWhite = new SolidBrush(Color.White);
g.FillEllipse(brWhite, _startPosX.Value + 5, _startPosY.Value + 10, 10, 10);
g.FillEllipse(brWhite, _startPosX.Value + 40, _startPosY.Value + 10, 10, 10);
g.DrawEllipse(pen, _startPosX.Value + 40, _startPosY.Value + 10, 10, 10);
g.DrawEllipse(pen, _startPosX.Value + 5, _startPosY.Value + 10, 10, 10);
//стёкла
Brush brBlue = new SolidBrush(Color.LightBlue);
g.FillRectangle(brBlue, _startPosX.Value + 2, _startPosY.Value + 3, 5, 5);
g.DrawRectangle(pen, _startPosX.Value + 2, _startPosY.Value + 3, 5, 5);
g.FillRectangle(brBlue, _startPosX.Value + 12, _startPosY.Value + 3, 5, 5);
g.DrawRectangle(pen, _startPosX.Value + 12, _startPosY.Value + 3, 5, 5);
g.FillRectangle(brBlue, _startPosX.Value + 32, _startPosY.Value + 3, 5, 5);
g.DrawRectangle(pen, _startPosX.Value + 32, _startPosY.Value + 3, 5, 5);
g.FillRectangle(brBlue, _startPosX.Value + 42, _startPosY.Value + 3, 5, 5);
g.DrawRectangle(pen, _startPosX.Value + 42, _startPosY.Value + 3, 5, 5);
//дверь
g.FillRectangle(additionalBrush, _startPosX.Value + 20, _startPosY.Value + 5, 5, 10);
g.DrawRectangle(pen, _startPosX.Value + 20, _startPosY.Value + 5, 5, 10);
if (!EntityAccordionBus.OnePart)
{
//корпус
g.FillRectangle(br, _startPosX.Value + 70, _startPosY.Value, 60, 15);
g.DrawRectangle(pen, _startPosX.Value + 70, _startPosY.Value, 60, 15);
//колёса
g.FillEllipse(brWhite, _startPosX.Value + 75, _startPosY.Value + 10, 10, 10);
g.FillEllipse(brWhite, _startPosX.Value + 110, _startPosY.Value + 10, 10, 10);
g.DrawEllipse(pen, _startPosX.Value + 110, _startPosY.Value + 10, 10, 10);
g.DrawEllipse(pen, _startPosX.Value + 75, _startPosY.Value + 10, 10, 10);
//стёкла
g.FillRectangle(brBlue, _startPosX.Value + 72, _startPosY.Value + 3, 5, 5);
g.DrawRectangle(pen, _startPosX.Value + 72, _startPosY.Value + 3, 5, 5);
g.FillRectangle(brBlue, _startPosX.Value + 82, _startPosY.Value + 3, 5, 5);
g.DrawRectangle(pen, _startPosX.Value + 82, _startPosY.Value + 3, 5, 5);
g.FillRectangle(brBlue, _startPosX.Value + 92, _startPosY.Value + 3, 5, 5);
g.DrawRectangle(pen, _startPosX.Value + 92, _startPosY.Value + 3, 5, 5);
g.FillRectangle(brBlue, _startPosX.Value + 102, _startPosY.Value + 3, 5, 5);
g.DrawRectangle(pen, _startPosX.Value + 102, _startPosY.Value + 3, 5, 5);
g.FillRectangle(brBlue, _startPosX.Value + 112, _startPosY.Value + 3, 5, 5);
g.DrawRectangle(pen, _startPosX.Value + 112, _startPosY.Value + 3, 5, 5);
//гормошка
g.DrawLine(pen, _startPosX.Value + 60, _startPosY.Value, _startPosX.Value + 62, _startPosY.Value + 3);
g.DrawLine(pen, _startPosX.Value + 62, _startPosY.Value + 3, _startPosX.Value + 65, _startPosY.Value);
g.DrawLine(pen, _startPosX.Value + 65, _startPosY.Value, _startPosX.Value + 67, _startPosY.Value + 3);
g.DrawLine(pen, _startPosX.Value + 67, _startPosY.Value + 3, _startPosX.Value + 70, _startPosY.Value);
g.DrawLine(pen, _startPosX.Value + 60, _startPosY.Value + 15, _startPosX.Value + 62, _startPosY.Value + 12);
g.DrawLine(pen, _startPosX.Value + 62, _startPosY.Value + 12, _startPosX.Value + 65, _startPosY.Value + 15);
g.DrawLine(pen, _startPosX.Value + 65, _startPosY.Value + 15, _startPosX.Value + 67, _startPosY.Value + 12);
g.DrawLine(pen, _startPosX.Value + 67, _startPosY.Value + 12, _startPosX.Value + 70, _startPosY.Value + 15);
g.DrawLine(pen, _startPosX.Value + 62, _startPosY.Value + 3, _startPosX.Value + 62, _startPosY.Value + 12);
g.DrawLine(pen, _startPosX.Value + 67, _startPosY.Value + 3, _startPosX.Value + 67, _startPosY.Value + 12);
g.DrawLine(pen, _startPosX.Value + 65, _startPosY.Value, _startPosX.Value + 65, _startPosY.Value + 15);
//двери
g.FillRectangle(additionalBrush, _startPosX.Value + 123, _startPosY.Value + 5, 5, 10);
g.DrawRectangle(pen, _startPosX.Value + 123, _startPosY.Value + 5, 5, 10);
if (EntityAccordionBus.FiveDoors)
{
g.FillRectangle(additionalBrush, _startPosX.Value + 87, _startPosY.Value + 9, 21, 5);
g.DrawRectangle(pen, _startPosX.Value + 87, _startPosY.Value + 9, 21, 5);
g.FillRectangle(additionalBrush, _startPosX.Value + 53, _startPosY.Value + 5, 5, 10);
g.DrawRectangle(pen, _startPosX.Value + 53, _startPosY.Value + 5, 5, 10);
g.FillRectangle(additionalBrush, _startPosX.Value + 27, _startPosY.Value + 9, 11, 5);
g.DrawRectangle(pen, _startPosX.Value + 27, _startPosY.Value + 9, 11, 5);
}
}
}
}
}

View File

@ -4,13 +4,17 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AccordionBus
namespace AccordionBus.Drawnings
{
/// <summary>
/// Направление премещения
/// </summary>
public enum DirectionType
{
/// <summary>
/// Неизвестное направление
/// </summary>
Unknow = -1,
/// <summary>
/// Вверх
/// </summary>

View File

@ -0,0 +1,100 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AccordionBus.Entities;
namespace AccordionBus.Drawnings
{
/// <summary>
/// Класс, отвечающий за перемещение и прорисовку объекта-сущности
/// </summary>
public class DrawningAccordionBus : DrawningBus
{
/// <summary>
/// Инициализация свойств
/// </summary>
/// <param name="speed"></param>
/// <param name="weight"></param>
/// <param name="bodyColor"></param>
/// <param name="additionalColor"></param>
/// <param name="onePart"></param>
/// <param name="fiveDoors"></param>
public DrawningAccordionBus(int speed, double weight, Color bodyColor, Color
additionalColor, bool onePart, bool fiveDoors): base(130, 20)
{
EntityBus = new EntityAccordionBus(speed, weight, bodyColor, additionalColor, onePart, fiveDoors);
}
/// <summary>
/// Отрисовка транспорта
/// </summary>
/// <param name="g"></param>
public override void DrawTransport(Graphics g)
{
if (EntityBus == null || EntityBus is not EntityAccordionBus accordionBus || !_startPosX.HasValue ||
!_startPosY.HasValue)
{
return;
}
base.DrawTransport(g);
if (!accordionBus.OnePart)
{
Pen pen = new(Color.Black);
Brush brWhite = new SolidBrush(Color.White);
Brush br = new SolidBrush(accordionBus.BodyColor);
Brush brBlue = new SolidBrush(Color.LightBlue);
Brush additionalBrush = new SolidBrush(accordionBus.AdditionalColor);
//корпус
g.FillRectangle(br, _startPosX.Value + 70, _startPosY.Value, 60, 15);
g.DrawRectangle(pen, _startPosX.Value + 70, _startPosY.Value, 60, 15);
//колёса
g.FillEllipse(brWhite, _startPosX.Value + 75, _startPosY.Value + 10, 10, 10);
g.FillEllipse(brWhite, _startPosX.Value + 110, _startPosY.Value + 10, 10, 10);
g.DrawEllipse(pen, _startPosX.Value + 110, _startPosY.Value + 10, 10, 10);
g.DrawEllipse(pen, _startPosX.Value + 75, _startPosY.Value + 10, 10, 10);
//стёкла
g.FillRectangle(brBlue, _startPosX.Value + 72, _startPosY.Value + 3, 5, 5);
g.DrawRectangle(pen, _startPosX.Value + 72, _startPosY.Value + 3, 5, 5);
g.FillRectangle(brBlue, _startPosX.Value + 82, _startPosY.Value + 3, 5, 5);
g.DrawRectangle(pen, _startPosX.Value + 82, _startPosY.Value + 3, 5, 5);
g.FillRectangle(brBlue, _startPosX.Value + 92, _startPosY.Value + 3, 5, 5);
g.DrawRectangle(pen, _startPosX.Value + 92, _startPosY.Value + 3, 5, 5);
g.FillRectangle(brBlue, _startPosX.Value + 102, _startPosY.Value + 3, 5, 5);
g.DrawRectangle(pen, _startPosX.Value + 102, _startPosY.Value + 3, 5, 5);
g.FillRectangle(brBlue, _startPosX.Value + 112, _startPosY.Value + 3, 5, 5);
g.DrawRectangle(pen, _startPosX.Value + 112, _startPosY.Value + 3, 5, 5);
//гормошка
g.DrawLine(pen, _startPosX.Value + 60, _startPosY.Value, _startPosX.Value + 62, _startPosY.Value + 3);
g.DrawLine(pen, _startPosX.Value + 62, _startPosY.Value + 3, _startPosX.Value + 65, _startPosY.Value);
g.DrawLine(pen, _startPosX.Value + 65, _startPosY.Value, _startPosX.Value + 67, _startPosY.Value + 3);
g.DrawLine(pen, _startPosX.Value + 67, _startPosY.Value + 3, _startPosX.Value + 70, _startPosY.Value);
g.DrawLine(pen, _startPosX.Value + 60, _startPosY.Value + 15, _startPosX.Value + 62, _startPosY.Value + 12);
g.DrawLine(pen, _startPosX.Value + 62, _startPosY.Value + 12, _startPosX.Value + 65, _startPosY.Value + 15);
g.DrawLine(pen, _startPosX.Value + 65, _startPosY.Value + 15, _startPosX.Value + 67, _startPosY.Value + 12);
g.DrawLine(pen, _startPosX.Value + 67, _startPosY.Value + 12, _startPosX.Value + 70, _startPosY.Value + 15);
g.DrawLine(pen, _startPosX.Value + 62, _startPosY.Value + 3, _startPosX.Value + 62, _startPosY.Value + 12);
g.DrawLine(pen, _startPosX.Value + 67, _startPosY.Value + 3, _startPosX.Value + 67, _startPosY.Value + 12);
g.DrawLine(pen, _startPosX.Value + 65, _startPosY.Value, _startPosX.Value + 65, _startPosY.Value + 15);
//двери
g.FillRectangle(additionalBrush, _startPosX.Value + 123, _startPosY.Value + 5, 5, 10);
g.DrawRectangle(pen, _startPosX.Value + 123, _startPosY.Value + 5, 5, 10);
if (accordionBus.FiveDoors)
{
g.FillRectangle(additionalBrush, _startPosX.Value + 87, _startPosY.Value + 9, 21, 5);
g.DrawRectangle(pen, _startPosX.Value + 87, _startPosY.Value + 9, 21, 5);
g.FillRectangle(additionalBrush, _startPosX.Value + 53, _startPosY.Value + 5, 5, 10);
g.DrawRectangle(pen, _startPosX.Value + 53, _startPosY.Value + 5, 5, 10);
g.FillRectangle(additionalBrush, _startPosX.Value + 27, _startPosY.Value + 9, 11, 5);
g.DrawRectangle(pen, _startPosX.Value + 27, _startPosY.Value + 9, 11, 5);
}
}
}
}
}

View File

@ -0,0 +1,265 @@
using AccordionBus.Entities;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AccordionBus.Drawnings
{
public class DrawningBus
{
/// <summary>
/// Класс-сущность
/// </summary>
public EntityBus? EntityBus { get; protected set; }
/// <summary>
/// Ширина окна
/// </summary>
private int? _pictureWeight;
/// <summary>
/// Высота окна
/// </summary>
private int? _pictureHeigth;
/// <summary>
/// Левая координата прорисовки авто
/// </summary>
protected int? _startPosX;
/// <summary>
/// Верхняя координата прорисовки авто
/// </summary>
protected int? _startPosY;
/// <summary>
/// Ширина прорисовки авто
/// </summary>
private readonly int _drawningBusWeight = 60;
/// <summary>
/// Высота прорисовки авто
/// </summary>
private readonly int _drawningBusHeigth = 20;
/// <summary>
/// Координата Х объекта
/// </summary>
/// <returns></returns>
public int? GetPosX() => _startPosX;
/// <summary>
/// Координата Y объекта
/// </summary>
/// <returns></returns>
public int? GetPosY() => _startPosY;
/// <summary>
/// Ширина объекта
/// </summary>
/// <returns></returns>
public int GetWidth() => _drawningBusWeight;
/// <summary>
/// Высота объекта
/// </summary>
/// <returns></returns>
public int GetHeigth() => _drawningBusHeigth;
/// <summary>
/// Пустой конструктор
/// </summary>
private DrawningBus()
{
_pictureWeight = null;
_pictureHeigth = null;
_startPosX = null;
_startPosY = null;
}
/// <summary>
/// Конструктор границ объекта
/// </summary>
/// <param name="drawningBusWeight">Ширина</param>
/// <param name="drawningBusHeight">Высота</param>
protected DrawningBus(int drawningBusWeight, int drawningBusHeight) : this()
{
_drawningBusWeight = drawningBusWeight;
_drawningBusHeigth = drawningBusHeight;
}
/// <summary>
/// Конструктор пораметров
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес</param>
/// <param name="bodyColor">Основной цвет</param>
public DrawningBus(int speed, double weight, Color bodyColor) : this()
{
EntityBus = new EntityBus(speed, weight, bodyColor);
}
/// <summary>
/// Установка границ поля
/// </summary>
/// <param name="width">Ширина</param>
/// <param name="heigth">Высота</param>
/// <returns>true - границы заданы, false - проверка не пройдена</returns>
public bool SetPictureSize(int width, int heigth)
{
if (width < _drawningBusWeight || heigth < _drawningBusHeigth)
{
return false;
}
_pictureWeight = width;
_pictureHeigth = heigth;
if (_startPosX.HasValue && _startPosX.Value + _drawningBusWeight > _pictureWeight)
{
_startPosX -= _startPosX.Value + _drawningBusWeight - _pictureWeight;
}
else if (_startPosY.HasValue && _startPosY.Value + _drawningBusHeigth > _pictureHeigth)
{
_startPosY -= _startPosY.Value + _drawningBusHeigth - _pictureHeigth;
}
return true;
}
/// <summary>
/// Установка позиции
/// </summary>
/// <param name="x">Координата X</param>
/// <param name="y">Координата Y</param>
public void SetPosition(int x, int y)
{
if (!_pictureHeigth.HasValue || !_pictureWeight.HasValue)
{
return;
}
if (x + _drawningBusWeight > _pictureWeight)
{
_startPosX = x - (x + _drawningBusWeight - _pictureWeight);
}
else if (x < 0)
{
_startPosX = 0;
}
else
{
_startPosX = x;
}
if (y + _drawningBusHeigth > _pictureHeigth)
{
_startPosY = y - (y + _drawningBusHeigth - _pictureHeigth);
}
else if (y < 0)
{
_startPosY = 0;
}
else
{
_startPosY = y;
}
}
/// <summary>
/// Перемещение
/// </summary>
/// <param name="direction">Направление</param>
/// <returns>true - перемещение выполнено, false - перемещение невозможнор</returns>
public bool MoveTransport(DirectionType direction)
{
if (EntityBus == null || !_startPosX.HasValue ||
!_startPosY.HasValue)
{
return false;
}
switch (direction)
{
case DirectionType.Left:
if (_startPosX.Value - EntityBus.Step > 0)
{
_startPosX -= (int)EntityBus.Step;
}
else
{
_startPosX = 0;
}
return true;
case DirectionType.Right:
if (_startPosX.Value + EntityBus.Step + _drawningBusWeight < _pictureWeight)
{
_startPosX += (int)EntityBus.Step;
}
else
{
_startPosX = _pictureWeight - _drawningBusWeight;
}
return true;
case DirectionType.Up:
if (_startPosY.Value - EntityBus.Step > 0)
{
_startPosY -= (int)EntityBus.Step;
}
else
{
_startPosY = 0;
}
return true;
case DirectionType.Down:
if (_startPosY.Value + EntityBus.Step + _drawningBusHeigth < _pictureHeigth)
{
_startPosY += (int)EntityBus.Step;
}
else
{
_startPosY = _pictureHeigth - _drawningBusHeigth;
}
return true;
default:
return false;
}
}
/// <summary>
/// Отрисовка транспорта
/// </summary>
/// <param name="g"></param>
public virtual void DrawTransport(Graphics g)
{
if (EntityBus == null || !_startPosX.HasValue ||
!_startPosY.HasValue)
{
return;
}
Pen pen = new(Color.Black);
Brush brDoor = new SolidBrush(Color.Gray);
//корпус
Brush br = new SolidBrush(EntityBus.BodyColor);
g.FillRectangle(br, _startPosX.Value, _startPosY.Value, 60, 15);
g.DrawRectangle(pen, _startPosX.Value, _startPosY.Value, 60, 15);
//колёса
Brush brWhite = new SolidBrush(Color.White);
g.FillEllipse(brWhite, _startPosX.Value + 5, _startPosY.Value + 10, 10, 10);
g.FillEllipse(brWhite, _startPosX.Value + 40, _startPosY.Value + 10, 10, 10);
g.DrawEllipse(pen, _startPosX.Value + 40, _startPosY.Value + 10, 10, 10);
g.DrawEllipse(pen, _startPosX.Value + 5, _startPosY.Value + 10, 10, 10);
//стёкла
Brush brBlue = new SolidBrush(Color.LightBlue);
g.FillRectangle(brBlue, _startPosX.Value + 2, _startPosY.Value + 3, 5, 5);
g.DrawRectangle(pen, _startPosX.Value + 2, _startPosY.Value + 3, 5, 5);
g.FillRectangle(brBlue, _startPosX.Value + 12, _startPosY.Value + 3, 5, 5);
g.DrawRectangle(pen, _startPosX.Value + 12, _startPosY.Value + 3, 5, 5);
g.FillRectangle(brBlue, _startPosX.Value + 32, _startPosY.Value + 3, 5, 5);
g.DrawRectangle(pen, _startPosX.Value + 32, _startPosY.Value + 3, 5, 5);
g.FillRectangle(brBlue, _startPosX.Value + 42, _startPosY.Value + 3, 5, 5);
g.DrawRectangle(pen, _startPosX.Value + 42, _startPosY.Value + 3, 5, 5);
//дверь
g.FillRectangle(brDoor, _startPosX.Value + 20, _startPosY.Value + 5, 5, 10);
g.DrawRectangle(pen, _startPosX.Value + 20, _startPosY.Value + 5, 5, 10);
}
}
}

View File

@ -4,25 +4,13 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AccordionBus
namespace AccordionBus.Entities
{
/// <summary>
/// класс-сущность "автобус с гормошкой"
/// </summary>
public class EntityAccordionBus
public class EntityAccordionBus : EntityBus
{
/// <summary>
/// Скорость
/// </summary>
public int Speed { get; set; }
/// <summary>
/// Вес
/// </summary>
public double Weight { get; set; }
/// <summary>
/// Основной цвет
/// </summary>
public Color BodyColor { get; set; }
/// <summary>
/// Дополнительный цвет
/// </summary>
@ -36,25 +24,17 @@ namespace AccordionBus
/// </summary>
public bool FiveDoors { get; set; }
/// <summary>
/// Шаг
/// </summary>
public double Step => Speed * 100 / Weight;
/// <summary>
/// инициализация полей объекта-класса
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">вес</param>
/// <param name="bodyColor">Основной цвет</param>
/// <param name="additionalColor">Дополнительный цвет</param>
/// <param name="threeDoors">3 двери</param>
/// <param name="fourDoors">4 двери</param>
/// <param name="onePart">1 часть</param>
/// <param name="fiveDoors">5 дверей</param>
public void Init(int speed, double weight, Color bodyColor, Color
additionalColor, bool onePart, bool fiveDoors)
public EntityAccordionBus(int speed, double weight, Color bodyColor,
Color additionalColor, bool onePart, bool fiveDoors) : base(speed, weight, bodyColor)
{
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
AdditionalColor = additionalColor;
OnePart = onePart;
FiveDoors = fiveDoors;

View File

@ -0,0 +1,43 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AccordionBus.Entities
{
/// <summary>
/// класс-сущность "Автобус"
/// </summary>
public class EntityBus
{
/// <summary>
/// Скорость
/// </summary>
public int Speed { get; set; }
/// <summary>
/// Вес
/// </summary>
public double Weight { get; set; }
/// <summary>
/// Основной цвет
/// </summary>
public Color BodyColor { get; set; }
/// <summary>
/// Шаг
/// </summary>
public double Step => Speed * 100 / Weight;
/// <summary>
/// Конструктор сущности
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес</param>
/// <param name="bodyColor">Основной цвет</param>
public EntityBus(int speed, double weight, Color bodyColor)
{
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
}
}
}

View File

@ -29,42 +29,32 @@
private void InitializeComponent()
{
pictureBoxAccordionBus = new PictureBox();
buttonCreate = new Button();
ButtonUp = new Button();
ButtonRight = new Button();
ButtonLeft = new Button();
ButtonDown = new Button();
comboBoxStrategy = new ComboBox();
buttonStrategyStap = new Button();
((System.ComponentModel.ISupportInitialize)pictureBoxAccordionBus).BeginInit();
SuspendLayout();
//
// pictureBoxAccordionBus
//
pictureBoxAccordionBus.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
pictureBoxAccordionBus.Dock = DockStyle.Fill;
pictureBoxAccordionBus.Location = new Point(0, 0);
pictureBoxAccordionBus.Name = "pictureBoxAccordionBus";
pictureBoxAccordionBus.Size = new Size(882, 453);
pictureBoxAccordionBus.Size = new Size(1182, 653);
pictureBoxAccordionBus.SizeMode = PictureBoxSizeMode.AutoSize;
pictureBoxAccordionBus.TabIndex = 0;
pictureBoxAccordionBus.TabStop = false;
//
// buttonCreate
//
buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonCreate.Location = new Point(12, 412);
buttonCreate.Name = "buttonCreate";
buttonCreate.Size = new Size(94, 29);
buttonCreate.TabIndex = 1;
buttonCreate.Text = "создать";
buttonCreate.UseVisualStyleBackColor = true;
buttonCreate.Click += ButtonCreate_Click;
//
// ButtonUp
//
ButtonUp.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
ButtonUp.BackgroundImage = Properties.Resources.buttUp;
ButtonUp.BackgroundImageLayout = ImageLayout.Stretch;
ButtonUp.ImageAlign = ContentAlignment.MiddleLeft;
ButtonUp.Location = new Point(773, 375);
ButtonUp.Location = new Point(1073, 575);
ButtonUp.Name = "ButtonUp";
ButtonUp.Size = new Size(30, 30);
ButtonUp.TabIndex = 2;
@ -77,7 +67,7 @@
ButtonRight.BackgroundImage = Properties.Resources.buttRIght;
ButtonRight.BackgroundImageLayout = ImageLayout.Stretch;
ButtonRight.ImageAlign = ContentAlignment.MiddleLeft;
ButtonRight.Location = new Point(809, 411);
ButtonRight.Location = new Point(1109, 611);
ButtonRight.Name = "ButtonRight";
ButtonRight.Size = new Size(30, 30);
ButtonRight.TabIndex = 3;
@ -90,7 +80,7 @@
ButtonLeft.BackgroundImage = Properties.Resources.buttLeft;
ButtonLeft.BackgroundImageLayout = ImageLayout.Stretch;
ButtonLeft.ImageAlign = ContentAlignment.MiddleLeft;
ButtonLeft.Location = new Point(737, 411);
ButtonLeft.Location = new Point(1037, 611);
ButtonLeft.Name = "ButtonLeft";
ButtonLeft.Size = new Size(30, 30);
ButtonLeft.TabIndex = 4;
@ -103,23 +93,44 @@
ButtonDown.BackgroundImage = Properties.Resources.buttDown;
ButtonDown.BackgroundImageLayout = ImageLayout.Stretch;
ButtonDown.ImageAlign = ContentAlignment.MiddleLeft;
ButtonDown.Location = new Point(773, 411);
ButtonDown.Location = new Point(1073, 611);
ButtonDown.Name = "ButtonDown";
ButtonDown.Size = new Size(30, 30);
ButtonDown.TabIndex = 5;
ButtonDown.UseVisualStyleBackColor = true;
ButtonDown.Click += ButtonMove_Click;
//
// comboBoxStrategy
//
comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList;
comboBoxStrategy.FormattingEnabled = true;
comboBoxStrategy.Items.AddRange(new object[] { "К центру", "К краю" });
comboBoxStrategy.Location = new Point(988, 12);
comboBoxStrategy.Name = "comboBoxStrategy";
comboBoxStrategy.Size = new Size(151, 28);
comboBoxStrategy.TabIndex = 7;
//
// buttonStrategyStap
//
buttonStrategyStap.Location = new Point(1061, 46);
buttonStrategyStap.Name = "buttonStrategyStap";
buttonStrategyStap.Size = new Size(78, 29);
buttonStrategyStap.TabIndex = 8;
buttonStrategyStap.Text = "Шаг";
buttonStrategyStap.UseVisualStyleBackColor = true;
buttonStrategyStap.Click += buttonStrategyStap_Click;
//
// FormAccordionBus
//
AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(882, 453);
ClientSize = new Size(1182, 653);
Controls.Add(buttonStrategyStap);
Controls.Add(comboBoxStrategy);
Controls.Add(ButtonDown);
Controls.Add(ButtonLeft);
Controls.Add(ButtonRight);
Controls.Add(ButtonUp);
Controls.Add(buttonCreate);
Controls.Add(pictureBoxAccordionBus);
Name = "FormAccordionBus";
StartPosition = FormStartPosition.CenterScreen;
@ -132,10 +143,11 @@
#endregion
private PictureBox pictureBoxAccordionBus;
private Button buttonCreate;
private Button ButtonUp;
private Button ButtonRight;
private Button ButtonLeft;
private Button ButtonDown;
private ComboBox comboBoxStrategy;
private Button buttonStrategyStap;
}
}

View File

@ -7,44 +7,48 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using AccordionBus.Drawnings;
using AccordionBus.MovementStrategy;
namespace AccordionBus
{
public partial class FormAccordionBus : Form
{
private DrawningAccordionBus? _drawningAccordionBus;
private DrawningBus? _drawningBus;
private AbstractStrategy? _strategy;
public DrawningBus SetBus
{
set
{
_drawningBus = value;
_drawningBus.SetPictureSize(pictureBoxAccordionBus.Width, pictureBoxAccordionBus.Height);
comboBoxStrategy.Enabled = true;
_strategy = null;
Draw();
}
}
public FormAccordionBus()
{
InitializeComponent();
_strategy = null;
}
private void Draw()
{
if (_drawningAccordionBus == null)
if (_drawningBus == null)
{
return;
}
Bitmap bmp = new(pictureBoxAccordionBus.Width, pictureBoxAccordionBus.Height);
Graphics gr = Graphics.FromImage(bmp);
_drawningAccordionBus.DrawTransport(gr);
_drawningBus.DrawTransport(gr);
pictureBoxAccordionBus.Image = bmp;
}
private void ButtonCreate_Click(object sender, EventArgs e)
{
Random random = new();
_drawningAccordionBus = new DrawningAccordionBus();
_drawningAccordionBus.Init(random.Next(100, 300), random.Next(1000, 3000),
Color.FromArgb(random.Next(0, 255), random.Next(0, 255), random.Next(0, 255)),
Color.FromArgb(random.Next(0, 255), random.Next(0, 255), random.Next(0, 255)), Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)));
_drawningAccordionBus.SetPictureSize(pictureBoxAccordionBus.Width, pictureBoxAccordionBus.Height);
_drawningAccordionBus.SetPosition(random.Next(50, 300), random.Next(50, 300));
Draw();
}
private void ButtonMove_Click(object sender, EventArgs e)
{
if (_drawningAccordionBus == null)
if (_drawningBus == null)
{
return;
}
@ -54,16 +58,16 @@ namespace AccordionBus
switch (name)
{
case "ButtonUp":
result = _drawningAccordionBus.MoveTransport(DirectionType.Up);
result = _drawningBus.MoveTransport(DirectionType.Up);
break;
case "ButtonDown":
result = _drawningAccordionBus.MoveTransport(DirectionType.Down);
result = _drawningBus.MoveTransport(DirectionType.Down);
break;
case "ButtonLeft":
result = _drawningAccordionBus.MoveTransport(DirectionType.Left);
result = _drawningBus.MoveTransport(DirectionType.Left);
break;
case "ButtonRight":
result = _drawningAccordionBus.MoveTransport(DirectionType.Right);
result = _drawningBus.MoveTransport(DirectionType.Right);
break;
}
@ -72,5 +76,32 @@ namespace AccordionBus
Draw();
}
}
private void buttonStrategyStap_Click(object sender, EventArgs e)
{
if (_drawningBus == null) return;
if (comboBoxStrategy.Enabled)
{
_strategy = comboBoxStrategy.SelectedIndex switch
{
0 => new MoveToCenter(),
1 => new MoveToBorder(),
_ => null
};
if (_strategy == null) return;
_strategy.SetData(new MoveableBus(_drawningBus), pictureBoxAccordionBus.Width, pictureBoxAccordionBus.Height);
}
if (_strategy == null) return;
comboBoxStrategy.Enabled = false;
_strategy.MakeStap();
Draw();
if (_strategy.GetStatus() == StrategyStatus.Finish)
{
comboBoxStrategy.Enabled = true;
_strategy = null;
}
}
}
}

View File

@ -0,0 +1,169 @@
namespace AccordionBus
{
partial class FormBusCollection
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
groupBoxTools = new GroupBox();
buttonRefresh = new Button();
buttonGoToCheck = new Button();
buttonRemoveBus = new Button();
maskedTextBox = new MaskedTextBox();
buttonAddAccordionBus = new Button();
buttonAddBus = new Button();
comboBoxSelectedCompany = new ComboBox();
pictureBox = new PictureBox();
groupBoxTools.SuspendLayout();
((System.ComponentModel.ISupportInitialize)pictureBox).BeginInit();
SuspendLayout();
//
// groupBoxTools
//
groupBoxTools.Controls.Add(buttonRefresh);
groupBoxTools.Controls.Add(buttonGoToCheck);
groupBoxTools.Controls.Add(buttonRemoveBus);
groupBoxTools.Controls.Add(maskedTextBox);
groupBoxTools.Controls.Add(buttonAddAccordionBus);
groupBoxTools.Controls.Add(buttonAddBus);
groupBoxTools.Controls.Add(comboBoxSelectedCompany);
groupBoxTools.Dock = DockStyle.Right;
groupBoxTools.Location = new Point(948, 0);
groupBoxTools.Name = "groupBoxTools";
groupBoxTools.Size = new Size(234, 653);
groupBoxTools.TabIndex = 0;
groupBoxTools.TabStop = false;
groupBoxTools.Text = "Инструменты";
//
// buttonRefresh
//
buttonRefresh.Location = new Point(15, 554);
buttonRefresh.Name = "buttonRefresh";
buttonRefresh.Size = new Size(207, 54);
buttonRefresh.TabIndex = 6;
buttonRefresh.Text = "Обновить";
buttonRefresh.UseVisualStyleBackColor = true;
buttonRefresh.Click += buttonRefresh_Click;
//
// buttonGoToCheck
//
buttonGoToCheck.Location = new Point(15, 443);
buttonGoToCheck.Name = "buttonGoToCheck";
buttonGoToCheck.Size = new Size(207, 54);
buttonGoToCheck.TabIndex = 5;
buttonGoToCheck.Text = "Передать на тесты";
buttonGoToCheck.UseVisualStyleBackColor = true;
buttonGoToCheck.Click += buttonGoToCheck_Click;
//
// buttonRemoveBus
//
buttonRemoveBus.Location = new Point(15, 310);
buttonRemoveBus.Name = "buttonRemoveBus";
buttonRemoveBus.Size = new Size(207, 54);
buttonRemoveBus.TabIndex = 4;
buttonRemoveBus.Text = "Удалить автобус";
buttonRemoveBus.UseVisualStyleBackColor = true;
buttonRemoveBus.Click += buttonRemoveBus_Click;
//
// maskedTextBox
//
maskedTextBox.Location = new Point(15, 277);
maskedTextBox.Mask = "00";
maskedTextBox.Name = "maskedTextBox";
maskedTextBox.Size = new Size(207, 27);
maskedTextBox.TabIndex = 3;
maskedTextBox.ValidatingType = typeof(int);
//
// buttonAddAccordionBus
//
buttonAddAccordionBus.Location = new Point(15, 155);
buttonAddAccordionBus.Name = "buttonAddAccordionBus";
buttonAddAccordionBus.Size = new Size(207, 54);
buttonAddAccordionBus.TabIndex = 2;
buttonAddAccordionBus.Text = "Добавить автобус с гормошкой";
buttonAddAccordionBus.UseVisualStyleBackColor = true;
buttonAddAccordionBus.Click += buttonAddAccordionBus_Click;
//
// buttonAddBus
//
buttonAddBus.Location = new Point(15, 95);
buttonAddBus.Name = "buttonAddBus";
buttonAddBus.Size = new Size(207, 54);
buttonAddBus.TabIndex = 1;
buttonAddBus.Text = "Добавить автобус";
buttonAddBus.UseVisualStyleBackColor = true;
buttonAddBus.Click += buttonAddBus_Click;
//
// comboBoxSelectedCompany
//
comboBoxSelectedCompany.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
comboBoxSelectedCompany.DropDownStyle = ComboBoxStyle.DropDownList;
comboBoxSelectedCompany.FormattingEnabled = true;
comboBoxSelectedCompany.Items.AddRange(new object[] { "Станция" });
comboBoxSelectedCompany.Location = new Point(15, 40);
comboBoxSelectedCompany.Name = "comboBoxSelectedCompany";
comboBoxSelectedCompany.Size = new Size(207, 28);
comboBoxSelectedCompany.TabIndex = 0;
comboBoxSelectedCompany.SelectedIndexChanged += comboBoxSelectedCompany_SelectedIndexChanged;
//
// pictureBox
//
pictureBox.Dock = DockStyle.Fill;
pictureBox.Location = new Point(0, 0);
pictureBox.Name = "pictureBox";
pictureBox.Size = new Size(948, 653);
pictureBox.TabIndex = 1;
pictureBox.TabStop = false;
//
// FormBusCollection
//
AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(1182, 653);
Controls.Add(pictureBox);
Controls.Add(groupBoxTools);
Name = "FormBusCollection";
StartPosition = FormStartPosition.CenterScreen;
Text = "Коллекция автобусов";
groupBoxTools.ResumeLayout(false);
groupBoxTools.PerformLayout();
((System.ComponentModel.ISupportInitialize)pictureBox).EndInit();
ResumeLayout(false);
}
#endregion
private GroupBox groupBoxTools;
private ComboBox comboBoxSelectedCompany;
private Button buttonAddBus;
private Button buttonAddAccordionBus;
private PictureBox pictureBox;
private Button buttonRemoveBus;
private MaskedTextBox maskedTextBox;
private Button buttonRefresh;
private Button buttonGoToCheck;
}
}

View File

@ -0,0 +1,131 @@
using AccordionBus.CollectionGenericObjects;
using AccordionBus.Drawnings;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics.Eventing.Reader;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace AccordionBus
{
public partial class FormBusCollection : Form
{
private AbstractCompany? _company;
public FormBusCollection()
{
InitializeComponent();
}
private void comboBoxSelectedCompany_SelectedIndexChanged(object sender, EventArgs e)
{
switch (comboBoxSelectedCompany.Text)
{
case "Станция":
_company = new BusStation(pictureBox.Width, pictureBox.Height, new MassiveGenericObjects<DrawningBus>());
pictureBox.Image = _company.Show();
break;
}
}
private void CreateObject(string type)
{
if (_company == null) return;
Random random = new();
DrawningBus _drawningBus;
switch (type)
{
case nameof(DrawningBus):
_drawningBus = new DrawningBus(random.Next(100, 300), random.Next(1000, 3000), GetColor(random));
_drawningBus.SetPictureSize(pictureBox.Width, pictureBox.Height);
break;
case nameof(DrawningAccordionBus):
_drawningBus = new DrawningAccordionBus(random.Next(100, 300), random.Next(1000, 3000),
GetColor(random), GetColor(random),
Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)));
_drawningBus.SetPictureSize(pictureBox.Width, pictureBox.Height);
break;
default:
return;
}
if (_company + _drawningBus)
{
MessageBox.Show("Объект добавлен");
pictureBox.Image = _company.Show();
}
else
{
MessageBox.Show("Объект не удалось добавить");
}
}
private static Color GetColor(Random rnd)
{
Color color = Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256));
ColorDialog dialog = new();
if (dialog.ShowDialog() == DialogResult.OK)
{
color = dialog.Color;
}
return color;
}
private void buttonAddBus_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningBus));
private void buttonAddAccordionBus_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningAccordionBus));
private void buttonRemoveBus_Click(object sender, EventArgs e)
{
if (_company == null || string.IsNullOrEmpty(maskedTextBox.Text)) return;
if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) return;
int pos = Convert.ToInt32(maskedTextBox.Text);
if (_company - pos)
{
MessageBox.Show("Объект удалён");
pictureBox.Image = _company.Show();
}
else
{
MessageBox.Show("Не удалось удалить объект");
}
}
private void buttonGoToCheck_Click(object sender, EventArgs e)
{
if (_company == null) return;
DrawningBus? bus = null;
int counter = 100;
while (bus == null || counter > 0)
{
bus = _company.GetRandomObject();
counter--;
}
if (bus == null) return;
FormAccordionBus form = new()
{
SetBus = bus
};
form.ShowDialog();
}
private void buttonRefresh_Click(object sender, EventArgs e)
{
if (_company == null) return;
pictureBox.Image = _company.Show();
}
}
}

View File

@ -0,0 +1,120 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>

View File

@ -0,0 +1,125 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AccordionBus.MovementStrategy
{
public abstract class AbstractStrategy
{
/// <summary>
/// Объект
/// </summary>
private IMoveableObjects? _moveableObject;
/// <summary>
/// Статус стратегии
/// </summary>
private StrategyStatus? _state = StrategyStatus.NotInit;
/// <summary>
/// Ширина
/// </summary>
protected int FieldWidth { get; private set; }
/// <summary>
/// Высота
/// </summary>
protected int FieldHeight { get; private set; }
/// <summary>
/// Получение статуса
/// </summary>
/// <returns></returns>
public StrategyStatus? GetStatus() { return _state; }
/// <summary>
/// Инициализация полей
/// </summary>
/// <param name="moveableObject"></param>
/// <param name="width"></param>
/// <param name="height"></param>
public void SetData(IMoveableObjects moveableObject, int width, int height)
{
if (moveableObject == null)
{
_state = StrategyStatus.NotInit;
return;
}
_state = StrategyStatus.InProgress;
_moveableObject = moveableObject;
FieldWidth = width;
FieldHeight = height;
}
/// <summary>
/// Напавление
/// </summary>
public void MakeStap()
{
if (_state != StrategyStatus.InProgress) return;
if (IsTargetDestination())
{
_state = StrategyStatus.Finish;
return;
}
MoveToTarget();
}
/// <summary>
/// Шаг влево
/// </summary>
/// <returns></returns>
protected bool MoveLeft() => MoveTo(MovementDirection.Left);
/// <summary>
/// Шаг вправо
/// </summary>
/// <returns></returns>
protected bool MoveRight() => MoveTo(MovementDirection.Right);
/// <summary>
/// Шаг вверх
/// </summary>
/// <returns></returns>
protected bool MoveUp() => MoveTo(MovementDirection.Up);
/// <summary>
/// Шаг вниз
/// </summary>
/// <returns></returns>
protected bool MoveDown() => MoveTo(MovementDirection.Down);
/// <summary>
/// Получение параметров
/// </summary>
/// <returns></returns>
protected ObjectParameters? GetObjectParameters() => _moveableObject?.GetObjectPosition;
/// <summary>
/// Получение шага
/// </summary>
/// <returns></returns>
protected int? GetStap()
{
if(_state != StrategyStatus.InProgress)
{
return null;
}
return _moveableObject?.GetStep;
}
/// <summary>
/// Шаг к цели
/// </summary>
protected abstract void MoveToTarget();
/// <summary>
/// Достижение цели
/// </summary>
/// <returns></returns>
protected abstract bool IsTargetDestination();
/// <summary>
/// Сделать шаг
/// </summary>
/// <param name="movementDirection"></param>
/// <returns></returns>
private bool MoveTo(MovementDirection movementDirection)
{
if(_state != StrategyStatus.InProgress)
{
return false;
}
return _moveableObject?.TryMoveObject(movementDirection) ?? false;
}
}
}

View File

@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AccordionBus.MovementStrategy
{
/// <summary>
/// Интерфейс для работы с пперемещаемым объектом
/// </summary>
public interface IMoveableObjects
{
/// <summary>
/// Получение координаты объекта
/// </summary>
ObjectParameters? GetObjectPosition { get; }
/// <summary>
/// Шаг объекта
/// </summary>
int GetStep { get; }
/// <summary>
/// попытка переместить объект в указаном направлении
/// </summary>
/// <param name="direction">Направление</param>
/// <returns>true - перемещение удалось false - перемещение невозможно</returns>
bool TryMoveObject(MovementDirection direction);
}
}

View File

@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AccordionBus.MovementStrategy
{
public class MoveToBorder : AbstractStrategy
{
protected override bool IsTargetDestination()
{
ObjectParameters? objParams = GetObjectParameters();
if (objParams == null)
{
return false;
}
return objParams.RightBorder() == FieldWidth && objParams.DownBorder() == FieldHeight;
}
protected override void MoveToTarget()
{
ObjectParameters? objParams = GetObjectParameters();
if (objParams == null)
{
return;
}
if (objParams.RightBorder() < FieldWidth) MoveRight();
if (objParams.DownBorder() < FieldHeight) MoveDown();
}
}
}

View File

@ -0,0 +1,56 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AccordionBus.MovementStrategy
{
public class MoveToCenter : AbstractStrategy
{
protected override bool IsTargetDestination()
{
ObjectParameters? objParams = GetObjectParameters();
if (objParams == null)
{
return false;
}
return objParams.ObjectMiddleHorizontal() - GetStap() <= FieldWidth / 2
&& objParams.ObjectMiddleHorizontal() + GetStap() >= FieldWidth / 2 &&
objParams.ObjectMiddleVertical() - GetStap() <= FieldHeight / 2
&& objParams.ObjectMiddleVertical() + GetStap() >= FieldHeight / 2;
}
protected override void MoveToTarget()
{
ObjectParameters? objParams = GetObjectParameters();
if (objParams == null)
{
return;
}
int diffX = objParams.ObjectMiddleHorizontal() - FieldWidth / 2;
if (Math.Abs(diffX) > GetStap())
{
if (diffX > 0)
{
MoveLeft();
}
else
{
MoveRight();
}
}
int diffY = objParams.ObjectMiddleVertical() - FieldHeight / 2;
if (Math.Abs(diffY) > GetStap())
{
if (diffY > 0)
{
MoveUp();
}
else
{
MoveDown();
}
}
}
}
}

View File

@ -0,0 +1,67 @@
using AccordionBus.Drawnings;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AccordionBus.MovementStrategy
{
/// <summary>
/// Класс-реализация IMoveableObjects с использованием DrawningBus
/// </summary>
public class MoveableBus : IMoveableObjects
{
/// <summary>
/// Поле-объект DrawningBus
/// </summary>
private readonly DrawningBus? _car;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="car">Объект класса DrawningBus</param>
public MoveableBus(DrawningBus? car)
{
_car = car;
}
public ObjectParameters? GetObjectPosition
{
get
{
if (_car == null || _car.EntityBus == null || _car.GetPosX() == null || _car.GetPosY() == null)
{
return null;
}
return new ObjectParameters(_car.GetPosX().Value, _car.GetPosY().Value, _car.GetWidth(), _car.GetHeigth());
}
}
public int GetStep => (int)(_car?.EntityBus?.Step ?? 0);
public bool TryMoveObject(MovementDirection direction)
{
if (_car == null || _car.EntityBus == null)
{
return false;
}
return _car.MoveTransport(GetDirectionType(direction));
}
/// <summary>
/// Конвертация из MovementDirection в DirectionType
/// </summary>
/// <param name="direction"></param>
/// <returns></returns>
private static DirectionType GetDirectionType(MovementDirection direction)
{
return direction switch
{
MovementDirection.Left => DirectionType.Left,
MovementDirection.Right => DirectionType.Right,
MovementDirection.Up => DirectionType.Up,
MovementDirection.Down => DirectionType.Down,
_ => DirectionType.Unknow,
};
}
}
}

View File

@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AccordionBus.MovementStrategy
{
public enum MovementDirection
{
/// <summary>
/// Вверх
/// </summary>
Up = 1,
/// <summary>
/// Вниз
/// </summary>
Down = 2,
/// <summary>
/// Влево
/// </summary>
Left = 3,
/// <summary>
/// Вправо
/// </summary>
Right = 4
}
}

View File

@ -0,0 +1,75 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AccordionBus.MovementStrategy
{
/// <summary>
/// Параметры-координаты объекта
/// </summary>
public class ObjectParameters
{
/// <summary>
/// Координата Х
/// </summary>
private readonly int _x;
/// <summary>
/// Координата Y
/// </summary>
private readonly int _y;
/// <summary>
/// Ширина
/// </summary>
private readonly int _width;
/// <summary>
/// Высота
/// </summary>
private readonly int _height;
/// <summary>
/// Левая граница
/// </summary>
/// <returns></returns>
public int LeftBorder() => _x;
/// <summary>
/// Верхняя граница
/// </summary>
/// <returns></returns>
public int TopBorder() => _y;
/// <summary>
/// Правая граница
/// </summary>
/// <returns></returns>
public int RightBorder() => _x + _width;
/// <summary>
/// Нижняя граница
/// </summary>
/// <returns></returns>
public int DownBorder() => _y + _height;
/// <summary>
/// Середина объекта
/// </summary>
/// <returns></returns>
public int ObjectMiddleHorizontal() => _x + _width / 2;
/// <summary>
/// Середина объекта
/// </summary>
/// <returns></returns>
public int ObjectMiddleVertical() => _y + _height / 2;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="x">Координата Х</param>
/// <param name="y">Координата Y</param>
/// <param name="width"Ширина></param>
/// <param name="height">Высота</param>
public ObjectParameters(int x, int y, int width, int height)
{
_x = x;
_y = y;
_width = width;
_height = height;
}
}
}

View File

@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AccordionBus.MovementStrategy
{
public enum StrategyStatus
{
/// <summary>
/// всё готово к началу
/// </summary>
NotInit,
/// <summary>
/// В процессе
/// </summary>
InProgress,
/// <summary>
/// Завершено
/// </summary>
Finish
}
}

View File

@ -11,7 +11,7 @@ namespace AccordionBus
// To customize application configuration such as set high DPI settings or default font,
// see https://aka.ms/applicationconfiguration.
ApplicationConfiguration.Initialize();
Application.Run(new FormAccordionBus());
Application.Run(new FormBusCollection());
}
}
}