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
|
#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 x * sin(x);
}
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;
vec2 start;
float x, y;
start.x = Wcx;
x = Vc_work.x;
y = f(x);
start.y = Wcy - (y - Vc_work.y) / V_work.y * Wy;
while (start.x < maxX)
{
vec2 end;
end.x = start.x + 1.f;
x += deltaX;
y = f(x);
end.y = Wcy - (y - Vc_work.y) / V_work.y * Wy;
vec2 tmpEnd = end;
bool visible = clip(start, end, minX, minY, maxX, maxY);
if (visible) {
g->DrawLine(pen, start.x, start.y, end.x, end.y);
}
start = tmpEnd;
}
}
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) {
switch (e->KeyCode) {
case Keys::Escape:
T = initT;
break;
default:
break;
}
Refresh();
}
};
}
|