Лабораторная работа 3.1(new)

This commit is contained in:
MatveyPetrov 2024-02-16 16:52:30 +04:00
parent fa5d91263c
commit 7fafb1a5a1
7 changed files with 128 additions and 135 deletions

View File

@ -13,29 +13,82 @@ public class AircraftSharingService : AbstractCompany
{
}
private int? _startPosX;
private int? _startPosY;
private int? ObjPositionX;
private int? ObjPositionY;
private void DrawPlace(Graphics g)
{
Pen pen = new(Color.Black);
g.DrawRectangle(pen, _startPosX.Value, _startPosY.Value, _placeSizeWidth, _placeSizeHeight);
}
private void DrawPosition(Graphics g)
{
Pen pen = new(Color.Black);
g.DrawRectangle(pen, _startPosX.Value, _startPosY.Value, 2, 2);
}
protected override void DrawBackGround(Graphics g)
{
Pen pen = new(Color.Black, 3);
for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++)
_startPosX = 0;
_startPosY = 0;
for (int x = 0; x <= _pictureWidth; x = x + _placeSizeWidth)
{
for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; j++)
if ((_pictureWidth - _placeSizeWidth) > _startPosX)
{
g.DrawLine(pen, i * _placeSizeWidth, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight);
for (int y = 0; y <= _pictureHeight; y = y + _placeSizeHeight)
{
if ((_pictureHeight - _placeSizeHeight) > _startPosY)
{
DrawPlace(g);
_startPosY = _startPosY + _placeSizeHeight;
}
}
_startPosX = _startPosX + _placeSizeWidth;
_startPosY = 0;
}
g.DrawLine(pen, i * _placeSizeWidth, 0, i * _placeSizeWidth, _pictureHeight / _placeSizeHeight * _placeSizeHeight);
}
}
protected override void SetObjectPosition(Graphics g)
{
for(int i = 0; i < _collection?.Count; i++)
_startPosX = 5;
_startPosY = 5;
int i = 0;
for (int x = 0; x <= _pictureWidth; x = x + _placeSizeWidth)
{
if ((_pictureWidth - _placeSizeWidth) > _startPosX)
{
DrawningAircraft airplane = _collection?.Get(i);
if (airplane != null)
{
int inRow = _pictureWidth / _placeSizeWidth;
airplane.SetPosition(((inRow - 1 - (i % inRow)) * _placeSizeWidth), ((_collection.Count / inRow - 1 - i / inRow) * _placeSizeHeight));
airplane.DrawTransport(g);
ObjPositionX = _startPosX;
for (int y = 0; y <= _pictureHeight; y = y + _placeSizeHeight)
{
if ((_pictureHeight - _placeSizeHeight) > _startPosY)
{
ObjPositionY = _startPosY;
if (i < (_collection?.Count))
{
DrawningAircraft obj = _collection.Get(i);
if (obj != null)
{
obj.SetpictureSize(_pictureWidth, _pictureHeight);
obj.SetPosition(Convert.ToInt32(ObjPositionX), Convert.ToInt32(ObjPositionY));
}
i++;
}
_startPosY = _startPosY + _placeSizeHeight;
}
}
_startPosX = _startPosX + _placeSizeWidth;
_startPosY = 5;
}
}
}

View File

@ -120,8 +120,10 @@ public class DrawningAircraft
/// <returns>true - границы заданы, false - проверка не пройдена, нельзя разместить объект в этих размерах</returns>
public bool SetpictureSize(int width, int height)
{
// TODO провека, что объект "влезает" в размеры поля
// если влезает, сохраняем границы и корректируем позицию объекта, если она была установлена
if (width <= _drawningMilitaryAircraftWidth || height <= _drawingMilitaryAircraftHeight)
{
return false;
}
_pictureWidth = width;
_pictureHeight = height;
return true;
@ -138,8 +140,12 @@ public class DrawningAircraft
{
return;
}
//TODO если при установке объекта в эти координаты, он будет "выходить" за границы формы
// то надо изменить координаты, чтобы он оставался в этих границах
if (x > _pictureWidth || x < 0 || y > _pictureHeight || y < 0)
{
x = 0;
y = 0;
}
_startPosX = x;
_startPosY = y;
}
@ -169,24 +175,22 @@ public class DrawningAircraft
return true;
//Вправо
case DirectionType.Right:
#pragma warning disable CS8629 // Тип значения, допускающего NULL, может быть NULL.
if (_startPosX.Value + EntityAircraft.Step <= _pictureWidth.Value)
{
if (_startPosX + 98 <= _pictureWidth)
_startPosX += (int)EntityAircraft.Step;
if (_startPosX.Value + _drawningMilitaryAircraftWidth + EntityAircraft.Step < _pictureWidth)
{
_startPosX += (int)EntityAircraft.Step;
}
#pragma warning restore CS8629 // Тип значения, допускающего NULL, может быть NULL.
return true;
//Влево
case DirectionType.Down:
#pragma warning disable CS8629 // Тип значения, допускающего NULL, может быть NULL.
if (_startPosY.Value + EntityAircraft.Step <= _pictureHeight.Value)
if (_startPosY.Value + _drawingMilitaryAircraftHeight + EntityAircraft.Step < _pictureHeight)
{
if (_startPosY + 90 <= _pictureHeight)
_startPosY += (int)EntityAircraft.Step;
_startPosY += (int)EntityAircraft.Step;
}
#pragma warning restore CS8629 // Тип значения, допускающего NULL, может быть NULL.
return true;
default:
return false;

View File

@ -44,33 +44,6 @@ public class DrawningMilitaryAircraft : DrawningAircraft
Pen pen = new(Color.Black);
Brush abr = new SolidBrush(airCraft.AdditionalColor);
Brush br = new SolidBrush(airCraft.BodyColor);
//крыло
g.FillRectangle(br, _startPosX.Value + 40, _startPosY.Value, 10, 80);
g.DrawRectangle(pen, _startPosX.Value + 40, _startPosY.Value, 10, 80);
//хвост
g.FillRectangle(br, _startPosX.Value + 5, _startPosY.Value + 27, 10, 5);
g.DrawRectangle(pen, _startPosX.Value + 5, _startPosY.Value + 27, 10, 5);
g.FillRectangle(br, _startPosX.Value + 5, _startPosY.Value + 47, 10, 5);
g.DrawRectangle(pen, _startPosX.Value + 5, _startPosY.Value + 47, 10, 5);
//Границы Самолета
g.FillRectangle(br, _startPosX.Value + 10, _startPosY.Value + 30, 50, 20);
g.DrawRectangle(pen, _startPosX.Value + 10, _startPosY.Value + 30, 50, 20);
//Хвост (центр)
g.FillRectangle(br, _startPosX.Value + 2, _startPosY.Value + 37, 10, 5);
g.DrawRectangle(pen, _startPosX.Value + 2, _startPosY.Value + 37, 10, 5);
//Кабина
g.DrawLine(pen, _startPosX.Value + 60, _startPosY.Value + 40, _startPosX.Value + 60, _startPosY.Value + 25);
g.DrawLine(pen, _startPosX.Value + 60, _startPosY.Value + 25, _startPosX.Value + 80, _startPosY.Value + 40);
g.DrawLine(pen, _startPosX.Value + 80, _startPosY.Value + 40, _startPosX.Value + 60, _startPosY.Value + 55);
g.DrawLine(pen, _startPosX.Value + 60, _startPosY.Value + 50, _startPosX.Value + 60, _startPosY.Value + 55);
base.DrawTransport(g);
//Ракеты

View File

@ -43,7 +43,7 @@
pictureBoxMilitaryAircraft.Dock = DockStyle.Fill;
pictureBoxMilitaryAircraft.Location = new Point(0, 0);
pictureBoxMilitaryAircraft.Name = "pictureBoxMilitaryAircraft";
pictureBoxMilitaryAircraft.Size = new Size(953, 651);
pictureBoxMilitaryAircraft.Size = new Size(893, 612);
pictureBoxMilitaryAircraft.TabIndex = 0;
pictureBoxMilitaryAircraft.TabStop = false;
//
@ -54,7 +54,7 @@
buttonLeft.BackgroundImage = Properties.Resources.Left;
buttonLeft.BackgroundImageLayout = ImageLayout.Stretch;
buttonLeft.ForeColor = SystemColors.ControlLightLight;
buttonLeft.Location = new Point(824, 610);
buttonLeft.Location = new Point(764, 571);
buttonLeft.Name = "buttonLeft";
buttonLeft.Size = new Size(35, 35);
buttonLeft.TabIndex = 2;
@ -68,7 +68,7 @@
buttonUp.BackgroundImage = Properties.Resources.Up;
buttonUp.BackgroundImageLayout = ImageLayout.Stretch;
buttonUp.ForeColor = SystemColors.ControlLightLight;
buttonUp.Location = new Point(865, 569);
buttonUp.Location = new Point(805, 530);
buttonUp.Name = "buttonUp";
buttonUp.Size = new Size(35, 35);
buttonUp.TabIndex = 3;
@ -82,7 +82,7 @@
buttonRight.BackgroundImage = Properties.Resources.Right;
buttonRight.BackgroundImageLayout = ImageLayout.Stretch;
buttonRight.ForeColor = SystemColors.ControlLightLight;
buttonRight.Location = new Point(906, 610);
buttonRight.Location = new Point(846, 571);
buttonRight.Name = "buttonRight";
buttonRight.Size = new Size(35, 35);
buttonRight.TabIndex = 4;
@ -96,7 +96,7 @@
buttonDown.BackgroundImage = Properties.Resources.Down;
buttonDown.BackgroundImageLayout = ImageLayout.Stretch;
buttonDown.ForeColor = SystemColors.ControlLightLight;
buttonDown.Location = new Point(865, 610);
buttonDown.Location = new Point(805, 571);
buttonDown.Name = "buttonDown";
buttonDown.Size = new Size(35, 35);
buttonDown.TabIndex = 5;
@ -109,7 +109,7 @@
comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList;
comboBoxStrategy.FormattingEnabled = true;
comboBoxStrategy.Items.AddRange(new object[] { "К ценру", "К краю" });
comboBoxStrategy.Location = new Point(823, 12);
comboBoxStrategy.Location = new Point(763, 12);
comboBoxStrategy.Name = "comboBoxStrategy";
comboBoxStrategy.Size = new Size(121, 23);
comboBoxStrategy.TabIndex = 7;
@ -117,7 +117,7 @@
// buttonStrategyStep
//
buttonStrategyStep.Anchor = AnchorStyles.Top | AnchorStyles.Right;
buttonStrategyStep.Location = new Point(870, 41);
buttonStrategyStep.Location = new Point(810, 41);
buttonStrategyStep.Name = "buttonStrategyStep";
buttonStrategyStep.Size = new Size(75, 23);
buttonStrategyStep.TabIndex = 8;
@ -129,7 +129,7 @@
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(953, 651);
ClientSize = new Size(893, 612);
Controls.Add(buttonStrategyStep);
Controls.Add(comboBoxStrategy);
Controls.Add(buttonDown);

View File

@ -60,47 +60,6 @@ namespace ProectMilitaryAircraft
pictureBoxMilitaryAircraft.Image = bmp;
}
private void CreateObj(string type)
{
Random rnd = new();
switch (type)
{
case nameof(DrawningAircraft):
_DrawningAircraft = new DrawningAircraft(rnd.Next(100, 300), rnd.Next(1000, 3000),
Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)), pictureBoxMilitaryAircraft.Width, pictureBoxMilitaryAircraft.Height);
break;
case nameof(DrawningMilitaryAircraft):
_DrawningAircraft = new DrawningMilitaryAircraft(rnd.Next(100, 300), rnd.Next(1000, 3000),
Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)),
Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)),
Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, 2)), pictureBoxMilitaryAircraft.Width, pictureBoxMilitaryAircraft.Height);
break;
default:
return;
}
_DrawningAircraft.SetpictureSize(pictureBoxMilitaryAircraft.Width, pictureBoxMilitaryAircraft.Height);
_DrawningAircraft.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100));
_AbstractStrategy = null;
comboBoxStrategy.Enabled = true;
Draw();
}
/// <summary>
/// Обработка кнопки нажатия "Создать ваенный самолет"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonCreateMA_Click(object sender, EventArgs e) => CreateObj(nameof(DrawningMilitaryAircraft));
/// <summary>
/// Обработка кнопки нажатия "Создать самолет"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonCreateA_Click(object sender, EventArgs e) => CreateObj(nameof(DrawningAircraft));
private void ButtonMove_Click(object sender, EventArgs e)
{
if (_DrawningAircraft == null)

View File

@ -10,47 +10,36 @@ public class MoveToBorder : AbstractStrategys
{
protected override bool IsTrgetDestansion()
{
var objParams = GetObjectParameters;
ObjectParameters? objParams = GetObjectParameters;
if (objParams == null)
{
return false;
}
return objParams.ObjectMiddleHorizontal - GetStep() <= FieldWidth / 2 &&
objParams.ObjectMiddleHorizontal + GetStep() >= FieldWidth / 2 &&
objParams.ObjectMiddleVertical - GetStep() <= FieldHeight / 2 &&
objParams.ObjectMiddleVertical + GetStep() >= FieldHeight / 2;
return objParams.RightBorder + GetStep() >= FieldWidth && objParams.DownBorder + GetStep() >= FieldHeight;
}
protected override void MoveToTarget()
{
var objParams = GetObjectParameters;
ObjectParameters? objParams = GetObjectParameters;
if (objParams == null)
{
return;
}
var diffX = objParams.RightBorder - FieldWidth;
int diffX = objParams.RightBorder - FieldWidth;
if (Math.Abs(diffX) > GetStep())
{
if (diffX > 0)
{
MoveLeft();
}
else
if (diffX < 0)
{
MoveRight();
}
}
var diffY = objParams.DownBorder - FieldHeight;
if (Math.Abs(diffY) > GetStep())
{
if (diffY > 0)
int diffY = objParams.DownBorder - FieldHeight;
if (Math.Abs(diffY) > GetStep())
{
MoveUp();
}
else
{
MoveDown();
if (diffY < 0)
{
MoveDown();
}
}
}
}

View File

@ -23,18 +23,33 @@ public class MoveToCenter : AbstractStrategys
protected override void MoveToTarget()
{
ObjectParameters? objParams = GetObjectParameters;
if(objParams == null) { return;}
int diffx = objParams.ObjectMiddleHorizontal - FieldWidth / 2;
if (Math.Abs(diffx) > GetStep())
if (objParams == null)
{
if (diffx > 0) { MoveLeft(); } else { MoveRight(); }
return;
}
int diffy = objParams.ObjectMiddleVertical - FieldHeight / 2;
if (Math.Abs(diffy) > GetStep())
int diffX = objParams.ObjectMiddleHorizontal - FieldWidth / 2;
if (Math.Abs(diffX) > GetStep())
{
if (diffy > 0) { MoveUp(); } else { MoveDown(); }
if (diffX > 0)
{
MoveLeft();
}
else
{
MoveRight();
}
}
int diffY = objParams.ObjectMiddleVertical - FieldHeight / 2;
if (Math.Abs(diffY) > GetStep())
{
if (diffY > 0)
{
MoveUp();
}
else
{
MoveDown();
}
}
}
}