From 73c6ec80c71dd43125b6a9b00dd9e940b827f29f Mon Sep 17 00:00:00 2001 From: Andrew Guschin Date: Tue, 10 Nov 2020 01:15:07 +0300 Subject: =?UTF-8?q?=D0=A0=D0=B0=D0=B7=D0=B4=D0=B5=D0=BB=D0=B8=D0=BB=205=20?= =?UTF-8?q?=D0=B7=D0=B0=D0=B4=D0=B0=D1=87=D1=83=20=D0=BD=D0=B0=20=D0=B4?= =?UTF-8?q?=D0=B2=D0=B0=20=D0=BF=D1=80=D0=BE=D0=B5=D0=BA=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- task05_2D/Guschin.sln | 31 ++++ task05_2D/Guschin/Clip.h | 54 ++++++ task05_2D/Guschin/Guschin.vcxproj | 143 ++++++++++++++++ task05_2D/Guschin/Guschin.vcxproj.filters | 41 +++++ task05_2D/Guschin/Guschin.vcxproj.user | 4 + task05_2D/Guschin/Matrix.h | 275 ++++++++++++++++++++++++++++++ task05_2D/Guschin/MyForm.cpp | 17 ++ task05_2D/Guschin/MyForm.h | 189 ++++++++++++++++++++ task05_2D/Guschin/MyForm.resx | 120 +++++++++++++ task05_2D/Guschin/Transform.h | 45 +++++ 10 files changed, 919 insertions(+) create mode 100644 task05_2D/Guschin.sln create mode 100644 task05_2D/Guschin/Clip.h create mode 100644 task05_2D/Guschin/Guschin.vcxproj create mode 100644 task05_2D/Guschin/Guschin.vcxproj.filters create mode 100644 task05_2D/Guschin/Guschin.vcxproj.user create mode 100644 task05_2D/Guschin/Matrix.h create mode 100644 task05_2D/Guschin/MyForm.cpp create mode 100644 task05_2D/Guschin/MyForm.h create mode 100644 task05_2D/Guschin/MyForm.resx create mode 100644 task05_2D/Guschin/Transform.h (limited to 'task05_2D') diff --git a/task05_2D/Guschin.sln b/task05_2D/Guschin.sln new file mode 100644 index 0000000..1dc06a7 --- /dev/null +++ b/task05_2D/Guschin.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.29806.167 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Guschin", "Guschin\Guschin.vcxproj", "{7FD831C6-82A0-4285-8C15-41D557E54312}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {7FD831C6-82A0-4285-8C15-41D557E54312}.Debug|x64.ActiveCfg = Debug|x64 + {7FD831C6-82A0-4285-8C15-41D557E54312}.Debug|x64.Build.0 = Debug|x64 + {7FD831C6-82A0-4285-8C15-41D557E54312}.Debug|x86.ActiveCfg = Debug|Win32 + {7FD831C6-82A0-4285-8C15-41D557E54312}.Debug|x86.Build.0 = Debug|Win32 + {7FD831C6-82A0-4285-8C15-41D557E54312}.Release|x64.ActiveCfg = Release|x64 + {7FD831C6-82A0-4285-8C15-41D557E54312}.Release|x64.Build.0 = Release|x64 + {7FD831C6-82A0-4285-8C15-41D557E54312}.Release|x86.ActiveCfg = Release|Win32 + {7FD831C6-82A0-4285-8C15-41D557E54312}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {44500E7D-60B5-4F8A-8A21-14C0785E2BF7} + EndGlobalSection +EndGlobal diff --git a/task05_2D/Guschin/Clip.h b/task05_2D/Guschin/Clip.h new file mode 100644 index 0000000..10a613a --- /dev/null +++ b/task05_2D/Guschin/Clip.h @@ -0,0 +1,54 @@ +#pragma once +#include "Matrix.h" +#include + +unsigned int codeKS(vec2 P, float minX, float minY, float maxX, float maxY) { + unsigned int code = 0; + + if (P.x < minX) { + code += 1; + } + else if (P.x > maxX) { + code += 2; + } + if (P.y < minY) { + code += 4; + } + else if (P.y > maxY) { + code += 8; + } + + return code; +} + +bool clip(vec2 & A, vec2 & B, float minX, float minY, float maxX, float maxY) { + unsigned int codeA = codeKS(A, minX, minY, maxX, maxY); + unsigned int codeB = codeKS(B, minX, minY, maxX, maxY); + while (codeA | codeB) { + if (codeA & codeB) { + return false; + } + if (codeA == 0) { + std::swap(A, B); + std::swap(codeA, codeB); + } + if (codeA & 1) { + A.y = A.y + (B.y - A.y) * (minX - A.x) / (B.x - A.x); + A.x = minX; + } + else if (codeA & 2) { + A.y = A.y + (B.y - A.y) * (maxX - A.x) / (B.x - A.x); + A.x = maxX; + } + else if (codeA & 4) { + A.x = A.x + (B.x - A.x) * (minY - A.y) / (B.y - A.y); + A.y = minY; + } + else { + A.x = A.x + (B.x - A.x) * (maxY - A.y) / (B.y - A.y); + A.y = maxY; + } + codeA = codeKS(A, minX, minY, maxX, maxY); + } + return true; +} diff --git a/task05_2D/Guschin/Guschin.vcxproj b/task05_2D/Guschin/Guschin.vcxproj new file mode 100644 index 0000000..e9575fa --- /dev/null +++ b/task05_2D/Guschin/Guschin.vcxproj @@ -0,0 +1,143 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + {7FD831C6-82A0-4285-8C15-41D557E54312} + v4.7.2 + ManagedCProj + Guschin + 10.0.18362.0 + + + + Application + true + v142 + true + Unicode + + + Application + false + v142 + true + Unicode + + + Application + true + v142 + true + Unicode + + + Application + false + v142 + true + Unicode + + + + + + + + + + + + + + + + + + + + + + + Level3 + WIN32;_DEBUG;%(PreprocessorDefinitions) + + + + Windows + Main + + + + + Level3 + _DEBUG;%(PreprocessorDefinitions) + + + + Windows + Main + + + + + Level3 + WIN32;NDEBUG;%(PreprocessorDefinitions) + + + + + + + + Level3 + NDEBUG;%(PreprocessorDefinitions) + + + + + + + + + + CppForm + + + + + + + + + + + + + + + + MyForm.h + + + + + + \ No newline at end of file diff --git a/task05_2D/Guschin/Guschin.vcxproj.filters b/task05_2D/Guschin/Guschin.vcxproj.filters new file mode 100644 index 0000000..5497660 --- /dev/null +++ b/task05_2D/Guschin/Guschin.vcxproj.filters @@ -0,0 +1,41 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Файлы заголовков + + + Файлы заголовков + + + Файлы заголовков + + + Файлы заголовков + + + + + Исходные файлы + + + + + Файлы ресурсов + + + \ No newline at end of file diff --git a/task05_2D/Guschin/Guschin.vcxproj.user b/task05_2D/Guschin/Guschin.vcxproj.user new file mode 100644 index 0000000..824d5a9 --- /dev/null +++ b/task05_2D/Guschin/Guschin.vcxproj.user @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/task05_2D/Guschin/Matrix.h b/task05_2D/Guschin/Matrix.h new file mode 100644 index 0000000..a47ee6e --- /dev/null +++ b/task05_2D/Guschin/Matrix.h @@ -0,0 +1,275 @@ +#pragma once + +class vec2 +{ +public: + float x, y; + vec2() {} + vec2(float a, float b) : x(a), y(b) {} + + vec2& operator*=(const vec2& v) + { + x *= v.x; + y *= v.y; + return *this; + } + + const vec2 operator*(const vec2& v) + { + return vec2(*this) *= v; + } + + float& operator[](int i) + { + return ((float*)this)[i]; + } +}; + +class vec3 +{ +public: + float x, y, z; + vec3() {} + vec3(float a, float b, float c) : x(a), y(b), z(c) {} + vec3(vec2 v, float c) : vec3(v.x, v.y, c) {} + + vec3& operator*=(const vec3& v) + { + x *= v.x; + y *= v.y; + z *= v.z; + return *this; + } + + const vec3 operator*(const vec3& v) + { + return vec3(*this) *= v; + } + + float& operator[](int i) + { + return ((float*)this)[i]; + } +}; + +class vec4 +{ +public: + float x, y, z, a; + vec4() {} + vec4(float a, float b, float c, float d) : x(a), y(b), z(c), a(d) {} + vec4(vec3 v, float c) : vec4(v.x, v.y, v.z, c) {} + + vec4& operator *= (const vec4& v) + { + x *= v.x; + y *= v.y; + a *= v.z; + a *= v.a; + return *this; + } + + const vec4 operator* (const vec4& v) + { + return vec4(*this) *= v; + } + + float& operator[] (int i) + { + return ((float*)this)[i]; + } +}; + +float dot(vec2 v1, vec2 v2) +{ + vec2 tmp = v1 * v2; + return tmp.x + tmp.y; +} + +float dot(vec3 v1, vec3 v2) +{ + vec3 tmp = v1 * v2; + return tmp.x + tmp.y + tmp.z; +} + +float dot(vec4 v1, vec4 v2) +{ + vec4 tmp = v1 * v2; + return tmp.x + tmp.y + tmp.z + tmp.a; +} + +class mat4 +{ +public: + vec4 row1, row2, row3, row4; + mat4() {} + mat4(vec4 r1, vec4 r2, vec4 r3, vec4 r4) : row1(r1), row2(r2), row3(r3), row4(r4) {} + mat4(float a) + { + row1 = vec4(a, 0.f, 0.f, 0.f); + row2 = vec4(0.f, a, 0.f, 0.f); + row3 = vec4(0.f, 0.f, a, 0.f); + row3 = vec4(0.f, 0.f, 0.f, a); + } + + vec4& operator[](int i) + { + return ((vec4*)this)[i]; + } + + mat4 transpose() + { + mat4 tmp(*this); + for (int i = 0; i < 4; ++i) + for (int j = 0; j < 4; ++j) + (*this)[i][j] = tmp[j][i]; + return *this; + } + + const vec4 operator*(const vec4& v) + { + vec4* res = new(vec4); + for (int i = 0; i < 3; ++i) + (*res)[i] = dot((*this)[i], v); + return *res; + } + + mat4& operator*=(const mat4& m) + { + mat4 A(*this), B(m); + B.transpose(); + for (int i = 0; i < 4; ++i) + (*this)[i] = A * B[i]; + return (*this).transpose(); + } + + const mat4 operator*(const mat4& m) + { + return mat4(*this) *= m; + } +}; + +class mat3 +{ +public: + vec3 row1, row2, row3; + mat3() {} + mat3(vec3 r1, vec3 r2, vec3 r3) : row1(r1), row2(r2), row3(r3) {} + mat3(float a) + { + row1 = vec3(a, 0.f, 0.f); + row2 = vec3(0.f, a, 0.f); + row3 = vec3(0.f, 0.f, a); + } + mat3(mat4 m) { + row1 = vec3(m.row1.x, m.row1.y, m.row1.z); + row2 = vec3(m.row2.x, m.row2.y, m.row2.z); + row3 = vec3(m.row3.x, m.row3.y, m.row3.z); + } + + vec3& operator[](int i) + { + return ((vec3*)this)[i]; + } + + mat3 transpose() + { + mat3 tmp(*this); + for (int i = 0; i < 3; ++i) + for (int j = 0; j < 3; ++j) + (*this)[i][j] = tmp[j][i]; + return *this; + } + + const vec3 operator*(const vec3& v) + { + vec3* res = new(vec3); + for (int i = 0; i < 3; ++i) + (*res)[i] = dot((*this)[i], v); + return *res; + } + + mat3& operator*=(const mat3& m) + { + mat3 A(*this), B(m); + B.transpose(); + for (int i = 0; i < 3; ++i) + (*this)[i] = A * B[i]; + return (*this).transpose(); + } + + const mat3 operator*(const mat3& m) + { + return mat3(*this) *= m; + } +}; + + +class mat2 +{ +public: + vec2 row1, row2; + + mat2(vec2 r1, vec2 r2) : row1(r1), row2(r2) {} + mat2(float a) + { + row1 = vec2(a, 0.f); + row2 = vec2(0.f, a); + } + mat2() {} + mat2(mat3 m) + { + row1 = vec2(m[0][0], m[0][1]); + row2 = vec2(m[1][0], m[1][1]); + } + + vec2& operator[](int i) + { + return ((vec2*)this)[i]; + } + + mat2 transpose() + { + mat2 tmp(*this); + for (int i = 0; i < 2; i++) + for (int j = 0; j < 2; j++) + (*this)[i][j] = tmp[j][i]; + return *this; + } + + const vec2 operator* (const vec2 &v) + { + vec2* res = new(vec2); + for (int i = 0; i < 2; i++) + { + (*res)[i] = dot((*this)[i], v); + } + return *res; + } + + mat2& operator*= (const mat2 &m) + { + mat2 A(*this), B(m); + B.transpose(); + for (int i = 0; i < 2; i++) + (*this)[i] = A * B[i]; + + return (*this).transpose(); + } + + const mat2 operator* (const mat2 &m) + { + return mat2(*this) *= m; + } +}; + +vec2 normalize(vec3 v) +{ + return vec2(v.x / v.z, v.y / v.z); +} + +vec3 normalize(vec4 v) +{ + return vec3(v.x / v.a, v.y / v.a, v.z / v.a); +} + diff --git a/task05_2D/Guschin/MyForm.cpp b/task05_2D/Guschin/MyForm.cpp new file mode 100644 index 0000000..bb5fa5d --- /dev/null +++ b/task05_2D/Guschin/MyForm.cpp @@ -0,0 +1,17 @@ +#include "Matrix.h" +#include +#include "Transform.h" +#include "Clip.h" +#include +#include "MyForm.h" + +using namespace System; +using namespace System::Windows::Forms; + +[STAThreadAttribute] +void Main(array ^ args) { + Application::EnableVisualStyles(); + Application::SetCompatibleTextRenderingDefault(false); + Guschin::MyForm form; + Application::Run(% form); +} \ No newline at end of file diff --git a/task05_2D/Guschin/MyForm.h b/task05_2D/Guschin/MyForm.h new file mode 100644 index 0000000..7e76f41 --- /dev/null +++ b/task05_2D/Guschin/MyForm.h @@ -0,0 +1,189 @@ +#pragma once + +namespace Guschin { + + using namespace System; + using namespace System::ComponentModel; + using namespace System::Collections; + using namespace System::Windows::Forms; + using namespace System::Data; + using namespace System::Drawing; + using namespace std; + + vec2 Vc; + vec2 V; + vec2 Vc_work, V_work; + mat3 T; + mat3 initT; + + public ref class MyForm : public System::Windows::Forms::Form + { + public: + MyForm(void) + { + InitializeComponent(); + } + + protected: + ~MyForm() + { + if (components) + { + delete components; + } + } + + private: + System::ComponentModel::Container ^components; + +#pragma region Windows Form Designer generated code + void InitializeComponent(void) + { + this->SuspendLayout(); + // + // MyForm + // + this->AutoScaleDimensions = System::Drawing::SizeF(8, 16); + this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font; + this->ClientSize = System::Drawing::Size(737, 686); + this->KeyPreview = true; + this->Margin = System::Windows::Forms::Padding(4, 4, 4, 4); + this->Name = L"MyForm"; + this->Text = L"MyForm"; + this->Load += gcnew System::EventHandler(this, &MyForm::MyForm_Load); + this->Paint += gcnew System::Windows::Forms::PaintEventHandler(this, &MyForm::MyForm_Paint); + this->KeyDown += gcnew System::Windows::Forms::KeyEventHandler(this, &MyForm::MyForm_KeyDown); + this->Resize += gcnew System::EventHandler(this, &MyForm::MyForm_Resize); + this->ResumeLayout(false); + + } +#pragma endregion + private: + float left = 30, right = 100, top = 20, bottom = 50; + float minX = left, maxX; + float minY = top, maxY; + float Wcx = left, Wcy; + float Wx, Wy; + + private: + System::Void rectCalc() { + maxX = ClientRectangle.Width - right; + maxY = ClientRectangle.Height - bottom; + Wcy = maxY; + Wx = maxX - left; + Wy = maxY - top; + } + System::Void worldRectCalc() + { + Vc_work = normalize(T * vec3(Vc, 1.f)); + V_work = mat2(T) * V; + } + float f(float x) + { + return tan(x); + } + bool f_exists(float x, float delta) + { + return fabs(2.f * acos(cos(x)) - Math::PI) > delta; + } + private: + System::Void MyForm_Resize(System::Object^ sender, System::EventArgs^ e) { + rectCalc(); + Refresh(); + } + private: + System::Void MyForm_Paint(System::Object^ sender, System::Windows::Forms::PaintEventArgs^ e) { + Graphics^ g = e->Graphics; + g->Clear(Color::Aquamarine); + + Pen^ rectPen = gcnew Pen(Color::Black, 2); + g->DrawRectangle(rectPen, left, top, Wx, Wy); + + Pen^ pen = gcnew Pen(Color::Blue, 1); + + float deltaX = V_work.x / Wx; + bool hasStart, hasEnd; + vec2 start, end; + float x, y; + + start.x = Wcx; + x = Vc_work.x; + hasStart = f_exists(x, deltaX); + if (hasStart) + { + y = f(x); + start.y = Wcy - (y - Vc_work.y) / V_work.y * Wy; + } + + float deltaY; + float red, green, blue; + while (start.x < maxX) + { + end.x = start.x + 1.f; + x += deltaX; + hasEnd = f_exists(x, deltaX); + if (hasEnd) + { + y = f(x); + deltaY = (y - Vc_work.y) / V_work.y; + end.y = Wcy - deltaY * Wy; + } + + vec2 tmpEnd = end; + bool visible = hasStart && hasEnd && clip(start, end, minX, minY, maxX, maxY); + if (visible) { + if (deltaY > 1.f) deltaY = 1.f; + if (deltaY < 0.f) deltaY = 0.f; + green = 510.f * deltaY; + if (deltaY < 0.5) + { + blue = 255.f - green; + red = 0.f; + } + else + { + blue = 0.f; + red = green - 255.f; + green = 510.f - green; + } + pen->Color = Color::FromArgb(red, green, blue); + g->DrawLine(pen, start.x, start.y, end.x, end.y); + } + start = tmpEnd; + hasStart = hasEnd; + } + } + private: + System::Void MyForm_Load(System::Object^ sender, System::EventArgs^ e) { + initT = mat3(1.f); + T = initT; + Vc = vec2(-2.f, -2.f); + V = vec2(4.f, 4.f); + rectCalc(); + worldRectCalc(); + } + private: + System::Void MyForm_KeyDown(System::Object^ sender, System::Windows::Forms::KeyEventArgs^ e) { + float centerX = Vc_work.x + V_work.x / 2; + float centerY = Vc_work.y + V_work.y / 2; + + switch (e->KeyCode) { + case Keys::Escape: + T = initT; + break; + case Keys::A: + T = translate(-V_work.x / Wx, 0.f) * T; + break; + case Keys::Z: + T = translate(-centerX, -centerY) * T; + T = scale(1.1) * T; + T = translate(centerX, centerY) * T; + break; + default: + break; + } + worldRectCalc(); + Refresh(); + } + }; +} diff --git a/task05_2D/Guschin/MyForm.resx b/task05_2D/Guschin/MyForm.resx new file mode 100644 index 0000000..d58980a --- /dev/null +++ b/task05_2D/Guschin/MyForm.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/task05_2D/Guschin/Transform.h b/task05_2D/Guschin/Transform.h new file mode 100644 index 0000000..d606647 --- /dev/null +++ b/task05_2D/Guschin/Transform.h @@ -0,0 +1,45 @@ +#pragma once + +mat3 translate(float Tx, float Ty) +{ + mat3* res = new mat3(1.f); + (*res)[0][2] = Tx; + (*res)[1][2] = Ty; + return *res; +} + +mat3 scale(float Sx, float Sy) +{ + mat3* res = new mat3(1.f); + (*res)[0][0] = Sx; + (*res)[1][1] = Sy; + return *res; +} + +mat3 scale(float S) +{ + return scale(S, S); +} + +mat3 rotate(float theta) +{ + mat3* res = new mat3(1.f); + (*res)[0][0] = (*res)[1][1] = (float)cos(theta); + (*res)[0][1] = (float)sin(theta); + (*res)[1][0] = -(*res)[0][1]; + return *res; +} + +mat3 mirrorX() +{ + mat3* res = new mat3(1.f); + (*res)[1][1] = -1; + return *res; +} + +mat3 mirrorY() +{ + mat3* res = new mat3(1.f); + (*res)[0][0] = -1; + return *res; +} \ No newline at end of file -- cgit v1.2.3