-
Notifications
You must be signed in to change notification settings - Fork 0
/
Unit1.pas
306 lines (260 loc) · 7.12 KB
/
Unit1.pas
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls;
type
Matrix = array [0..3, 0..3] of real;
TPoint3D = record
x, y, z, w : integer;
end;
TForm1 = class(TForm)
pb1: TPaintBox;
tmr1: TTimer;
btn1: TButton;
lbl1: TLabel;
lbl2: TLabel;
procedure Points3DtoPoints2D(P3:array of TPoint3D; var P2:array of TPoint);
function MulMatrix(a,b:Matrix):Matrix;
function P3MulMatrix(P:TPoint3D; A:Matrix):TPoint;
function MatrixS(X, Y, Z:real):Matrix;
function MatrixPz(C:real):Matrix;
function MatrixRx(f:real):Matrix;
function MatrixRz(f:real):Matrix;
function MatrixRy(f:real):Matrix;
procedure Draw;
procedure tmr1Timer(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure btn1Click(Sender: TObject);
procedure pb1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
private
public
end;
var
Form1: TForm1;
MainMatrix: Matrix;
x1,y1:integer;
const Points3D : array [0..7] of TPoint3D =
((x: 100; y: 100; z: 100; w:1), //0
(x:-100; y: 100; z: 100; w:1), //1
(x:-100; y:-100; z: 100; w:1), //2
(x: 100; y:-100; z: 100; w:1), //3
(x: 100; y: 100; z:-100; w:1), //4
(x:-100; y: 100; z:-100; w:1), //5
(x:-100; y:-100; z:-100; w:1), //6
(x: 100; y:-100; z:-100; w:1));//7
implementation
{$R *.dfm}
{Создание матрицы поворота
Преобразование 3д координат вершин куба 2д координаты и одновременно применение матрицы поворота
}
//Преобразование 3д координат в 2д
procedure TForm1.Points3DtoPoints2D(P3:array of TPoint3D; var P2:array of TPoint);
var a,b:Matrix;
i:integer;
begin
a:=MatrixS(1.4, 1.4, 1.4);
a:=MulMatrix(a,MainMatrix);
b:=MatrixPz(500);
//Масштабирование
a:=MulMatrix(a,b); //Применение матрицы поворота
//Перспектива
for i:=low(P3) to High(P3) do
begin
P2[i]:=P3MulMatrix(P3[i],a);
P2[i].x:=P2[i].x + 300;
P2[i].y:=P2[i].y + 300;
end;
end;
//Умножение матриц
function TForm1.MulMatrix(a,b:Matrix):Matrix;
var i,j:integer;
begin
for i:=0 to 3 do
for j:=0 to 3 do
Result[i,j]:=a[0,j]*b[i,0]+a[1,j]*b[i,1]+a[2,j]*b[i,2]+a[3,j]*b[i,3];
end;
// Умножение матрицы на точку
function TForm1.P3MulMatrix(P:TPoint3D; A:Matrix):TPoint;
var w:real;
begin
w:=P.X*A[3,0]+P.Y*A[3,1]+P.Z*A[3,2]+P.w*A[3,3];
Result.X:=trunc((P.X*A[0,0]+P.Y*A[0,1]+P.Z*A[0,2]+P.w*A[0,3])/w);
Result.Y:=trunc((P.X*A[1,0]+P.Y*A[1,1]+P.Z*A[1,2]+P.w*A[1,3])/w);
end;
function TForm1.MatrixS(X, Y, Z:real):Matrix;
var a:Matrix;
i,j:integer; //масштабирование
begin
for i:=0 to 3 do
for j:=0 to 3 do
a[i,j]:=0;
a[0,0]:=X; // X, 0, 0, 0,
a[1,1]:=Y; // 0, Y, 0, 0,
a[2,2]:=Z; // 0, 0, Z, 0,
a[3,3]:=1; // 0, 0, 0, 1
Result:=a;
end;
function TForm1.MatrixPz(C:real):Matrix; //перспектива
var a:Matrix;
i,j:integer;
begin
for i:=0 to 3 do
for j:=0 to 3 do
a[i,j]:=0;
a[3,2]:=-1/c;
a[0,0]:=1; // 1, 0, 0, 0,
a[1,1]:=1; // 0, 1, 0, 0,
a[2,2]:=1; // 0, 0, 1, -1/c,
a[3,3]:=1; // 0, 0, 0, 1
Result:=a;
end;
function TForm1.MatrixRx(f:real):Matrix; //поворот по х
var a:Matrix;
i,j:integer;
begin
for i:=0 to 3 do
for j:=0 to 3 do
a[i,j]:=0;
a[0,0]:=1; // 1, 0 , 0 , 0,
a[1,1]:=cos(f); // 0, cos(f), sin(f), 0,
a[2,2]:=cos(f); // 0, -sin(f), cos(f), 0,
a[1,2]:=-sin(f); // 0, 0 , 0 , 1
a[2,1]:=sin(f);
a[3,3]:=1;
Result:=a;
end;
function TForm1.MatrixRz(f:real):Matrix; //поворот по z
var a:Matrix;
i,j:integer;
begin
for i:=0 to 3 do
for j:=0 to 3 do
a[i,j]:=0;
a[0,0]:=cos(f); // cos(f), sin(f), 0, 0,
a[1,1]:=cos(f); // -sin(f), cos(f), 0, 0,
a[2,2]:=1; // 0 , 0 , 1, 0,
a[3,3]:=1; // 0 , 0 , 0, 1
a[0,1]:=-sin(f);
a[1,0]:=sin(f);
Result:=a;
end;
function TForm1.MatrixRy(f:real):Matrix;
var a:Matrix;
i,j:integer;
begin
for i:=0 to 3 do // cos(f), 0, sin(f), 0,
for j:=0 to 3 do // 0 , 1, 0 , 0,
a[i,j]:=0; // -sin(f), 0, cos(f), 0,
// 0 , 0, 0 , 1
a[0,0]:=cos(f);
a[1,1]:=1;
a[2,2]:=cos(f);
a[3,3]:=1;
a[0,2]:=sin(f);
a[2,0]:=-sin(f);
Result:=a;
end;
procedure TForm1.Draw;
var P : array [0..7] of TPoint;
i:integer;
begin
Points3DToPoints2D(Points3D, P);
pb1.Canvas.brush.Color := clWhite;
pb1.Canvas.fillRect(Rect(0, 0, pb1.Width, pb1.Height));
pb1.Canvas.Pen.Color := clGreen;
pb1.Canvas.MoveTo(P[0].x, P[0].y);
pb1.Canvas.LineTo(P[1].x, P[1].y);
pb1.Canvas.LineTo(P[2].x, P[2].y);
pb1.Canvas.LineTo(P[3].x, P[3].y);
pb1.Canvas.LineTo(P[0].x, P[0].y);
pb1.Canvas.MoveTo(P[4].x, P[4].y);
pb1.Canvas.LineTo(P[5].x, P[5].y);
pb1.Canvas.LineTo(P[6].x, P[6].y);
pb1.Canvas.LineTo(P[7].x, P[7].y);
pb1.Canvas.LineTo(P[4].x, P[4].y);
pb1.Canvas.MoveTo(P[1].x, P[1].y);
pb1.Canvas.LineTo(P[5].x, P[5].y);
pb1.Canvas.MoveTo(P[2].x, P[2].y);
pb1.Canvas.LineTo(P[6].x, P[6].y);
pb1.Canvas.MoveTo(P[3].x, P[3].y);
pb1.Canvas.LineTo(P[7].x, P[7].y);
pb1.Canvas.MoveTo(P[0].x, P[0].y);
pb1.Canvas.LineTo(P[4].x, P[4].y);
end;
procedure TForm1.tmr1Timer(Sender: TObject);
var a,b,c:Matrix;
begin
a:=MatrixRz(pi/36);
b:=MatrixRx(pi/48);
c:=MatrixRy(pi/36);
a:=MulMatrix(a,b);
a:=MulMatrix(a,c);
MainMatrix:=MulMatrix(MainMatrix, a);
Draw;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
MainMatrix:=MatrixRz(0);
Draw;
x1:=0;
y1:=0;
end;
procedure TForm1.btn1Click(Sender: TObject);
var a,b:matrix;
begin
if tmr1.Enabled then tmr1.Enabled:=False
else tmr1.Enabled:=True;
end;
procedure TForm1.pb1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
var w,val:integer;
step:real;
b:Matrix;
fl:Boolean;
begin
{w:=pb1.Width;
step:=(2*pi)/(360*(w/20));
val:=trunc(x/step);
MainMatrix:=MatrixRx(0);
b:=MatrixRx(val*step);
MainMatrix:=MulMatrix(MainMatrix, b);
Draw; }
if x mod 20 = 0 then
begin
if x<x1 then
begin
b:=MatrixRy(pi/24);
MainMatrix:=MulMatrix(MainMatrix, b);
lbl1.Caption:=inttostr(x);
lbl2.Caption:=inttostr(x1);
end
else
if x>x1 then
begin
b:=MatrixRy(-pi/24);
MainMatrix:=MulMatrix(MainMatrix, b);
end;
x1:=x;
end;
if y mod 20 = 0 then
begin
if y<y1 then
begin
b:=MatrixRx(pi/15);
MainMatrix:=MulMatrix(MainMatrix, b);
lbl1.Caption:=inttostr(x);
lbl2.Caption:=inttostr(x1);
end
else
if y>y1 then
begin
b:=MatrixRx(-pi/15);
MainMatrix:=MulMatrix(MainMatrix, b);
end;
y1:=y;
end;
Draw;
end;
end.