Advertisement
Guest User

Untitled

a guest
Apr 21st, 2015
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.52 KB | None | 0 0
  1. #pragma once
  2.  
  3.  
  4. namespace graphic4 {
  5.  
  6.     using namespace System;
  7.     using namespace System::ComponentModel;
  8.     using namespace System::Collections;
  9.     using namespace System::Windows::Forms;
  10.     using namespace System::Data;
  11.     using namespace System::Drawing;
  12.  
  13.     /// <summary>
  14.     /// Summary for Form1
  15.     ///
  16.     /// WARNING: If you change the name of this class, you will need to change the
  17.     ///          'Resource File Name' property for the managed resource compiler tool
  18.     ///          associated with all .resx files this class depends on.  Otherwise,
  19.     ///          the designers will not be able to interact properly with localized
  20.     ///          resources associated with this form.
  21.     /// </summary>
  22.     public ref class Form1 : public System::Windows::Forms::Form
  23.     {
  24.     public:
  25.         Form1(void)
  26.         {
  27.             InitializeComponent();
  28.             //
  29.             //TODO: Add the constructor code here
  30.             //
  31.         }
  32.  
  33.     protected:
  34.         /// <summary>
  35.         /// Clean up any resources being used.
  36.         /// </summary>
  37.         ~Form1()
  38.         {
  39.             if (components)
  40.             {
  41.                 delete components;
  42.             }
  43.         }
  44.     private: System::Collections::Generic::List<polygon^> polygons;
  45.     private: Rectangle rect;
  46.     private: float left, right, top, bottom,
  47.                  Wcx, Wcy, Wx, Wy,
  48.                  Vcx, Vcy, Vx, Vy;
  49.     private: System::Windows::Forms::Button^  loadImageButton;
  50.     private: System::Windows::Forms::OpenFileDialog^  openFileDialog;
  51.     protected:
  52.  
  53.     private:
  54.         /// <summary>
  55.         /// Required designer variable.
  56.         /// </summary>
  57.         System::ComponentModel::Container ^components;
  58.  
  59. #pragma region Windows Form Designer generated code
  60.         /// <summary>
  61.         /// Required method for Designer support - do not modify
  62.         /// the contents of this method with the code editor.
  63.         /// </summary>
  64.         void InitializeComponent(void)
  65.         {
  66.             this->loadImageButton = (gcnew System::Windows::Forms::Button());
  67.             this->openFileDialog = (gcnew System::Windows::Forms::OpenFileDialog());
  68.             this->SuspendLayout();
  69.             //
  70.             // loadImageButton
  71.             //
  72.             this->loadImageButton->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((System::Windows::Forms::AnchorStyles::Top | System::Windows::Forms::AnchorStyles::Right));
  73.             this->loadImageButton->Location = System::Drawing::Point(592, 12);
  74.             this->loadImageButton->Name = L"loadImageButton";
  75.             this->loadImageButton->Size = System::Drawing::Size(75, 23);
  76.             this->loadImageButton->TabIndex = 0;
  77.             this->loadImageButton->Text = L"Open";
  78.             this->loadImageButton->UseVisualStyleBackColor = true;
  79.             this->loadImageButton->Click += gcnew System::EventHandler(this, &Form1::loadImageButton_Click);
  80.             //
  81.             // openFileDialog
  82.             //
  83.             this->openFileDialog->DefaultExt = L"txt";
  84.             this->openFileDialog->Filter = L"Text files (*.txt)|*.txt|All files (*.*)|*.*";
  85.             this->openFileDialog->Title = L"Open file";
  86.             //
  87.             // Form1
  88.             //
  89.             this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
  90.             this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
  91.             this->ClientSize = System::Drawing::Size(679, 417);
  92.             this->Controls->Add(this->loadImageButton);
  93.             this->DoubleBuffered = true;
  94.             this->KeyPreview = true;
  95.             this->MinimumSize = System::Drawing::Size(200, 100);
  96.             this->Name = L"Form1";
  97.             this->Text = L"Task 7";
  98.             this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
  99.             this->Paint += gcnew System::Windows::Forms::PaintEventHandler(this, &Form1::Form1_Paint);
  100.             this->Resize += gcnew System::EventHandler(this, &Form1::Form1_Resize);
  101.             this->KeyDown += gcnew System::Windows::Forms::KeyEventHandler(this, &Form1::Form1_KeyDown);
  102.             this->ResumeLayout(false);
  103.  
  104.         }
  105. #pragma endregion
  106.     private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
  107.                  rect = Form::ClientRectangle;
  108.                  const short MARGIN_VALUE = 20;
  109.  
  110.                  left = MARGIN_VALUE;
  111.                  top = MARGIN_VALUE;
  112.                  right = MARGIN_VALUE;
  113.                  bottom = MARGIN_VALUE;
  114.  
  115.                  Wcx = left;
  116.                  Wcy = rect.Height - bottom;
  117.                  Wx = rect.Width - left - right;
  118.                  Wy = rect.Height - top - bottom;
  119.  
  120.                  polygons.Clear();
  121.  
  122.                  this->Refresh();
  123.              }
  124.  
  125.     private: System::Void Form1_Paint(System::Object^  sender, System::Windows::Forms::PaintEventArgs^  e) {
  126.                  Graphics^ g = e->Graphics;
  127.  
  128.                  Pen^ blackPen = gcnew Pen(Color::Black);
  129.                  blackPen->Width = 1;
  130.  
  131.                  Pen^ borderPen = gcnew Pen(Color::Red);
  132.                  borderPen->Width = 3;
  133.  
  134.                  Drawing::Font^ font = gcnew Drawing::Font("Arial", 8);
  135.                  SolidBrush^ brush = gcnew SolidBrush(Color::Blue);
  136.  
  137.                  point Pmax, Pmin;
  138.                  Pmax.x = this->ClientRectangle.Width - right;
  139.                  Pmin.x = left;
  140.  
  141.                  Pmax.y = this->ClientRectangle.Height - bottom;
  142.                  Pmin.y = top;
  143.  
  144.                  g->DrawRectangle(borderPen, left, top, Wx, Wy);
  145.  
  146.                  for (int i = 0; i < polygons.Count; i++) {
  147.                     polygon^ p = polygons[i];
  148.                     polygon^ p_new = gcnew polygon(0);
  149.                    
  150.                     point a, b;
  151.                     vec A, B;
  152.  
  153.                     for (int j = 0; j < p->Count; j++) {
  154.                         point2vec(p[j], A);
  155.                         timesMatVec(T, A, B);
  156.                         vec2point(B, a);
  157.                         p_new->Add(a);
  158.                     }
  159.  
  160.                     p_new = Pclip(p_new, Pmin, Pmax);
  161.  
  162.                     if (p_new->Count) {
  163.                         b = p_new[p_new->Count - 1];
  164.  
  165.                         for (int k = 0; k < p_new->Count; k++) {
  166.                             g->DrawLine(blackPen, b.x, b.y, p_new[k].x, p_new[k].y);
  167.                             b = p_new[k];
  168.                         }
  169.                     }
  170.                  }
  171.              }
  172.     private: System::Void loadImageButton_Click(System::Object^  sender, System::EventArgs^  e) {
  173.  
  174.                  if (this->openFileDialog->ShowDialog() ==
  175.                      System::Windows::Forms::DialogResult::OK) {
  176.                          wchar_t fileName[1024];   
  177.                          for (int i = 0; i < openFileDialog->FileName->Length; i++)
  178.                              fileName[i] = openFileDialog->FileName[i];
  179.                          fileName[openFileDialog->FileName->Length] = '\0';
  180.                          std::ifstream in(fileName);
  181.                          if ( in.is_open() ) {
  182.                              matrices.clear();
  183.                              std::stack<mat> matStack;
  184.                              mat K;
  185.                              unit(K);
  186.                              unit(T);
  187.  
  188.                              std::string str;
  189.                              getline (in, str);
  190.  
  191.                              while (in) {
  192.                                  if ((str.find_last_not_of(" \t\r\n") != std::string::npos)
  193.                                      && (str[0] != '#')) {
  194.                                          std::stringstream s(str);
  195.                                        
  196.                                          std::string cmd;
  197.                                          s >> cmd;
  198.  
  199.                                          if ( cmd == "frame" ) {
  200.                                             int point1, point2, point3, point4;
  201.  
  202.                                             s >> point1 >> point2 >> point3 >> point4;
  203.  
  204.                                             Vcx = point1;
  205.                                             Vcy = point2;
  206.                                             Vx = point3;
  207.                                             Vy = point4;
  208.  
  209.                                             frame(Vx, Vy, Vcx, Vcy, Wx, Wy, Wcx, Wcy, T);
  210.                                          } else if ( cmd == "polygon" ) {
  211.                                              int numpoint;
  212.                                              s >> numpoint;
  213.  
  214.                                              polygon^ P = gcnew polygon(0);
  215.                                              for (int i = 0; i < numpoint; i++) {
  216.                                                  point p;
  217.                                                  s >> p.x >> p.y;
  218.                                                  P->Add(p);
  219.                                              }
  220.                                              polygons.Add(P);
  221.                                          }
  222.                                     }
  223.                                  getline (in, str);
  224.                                 }
  225.                             }
  226.                          this->Refresh();
  227.                  }
  228.              }
  229.     private: System::Void Form1_KeyDown(System::Object^  sender, System::Windows::Forms::KeyEventArgs^  e) {
  230.                  rect = Form::ClientRectangle;
  231.                  mat R, T1;
  232.                  switch (e->KeyCode) {
  233.                      case Keys::Q :
  234.                          rotate(-0.05, R);
  235.                          break;
  236.                      case Keys::E :
  237.                          rotate(0.05, R);
  238.                          break;
  239.  
  240.                      case Keys::Z :
  241.                          scale(R, 1/1.1);
  242.                          break;
  243.                      case Keys::X :
  244.                          scale(R, 1.1);
  245.                          break;
  246.  
  247.                      case Keys::T :
  248.                          move(0, -10, R);
  249.                          break;
  250.                      case Keys::G :
  251.                          move(0, 10, R);
  252.                          break;
  253.                      case Keys::F :
  254.                          move(-10, 0, R);
  255.                          break;
  256.                      case Keys::H :
  257.                          move(10, 0, R);
  258.                          break;
  259.  
  260.                      case Keys::W :
  261.                          move(0, -1, R);
  262.                          break;
  263.                      case Keys::S :
  264.                          move(0, 1, R);
  265.                          break;
  266.                      case Keys::A :
  267.                          move(-1, 0, R);
  268.                          break;
  269.                      case Keys::D :
  270.                          move(1, 0, R);
  271.                          break;
  272.  
  273.                      case Keys::U :
  274.                          reflect(-1, 1, R);
  275.                          times(R, T, T);
  276.                          move(0, rect.Height, R);
  277.                          break;
  278.                      case Keys::J :
  279.                          reflect(1, -1, R);;
  280.                          times(R, T, T);
  281.                          move(rect.Width, 0, R);
  282.                          break;
  283.  
  284.                      case Keys::R :
  285.                          move(-rect.Width/2, -rect.Height/2, R);
  286.                          times(R, T, T1);
  287.                          set(T1, T);
  288.                          rotate(-0.05, R);
  289.                          times(R, T, T1);
  290.                          set(T1, T);
  291.                          move(rect.Width/2, rect.Height/2, R);
  292.                          break;
  293.                      case Keys::Y :
  294.                          move(-rect.Width/2, -rect.Height/2, R);
  295.                          times(R, T, T1);
  296.                          set(T1, T);
  297.                          rotate(0.05, R);
  298.                          times(R, T, T1);
  299.                          set(T1, T);
  300.                          move(rect.Width/2, rect.Height/2, R);
  301.                          break;
  302.  
  303.                      case Keys::C :
  304.                          move(-rect.Width/2, -rect.Height/2, R);
  305.                          times(R, T, T);
  306.                          scale(R, 1/1.1);
  307.                          times(R, T, T);
  308.                          move(rect.Width/2, rect.Height/2, R);
  309.                          break;
  310.                      case Keys::V :
  311.                          move(-rect.Width/2, -rect.Height/2, R);
  312.                          times(R, T, T);
  313.                          scale(R, 1.1);
  314.                          times(R, T, T);
  315.                          move(rect.Width/2, rect.Height/2, R);
  316.                          break;
  317.  
  318.                       case Keys::I :
  319.                         move(0, -rect.Height/2, R);
  320.                         times(R, T, T1);
  321.                         set(T1, T);
  322.                         scale(R, 1/1.1, 1);
  323.                         times(R, T, T1);
  324.                         set(T1, T);
  325.                         move(0, rect.Height, R);
  326.                         break;
  327.                      case Keys::O :
  328.                          move(0, -rect.Height/2, R);
  329.                          times(R, T, T1);
  330.                          set(T1, T);
  331.                          scale(R, 1.1, 1);
  332.                          times(R, T, T1);
  333.                          set(T1, T);
  334.                          move(0, rect.Height, R);
  335.                          break;
  336.                      case Keys::K :
  337.                          move(-rect.Width/2, 0, R);
  338.                          times(R, T, T1);
  339.                          set(T1, T);
  340.                          scale(R, 1, 1/1.1);
  341.                          times(R, T, T1);
  342.                          set(T1, T);
  343.                          move(rect.Width, 0, R);
  344.                          break;
  345.                      case Keys::L :
  346.                          move(-rect.Width/2, 0, R);
  347.                          times(R, T, T1);
  348.                          set(T1, T);
  349.                          scale(R, 1, 1.1);
  350.                          times(R, T, T1);
  351.                          set(T1, T);
  352.                          move(rect.Width, 0, R);
  353.                          break;
  354.  
  355.                      case Keys::Escape :
  356.                          frame(Vx, Vy, Vcx, Vcy, Wx, Wy, Wcx, Wcy, T);
  357.                          unit(R);
  358.                          break;
  359.  
  360.                      default :
  361.                          unit(R);
  362.                  }
  363.                  times(R, T, T1);
  364.                  set(T1, T);
  365.                  this->Refresh();
  366.              }
  367. private: System::Void Form1_Resize(System::Object^  sender, System::EventArgs^  e) {
  368.              rect = ClientRectangle;
  369.  
  370.              float oldWx = Wx;
  371.              float oldWy = Wy;
  372.              
  373.              Wcx = left;
  374.              Wcy = rect.Height - bottom;
  375.              Wx = rect.Width - left - right;
  376.              Wy = rect.Height - top - bottom;
  377.  
  378.              mat R, T1;
  379.              move(-Wcx / 2, -top / 2, R);
  380.              times(R, T, T1);
  381.              set(T1, T);
  382.  
  383.              scale(R, Wx / oldWx, Wy / oldWy);
  384.              times(R, T, T1);
  385.              set(T1, T);
  386.  
  387.              move(Wcx, top, R);
  388.              times(R, T, T1);
  389.              set(T1, T);
  390.              
  391.              this->Refresh();
  392.          }
  393. };
  394. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement