Advertisement
Onigiri10

ships

Nov 26th, 2021
704
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 13.43 KB | None | 0 0
  1. #pragma once
  2. #include "windows.h"
  3. #include <list>
  4.  
  5. namespace ships {
  6.  
  7.     using namespace System;
  8.     using namespace System::ComponentModel;
  9.     using namespace System::Collections;
  10.     using namespace System::Windows::Forms;
  11.     using namespace System::Data;
  12.     using namespace System::Drawing;
  13.     using namespace System::Threading;
  14.     using namespace System::Collections::Generic;
  15.  
  16.     /// <summary>
  17.     /// Сводка для MyForm
  18.     /// </summary>
  19.     public ref class MyForm : public System::Windows::Forms::Form
  20.     {
  21.         Image^ ship1 = Image::FromFile("C:/Users/golubova/Desktop/ship1.png");
  22.         Image^ ship1_2 = Image::FromFile("C:/Users/golubova/Desktop/ship_135.png");
  23.         Image^ ship2 = Image::FromFile("C:/Users/golubova/Desktop/ship2.png");
  24.         Image^ ship2_3 = Image::FromFile("C:/Users/golubova/Desktop/ship_225.png");
  25.         Image^ ship3 = Image::FromFile("C:/Users/golubova/Desktop/ship3.png");
  26.         Image^ ship3_4 = Image::FromFile("C:/Users/golubova/Desktop/ship_315.png");
  27.         Image^ ship4 = Image::FromFile("C:/Users/golubova/Desktop/ship4.png");
  28.         Image^ ship4_1 = Image::FromFile("C:/Users/golubova/Desktop/ship_45.png");
  29.  
  30.         int height, width;
  31.         const double metrPerPixels = 2;
  32.         int secondsPerModelStep = 1;
  33.  
  34.         double x1 = 0;
  35.         double y1 = 0;
  36.         int direction;                      //направление, в котором движется судно
  37.                                             //1-восток, 2-юг, 3-запад, 4-север
  38.         const int EAST = 1;                
  39.         const int SOUTH = 2;           
  40.         const int WEST = 3;                
  41.         const int NORTH = 4;
  42.  
  43.         double speed = 2;                   //м/c
  44.         double FULL_SPEED = 2.0;
  45.         const double SLOW_SPEED = 0.5;
  46.         int shipMode;                       //режим судна
  47.         const int ROADS_MODE = 0;           //0-на рейде
  48.         const int FULL_SPEED_MODE = 1;      //1-полный ход
  49.         const int SLOW_SPEED_MODE = 2;      //2-малый ход
  50.         const int UNLOAD_MODE = 3;          //3-разгрузка
  51.         const int LOAD_MODE = 4;            //4-загрузка
  52.  
  53.         int targetPointIndex = 0;           //номер точки, к которой движется судно
  54.  
  55.         const int PASS_MAX_CAPACITY = 1000; //вместимость пассажиров
  56.         const int PASS_CHARGE_RATE = 5;     //посадка пассажиров в секунду
  57.         int passengersOnBoard = PASS_MAX_CAPACITY;
  58.        
  59.         /*Список точек, к которым движется судно*/
  60.         List<Tuple<int, int, int,int,double, String^>^>^ route = gcnew List<Tuple<int, int, int, int, double, String^>^>();
  61.  
  62.         bool isEmpty = false;
  63.        
  64.         void modelStep() //функция, описывающая движение судна
  65.         {
  66.             for (int k = 0; k < secondsPerModelStep; k++)
  67.             {
  68.                 if (speed > 0)
  69.                 {
  70.                     int tx = route[targetPointIndex]->Item1;
  71.                     int ty = route[targetPointIndex]->Item2;
  72.  
  73.                     double d = Math::Sqrt((x1 - tx)*(x1 - tx) + (y1 - ty)*(y1 - ty));
  74.                     if (d < speed)
  75.                     {
  76.                         if (targetPointIndex == route->Count - 1)
  77.                         {
  78.                             listBox1->Items->Add("Конец маршрута");
  79.                             return;
  80.                         }
  81.                         direction = route[targetPointIndex]->Item3;
  82.                         shipMode = route[targetPointIndex]->Item4;
  83.                         speed = route[targetPointIndex]->Item5;
  84.                         listBox1->Items->Add(route[targetPointIndex]->Item6);
  85.                         targetPointIndex++;
  86.                        
  87.                         x1 = tx;
  88.                         y1 = ty;
  89.                         continue;
  90.                     }
  91.                     if (direction == EAST)
  92.                     {
  93.                         x1 += speed;
  94.                     }
  95.                     else if (direction == SOUTH)
  96.                     {
  97.                         y1 += speed;
  98.                     }
  99.                     else if (direction == WEST)
  100.                     {
  101.                         x1 -= speed;
  102.                     }
  103.                     else
  104.                     {
  105.                         y1 -= speed;
  106.                     }
  107.                 }
  108.                 else if (shipMode == UNLOAD_MODE) //высадка пассажиров
  109.                 {
  110.                     if (passengersOnBoard >= PASS_CHARGE_RATE)
  111.                     {
  112.                         passengersOnBoard -= PASS_CHARGE_RATE;
  113.                     }
  114.                     else
  115.                     {
  116.                         passengersOnBoard = 0;
  117.                     }
  118.                     if (passengersOnBoard == 0)
  119.                     {
  120.                         shipMode = route[targetPointIndex+1]->Item4;
  121.                         listBox1->Items->Add(route[targetPointIndex+1]->Item6);
  122.                         speed = route[targetPointIndex + 1]->Item5;
  123.                         targetPointIndex++;
  124.                     }
  125.                 }
  126.                 else if (shipMode == LOAD_MODE) //посадка пассажиров
  127.                 {
  128.                     if (passengersOnBoard < PASS_MAX_CAPACITY)
  129.                     {
  130.                         passengersOnBoard += PASS_CHARGE_RATE;
  131.                     }
  132.                     if (passengersOnBoard >= PASS_MAX_CAPACITY)
  133.                     {
  134.                         passengersOnBoard = PASS_CHARGE_RATE;
  135.                         shipMode = route[targetPointIndex + 1]->Item4;
  136.                         listBox1->Items->Add(route[targetPointIndex + 1]->Item6);
  137.                         speed = route[targetPointIndex + 1]->Item5;
  138.                         targetPointIndex++;
  139.                     }
  140.                 }
  141.             }
  142.         }
  143.        
  144.     public:
  145.         MyForm(void)
  146.         {
  147.             InitializeComponent();
  148.             //
  149.             //TODO: добавьте код конструктора
  150.             //
  151.         }
  152.  
  153.     protected:
  154.         /// <summary>
  155.         /// Освободить все используемые ресурсы.
  156.         /// </summary>
  157.         ~MyForm()
  158.         {
  159.             if (components)
  160.             {
  161.                 delete components;
  162.             }
  163.         }
  164.  
  165.     private: System::Windows::Forms::Timer^  timer1;
  166.     private: System::Windows::Forms::TrackBar^  trackBar1;
  167.     private: System::Windows::Forms::ListBox^  listBox1;
  168.     private: System::Windows::Forms::PictureBox^  picPort;
  169.     //private: System::Windows::Forms::PictureBox^  picPortKors; //временно убрана картинка порта
  170.     protected:
  171.  
  172.     private: System::Windows::Forms::PictureBox^  picShip;
  173.     private: System::ComponentModel::IContainer^  components;
  174.     protected:
  175.     protected:
  176.     protected:
  177.     private:
  178.         /// <summary>
  179.         /// Обязательная переменная конструктора.
  180.         /// </summary>
  181. #pragma region Windows Form Designer generated code
  182.         /// <summary>
  183.         /// Требуемый метод для поддержки конструктора — не изменяйте
  184.         /// содержимое этого метода с помощью редактора кода.
  185.         /// </summary>
  186.         void InitializeComponent(void)
  187.         {
  188.             this->components = (gcnew System::ComponentModel::Container());
  189.             System::ComponentModel::ComponentResourceManager^  resources = (gcnew System::ComponentModel::ComponentResourceManager(MyForm::typeid));
  190.             this->picPort = (gcnew System::Windows::Forms::PictureBox());
  191.             this->picShip = (gcnew System::Windows::Forms::PictureBox());
  192.             this->timer1 = (gcnew System::Windows::Forms::Timer(this->components));
  193.             this->trackBar1 = (gcnew System::Windows::Forms::TrackBar());
  194.             this->listBox1 = (gcnew System::Windows::Forms::ListBox());
  195.             //this->picPortKors = (gcnew System::Windows::Forms::PictureBox());
  196.             (cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->picPort))->BeginInit();
  197.             (cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->picShip))->BeginInit();
  198.             (cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->trackBar1))->BeginInit();
  199.             //(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->picPortKors))->BeginInit();
  200.             this->SuspendLayout();
  201.             //
  202.             // picPort
  203.             //
  204.             this->picPort->BackColor = System::Drawing::SystemColors::ActiveCaption;
  205.             this->picPort->Dock = System::Windows::Forms::DockStyle::Fill;
  206.             this->picPort->Image = (cli::safe_cast<System::Drawing::Image^>(resources->GetObject(L"picPort.Image")));
  207.             this->picPort->Location = System::Drawing::Point(0, 0);
  208.             this->picPort->Name = L"picPort";
  209.             this->picPort->Size = System::Drawing::Size(871, 613);
  210.             this->picPort->SizeMode = System::Windows::Forms::PictureBoxSizeMode::StretchImage;
  211.             this->picPort->TabIndex = 0;
  212.             this->picPort->TabStop = false;
  213.             //
  214.             // picShip
  215.             //
  216.             this->picShip->BackColor = System::Drawing::Color::LightSkyBlue;
  217.             this->picShip->Image = (cli::safe_cast<System::Drawing::Image^>(resources->GetObject(L"picShip.Image")));
  218.             this->picShip->Location = System::Drawing::Point(0, 12);
  219.             this->picShip->Name = L"picShip";
  220.             this->picShip->Size = System::Drawing::Size(51, 20);
  221.             this->picShip->SizeMode = System::Windows::Forms::PictureBoxSizeMode::Zoom;
  222.             this->picShip->TabIndex = 1;
  223.             this->picShip->TabStop = false;
  224.             this->picShip->Visible = false;
  225.             this->picShip->Click += gcnew System::EventHandler(this, &MyForm::pictureBox2_Click);
  226.             //
  227.             // timer1
  228.             //
  229.             this->timer1->Enabled = true;
  230.             this->timer1->Interval = 20;
  231.             this->timer1->Tick += gcnew System::EventHandler(this, &MyForm::timer1_Tick);
  232.             //
  233.             // trackBar1
  234.             //
  235.             this->trackBar1->Location = System::Drawing::Point(660, 520);
  236.             this->trackBar1->Maximum = 20;
  237.             this->trackBar1->Minimum = 1;
  238.             this->trackBar1->Name = L"trackBar1";
  239.             this->trackBar1->Size = System::Drawing::Size(189, 69);
  240.             this->trackBar1->TabIndex = 2;
  241.             this->trackBar1->TickFrequency = 75;
  242.             this->trackBar1->Value = 1;
  243.             this->trackBar1->Scroll += gcnew System::EventHandler(this, &MyForm::trackBar1_Scroll);
  244.             //
  245.             // listBox1
  246.             //
  247.             this->listBox1->FormattingEnabled = true;
  248.             this->listBox1->ItemHeight = 20;
  249.             this->listBox1->Location = System::Drawing::Point(604, 50);
  250.             this->listBox1->Name = L"listBox1";
  251.             this->listBox1->Size = System::Drawing::Size(245, 84);
  252.             this->listBox1->TabIndex = 3;
  253.             /*
  254.             // picPortKors
  255.             //
  256.             this->picPortKors->BackColor = System::Drawing::Color::Transparent;
  257.             this->picPortKors->Image = (cli::safe_cast<System::Drawing::Image^>(resources->GetObject(L"picPortKors.Image")));
  258.             this->picPortKors->Location = System::Drawing::Point(190, 499);
  259.             this->picPortKors->Name = L"picPortKors";
  260.             this->picPortKors->Size = System::Drawing::Size(81, 56);
  261.             this->picPortKors->SizeMode = System::Windows::Forms::PictureBoxSizeMode::Zoom;
  262.             this->picPortKors->TabIndex = 5;
  263.             this->picPortKors->TabStop = false;
  264.             */
  265.             //
  266.             // MyForm
  267.             //
  268.             this->AutoScaleDimensions = System::Drawing::SizeF(9, 20);
  269.             this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
  270.             this->ClientSize = System::Drawing::Size(871, 613);
  271.             //this->Controls->Add(this->picPortKors);
  272.             this->Controls->Add(this->listBox1);
  273.             this->Controls->Add(this->trackBar1);
  274.             this->Controls->Add(this->picShip);
  275.             this->Controls->Add(this->picPort);
  276.             this->Name = L"MyForm";
  277.             this->Text = L"Модель захода морских судов в порт";
  278.             (cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->picPort))->EndInit();
  279.             (cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->picShip))->EndInit();
  280.             (cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->trackBar1))->EndInit();
  281.             //(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->picPortKors))->EndInit();
  282.             this->ResumeLayout(false);
  283.             this->PerformLayout();
  284.            
  285.             /*Маршут точек, по которым движутся суда*/
  286.             route->Add(gcnew Tuple<int, int, int, int, double, String^>(0, 250, 1, FULL_SPEED_MODE, FULL_SPEED, "Режим полный ход")); //старт
  287.             route->Add(gcnew Tuple<int, int, int, int, double, String^>(375, 250, 2, SLOW_SPEED_MODE, SLOW_SPEED, "Режим малый ход"));
  288.             route->Add(gcnew Tuple<int, int, int, int, double, String^>(375, 400, 2, UNLOAD_MODE, 0, "Приступаю к разгрузке"));
  289.             route->Add(gcnew Tuple<int, int, int, int, double, String^>(375, 400, 2, LOAD_MODE, 0, "Приступаю к погрузке"));
  290.             route->Add(gcnew Tuple<int, int, int, int, double, String^>(375, 400, 2, SLOW_SPEED_MODE, SLOW_SPEED, "Режим малый ход"));
  291.             route->Add(gcnew Tuple<int, int, int, int, double, String^>(375, 520, 1, SLOW_SPEED_MODE, SLOW_SPEED, "Режим малый ход"));
  292.             route->Add(gcnew Tuple<int, int, int, int, double, String^>(700, 520, 4, SLOW_SPEED_MODE, SLOW_SPEED, "Режим малый ход"));
  293.             route->Add(gcnew Tuple<int, int, int, int, double, String^>(700, 200, 1, SLOW_SPEED_MODE, SLOW_SPEED, "Режим малый ход"));
  294.             route->Add(gcnew Tuple<int, int, int, int, double, String^>(800, 200, 1, FULL_SPEED_MODE, FULL_SPEED, "Режим полный ход"));
  295.             route->Add(gcnew Tuple<int, int, int, int, double, String^>(1210, 200, 1, ROADS_MODE, 0, "Конец маршрута"));
  296.  
  297.  
  298.             x1 = route[0]->Item1;
  299.             y1 = route[0]->Item2;
  300.             direction = route[0]->Item3;
  301.             shipMode = route[0]->Item4;
  302.             speed = route[0]->Item5;
  303.         }
  304. #pragma endregion
  305.     private: System::Void pictureBox1_Click(System::Object^  sender, System::EventArgs^  e) {
  306.     }
  307.     private: System::Void pictureBox2_Click(System::Object^  sender, System::EventArgs^  e) {
  308.     }
  309.     private: System::Void timer1_Tick(System::Object^  sender, System::EventArgs^  e) {
  310.  
  311.         modelStep();
  312.         if (direction == EAST)
  313.         {
  314.             picShip->Image = ship1;
  315.             picShip->Width = ship1->Width;
  316.             picShip->Height = ship1->Height;
  317.         }
  318.         else if (direction == SOUTH)
  319.         {
  320.             picShip->Image = ship2;
  321.             picShip->Width = ship2->Width;
  322.             picShip->Height = ship2->Height;
  323.         }
  324.         else if (direction == WEST)
  325.         {
  326.             picShip->Image = ship3;
  327.             picShip->Width = ship3->Width;
  328.             picShip->Height = ship3->Height;
  329.         }
  330.         else
  331.         {
  332.             picShip->Image = ship4;
  333.             picShip->Width = ship4->Width;
  334.             picShip->Height = ship4->Height;
  335.         }
  336.  
  337.         int leftUpCornerY = y1 / metrPerPixels - picShip->Height / 2; //смещение координат на центр картинки
  338.         int leftUpCornerX = x1 / metrPerPixels - picShip->Width / 2;
  339.         picShip->Top = leftUpCornerY;
  340.         picShip->Left = leftUpCornerX;
  341.  
  342.         picShip->Visible = true;
  343.     }  
  344.  
  345.     private: System::Void trackBar1_Scroll(System::Object^  sender, System::EventArgs^  e)
  346.     {
  347.         secondsPerModelStep = trackBar1->Value; //изменение скорости модельного времени
  348.     }
  349. };
  350. }
  351.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement