summaryrefslogtreecommitdiff
path: root/task05_3D/Guschin/MyForm.h
blob: 369f2e818f31df79b67c0cc539ee875de0812255 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#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;

	vec3 Vc;
	vec3 V;
	vec3 Vc_work, V_work;
	mat4 T;
	mat4 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 * vec4(Vc, 1.f));
			V_work = mat3(T) * V;
		}
		float f(float x, float z)
		{
			return x * sin(sqrtf(x * x + z * z));
		}
		bool f_exists(float x, float z, float delta)
		{
			return true;
		}
	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;
			float z = Vc_work.z;
			vec2 start, end;
			float x, y;

			start.x = Wcx;
			x = Vc_work.x;
			hasStart = f_exists(x, z, deltaX);
			if (hasStart)
			{
				y = f(x, z);
				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, z, deltaX);
				if (hasEnd)
				{
					y = f(x, z);
                    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 = mat4(1.f);
			T = initT;
			Vc = vec3(-2.f, -2.f, -2.f);
			V = vec3(4.f, 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;
			float centerZ = Vc_work.z + V_work.z / 2;

			switch (e->KeyCode) {
			case Keys::Escape:
				T = initT;
				break;
			case Keys::A:
				T = translate(-V_work.x / Wx, 0.f, 0.f) * T;
				break;
			case Keys::Z:
				T = translate(-centerX, -centerY, -centerZ) * T;
				T = scale(1.1, 1.1, 1.1) * T;
				T = translate(centerX, centerY, centerZ) * T;
				break;
			default:
				break;
			}
			worldRectCalc();
			Refresh();
		}
	};
}