First stage of lab work

This commit is contained in:
Zara28 2022-10-09 13:17:48 +04:00
parent da0640612c
commit 396b2d8f1e
2 changed files with 83 additions and 45 deletions

View File

@ -49,10 +49,6 @@
_map = map;
}
/// <summary>
/// Перегрузка оператора сложения
/// </summary>
@ -92,13 +88,9 @@
public Bitmap ShowOnMap()
{
Shaking();
for (int i = 0; i < _setMachines.Count; i++)
foreach (var machine in _setMachines.GetMachine())
{
var car = _setMachines.Get(i);
if (car != null)
{
return _map.CreateMap(_pictureWidth, _pictureHeight, car);
}
return _map.CreateMap(_pictureWidth, _pictureHeight, machine);
}
return new(_pictureWidth, _pictureHeight);
}
@ -120,14 +112,15 @@
/// </summary>
private void Shaking()
{
int j = _setMachines.Count - 1;
for (int i = 0; i < _setMachines.Count; i++)
{
if (_setMachines.Get(i) == null)
if (_setMachines[i] == null)
{
for (; j > i; j--)
{
var car = _setMachines.Get(j);
var car = _setMachines[j];
if (car != null)
{
_setMachines.Insert(car, i);
@ -141,6 +134,7 @@
}
}
}
}
/// <summary>
/// Метод отрисовки фона
@ -174,18 +168,18 @@
int currentWidth = 0;
int currentHeight = 0;
for (int i = 0; i < _setMachines.Count; i++)
foreach (var machine in _setMachines.GetMachine())
{
int dop = 0;
if(currentWidth > 0 && currentHeight > 0)
if (currentWidth > 0 && currentHeight > 0)
{
dop = 30;
}
_setMachines.Get(i)?.SetObject(currentWidth * _placeSizeWidth + dop,
machine.SetObject(currentWidth * _placeSizeWidth + dop,
currentHeight * _placeSizeHeight + dop,
_pictureWidth, _pictureHeight);
_setMachines.Get(i)?.DrawningObject(g);
machine?.DrawningObject(g);
if (currentWidth < width - 1)
{
currentWidth++;
@ -196,8 +190,9 @@
currentHeight++;
}
if (currentHeight > height) return;
}
}
}
}

View File

@ -8,13 +8,14 @@
where T : class
{
/// <summary>
/// Массив объектов, которые храним
/// Список объектов, которые храним
/// </summary>
private readonly T[] _places;
private readonly List<T> _places;
private readonly int _maxCount;
/// <summary>
/// Количество объектов в массиве
/// Количество объектов в списке
/// </summary>
public int Count => _places.Length;
public int Count => _places.Count;
private int BusyPlaces = 0;
/// <summary>
/// Конструктор
@ -22,7 +23,9 @@
/// <param name="count"></param>
public SetMachineGeneric(int count)
{
_places = new T[count];
_maxCount = count;
_places = new List<T>();
}
/// <summary>
/// Добавление объекта в набор
@ -31,7 +34,8 @@
/// <returns></returns>
public int Insert(T machine)
{
return Insert(machine, 0);
if (Count + 1 <= _maxCount) return Insert(machine, 0);
else return -1;
}
/// <summary>
/// Добавление объекта в набор на конкретную позицию
@ -41,21 +45,13 @@
/// <returns></returns>
public int Insert(T machine, int position)
{
if (position < 0 || position >= _places.Length || BusyPlaces == _places.Length) return -1;
BusyPlaces++;
while (_places[position] != null)
if (position >= _maxCount && position < 0)
{
for (int i = _places.Length - 1; i > 0; --i)
{
if (_places[i] == null && _places[i - 1] != null)
{
_places[i] = _places[i - 1];
_places[i - 1] = null;
}
}
return -1;
}
_places[position] = machine;
_places.Insert(position, machine);
return position;
}
/// <summary>
@ -65,21 +61,68 @@
/// <returns></returns>
public T Remove(int position)
{
if (position < 0 || position >= _places.Length) return null;
T deletemashine = _places[position];
_places[position] = null;
return deletemashine;
if (position < _maxCount && position >= 0)
{
if (_places.ElementAt(position) != null)
{
T result = _places.ElementAt(position);
_places.RemoveAt(position);
return result;
}
return null;
}
return null;
}
/// <summary>
/// Получение объекта из набора по позиции
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public T Get(int position)
public T this[int position]
{
if (position < 0 || position >= _places.Length) return null;
else if (_places[position] == null) return null;
return _places[position];
get
{
if (position < _maxCount && position >= 0)
{
return _places.ElementAt(position);
}
return null;
}
set
{
if (position < _maxCount && position >= 0)
{
Insert(this[position], position);
}
}
}
/// <summary>
/// Проход по набору до первого пустого
/// </summary>
/// <returns></returns>
public IEnumerable<T> GetMachine()
{
foreach (var machine in _places)
{
if (machine != null)
{
yield return machine;
}
else
{
yield break;
}
}
}
}
}