Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- namespace graphic4 {
- using namespace System;
- using namespace System::ComponentModel;
- using namespace System::Collections;
- using namespace System::Windows::Forms;
- using namespace System::Data;
- using namespace System::Drawing;
- /// <summary>
- /// Summary for Form1
- ///
- /// WARNING: If you change the name of this class, you will need to change the
- /// 'Resource File Name' property for the managed resource compiler tool
- /// associated with all .resx files this class depends on. Otherwise,
- /// the designers will not be able to interact properly with localized
- /// resources associated with this form.
- /// </summary>
- public ref class Form1 : public System::Windows::Forms::Form
- {
- public:
- Form1(void)
- {
- InitializeComponent();
- //
- //TODO: Add the constructor code here
- //
- }
- protected:
- /// <summary>
- /// Clean up any resources being used.
- /// </summary>
- ~Form1()
- {
- if (components)
- {
- delete components;
- }
- }
- private: System::Collections::Generic::List<polygon^> polygons;
- private: Rectangle rect;
- private: float left, right, top, bottom,
- Wcx, Wcy, Wx, Wy,
- Vcx, Vcy, Vx, Vy;
- private: System::Windows::Forms::Button^ loadImageButton;
- private: System::Windows::Forms::OpenFileDialog^ openFileDialog;
- protected:
- private:
- /// <summary>
- /// Required designer variable.
- /// </summary>
- System::ComponentModel::Container ^components;
- #pragma 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>
- void InitializeComponent(void)
- {
- this->loadImageButton = (gcnew System::Windows::Forms::Button());
- this->openFileDialog = (gcnew System::Windows::Forms::OpenFileDialog());
- this->SuspendLayout();
- //
- // loadImageButton
- //
- this->loadImageButton->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((System::Windows::Forms::AnchorStyles::Top | System::Windows::Forms::AnchorStyles::Right));
- this->loadImageButton->Location = System::Drawing::Point(592, 12);
- this->loadImageButton->Name = L"loadImageButton";
- this->loadImageButton->Size = System::Drawing::Size(75, 23);
- this->loadImageButton->TabIndex = 0;
- this->loadImageButton->Text = L"Open";
- this->loadImageButton->UseVisualStyleBackColor = true;
- this->loadImageButton->Click += gcnew System::EventHandler(this, &Form1::loadImageButton_Click);
- //
- // openFileDialog
- //
- this->openFileDialog->DefaultExt = L"txt";
- this->openFileDialog->Filter = L"Text files (*.txt)|*.txt|All files (*.*)|*.*";
- this->openFileDialog->Title = L"Open file";
- //
- // Form1
- //
- this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
- this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
- this->ClientSize = System::Drawing::Size(679, 417);
- this->Controls->Add(this->loadImageButton);
- this->DoubleBuffered = true;
- this->KeyPreview = true;
- this->MinimumSize = System::Drawing::Size(200, 100);
- this->Name = L"Form1";
- this->Text = L"Task 7";
- this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
- this->Paint += gcnew System::Windows::Forms::PaintEventHandler(this, &Form1::Form1_Paint);
- this->Resize += gcnew System::EventHandler(this, &Form1::Form1_Resize);
- this->KeyDown += gcnew System::Windows::Forms::KeyEventHandler(this, &Form1::Form1_KeyDown);
- this->ResumeLayout(false);
- }
- #pragma endregion
- private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
- rect = Form::ClientRectangle;
- const short MARGIN_VALUE = 20;
- left = MARGIN_VALUE;
- top = MARGIN_VALUE;
- right = MARGIN_VALUE;
- bottom = MARGIN_VALUE;
- Wcx = left;
- Wcy = rect.Height - bottom;
- Wx = rect.Width - left - right;
- Wy = rect.Height - top - bottom;
- polygons.Clear();
- this->Refresh();
- }
- private: System::Void Form1_Paint(System::Object^ sender, System::Windows::Forms::PaintEventArgs^ e) {
- Graphics^ g = e->Graphics;
- Pen^ blackPen = gcnew Pen(Color::Black);
- blackPen->Width = 1;
- Pen^ borderPen = gcnew Pen(Color::Red);
- borderPen->Width = 3;
- Drawing::Font^ font = gcnew Drawing::Font("Arial", 8);
- SolidBrush^ brush = gcnew SolidBrush(Color::Blue);
- point Pmax, Pmin;
- Pmax.x = this->ClientRectangle.Width - right;
- Pmin.x = left;
- Pmax.y = this->ClientRectangle.Height - bottom;
- Pmin.y = top;
- g->DrawRectangle(borderPen, left, top, Wx, Wy);
- for (int i = 0; i < polygons.Count; i++) {
- polygon^ p = polygons[i];
- polygon^ p_new = gcnew polygon(0);
- point a, b;
- vec A, B;
- for (int j = 0; j < p->Count; j++) {
- point2vec(p[j], A);
- timesMatVec(T, A, B);
- vec2point(B, a);
- p_new->Add(a);
- }
- p_new = Pclip(p_new, Pmin, Pmax);
- if (p_new->Count) {
- b = p_new[p_new->Count - 1];
- for (int k = 0; k < p_new->Count; k++) {
- g->DrawLine(blackPen, b.x, b.y, p_new[k].x, p_new[k].y);
- b = p_new[k];
- }
- }
- }
- }
- private: System::Void loadImageButton_Click(System::Object^ sender, System::EventArgs^ e) {
- if (this->openFileDialog->ShowDialog() ==
- System::Windows::Forms::DialogResult::OK) {
- wchar_t fileName[1024];
- for (int i = 0; i < openFileDialog->FileName->Length; i++)
- fileName[i] = openFileDialog->FileName[i];
- fileName[openFileDialog->FileName->Length] = '\0';
- std::ifstream in(fileName);
- if ( in.is_open() ) {
- matrices.clear();
- std::stack<mat> matStack;
- mat K;
- unit(K);
- unit(T);
- std::string str;
- getline (in, str);
- while (in) {
- if ((str.find_last_not_of(" \t\r\n") != std::string::npos)
- && (str[0] != '#')) {
- std::stringstream s(str);
- std::string cmd;
- s >> cmd;
- if ( cmd == "frame" ) {
- int point1, point2, point3, point4;
- s >> point1 >> point2 >> point3 >> point4;
- Vcx = point1;
- Vcy = point2;
- Vx = point3;
- Vy = point4;
- frame(Vx, Vy, Vcx, Vcy, Wx, Wy, Wcx, Wcy, T);
- } else if ( cmd == "polygon" ) {
- int numpoint;
- s >> numpoint;
- polygon^ P = gcnew polygon(0);
- for (int i = 0; i < numpoint; i++) {
- point p;
- s >> p.x >> p.y;
- P->Add(p);
- }
- polygons.Add(P);
- }
- }
- getline (in, str);
- }
- }
- this->Refresh();
- }
- }
- private: System::Void Form1_KeyDown(System::Object^ sender, System::Windows::Forms::KeyEventArgs^ e) {
- rect = Form::ClientRectangle;
- mat R, T1;
- switch (e->KeyCode) {
- case Keys::Q :
- rotate(-0.05, R);
- break;
- case Keys::E :
- rotate(0.05, R);
- break;
- case Keys::Z :
- scale(R, 1/1.1);
- break;
- case Keys::X :
- scale(R, 1.1);
- break;
- case Keys::T :
- move(0, -10, R);
- break;
- case Keys::G :
- move(0, 10, R);
- break;
- case Keys::F :
- move(-10, 0, R);
- break;
- case Keys::H :
- move(10, 0, R);
- break;
- case Keys::W :
- move(0, -1, R);
- break;
- case Keys::S :
- move(0, 1, R);
- break;
- case Keys::A :
- move(-1, 0, R);
- break;
- case Keys::D :
- move(1, 0, R);
- break;
- case Keys::U :
- reflect(-1, 1, R);
- times(R, T, T);
- move(0, rect.Height, R);
- break;
- case Keys::J :
- reflect(1, -1, R);;
- times(R, T, T);
- move(rect.Width, 0, R);
- break;
- case Keys::R :
- move(-rect.Width/2, -rect.Height/2, R);
- times(R, T, T1);
- set(T1, T);
- rotate(-0.05, R);
- times(R, T, T1);
- set(T1, T);
- move(rect.Width/2, rect.Height/2, R);
- break;
- case Keys::Y :
- move(-rect.Width/2, -rect.Height/2, R);
- times(R, T, T1);
- set(T1, T);
- rotate(0.05, R);
- times(R, T, T1);
- set(T1, T);
- move(rect.Width/2, rect.Height/2, R);
- break;
- case Keys::C :
- move(-rect.Width/2, -rect.Height/2, R);
- times(R, T, T);
- scale(R, 1/1.1);
- times(R, T, T);
- move(rect.Width/2, rect.Height/2, R);
- break;
- case Keys::V :
- move(-rect.Width/2, -rect.Height/2, R);
- times(R, T, T);
- scale(R, 1.1);
- times(R, T, T);
- move(rect.Width/2, rect.Height/2, R);
- break;
- case Keys::I :
- move(0, -rect.Height/2, R);
- times(R, T, T1);
- set(T1, T);
- scale(R, 1/1.1, 1);
- times(R, T, T1);
- set(T1, T);
- move(0, rect.Height, R);
- break;
- case Keys::O :
- move(0, -rect.Height/2, R);
- times(R, T, T1);
- set(T1, T);
- scale(R, 1.1, 1);
- times(R, T, T1);
- set(T1, T);
- move(0, rect.Height, R);
- break;
- case Keys::K :
- move(-rect.Width/2, 0, R);
- times(R, T, T1);
- set(T1, T);
- scale(R, 1, 1/1.1);
- times(R, T, T1);
- set(T1, T);
- move(rect.Width, 0, R);
- break;
- case Keys::L :
- move(-rect.Width/2, 0, R);
- times(R, T, T1);
- set(T1, T);
- scale(R, 1, 1.1);
- times(R, T, T1);
- set(T1, T);
- move(rect.Width, 0, R);
- break;
- case Keys::Escape :
- frame(Vx, Vy, Vcx, Vcy, Wx, Wy, Wcx, Wcy, T);
- unit(R);
- break;
- default :
- unit(R);
- }
- times(R, T, T1);
- set(T1, T);
- this->Refresh();
- }
- private: System::Void Form1_Resize(System::Object^ sender, System::EventArgs^ e) {
- rect = ClientRectangle;
- float oldWx = Wx;
- float oldWy = Wy;
- Wcx = left;
- Wcy = rect.Height - bottom;
- Wx = rect.Width - left - right;
- Wy = rect.Height - top - bottom;
- mat R, T1;
- move(-Wcx / 2, -top / 2, R);
- times(R, T, T1);
- set(T1, T);
- scale(R, Wx / oldWx, Wy / oldWy);
- times(R, T, T1);
- set(T1, T);
- move(Wcx, top, R);
- times(R, T, T1);
- set(T1, T);
- this->Refresh();
- }
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement