This repository was archived by the owner on Jan 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTrans.frm
More file actions
629 lines (568 loc) · 18.3 KB
/
Trans.frm
File metadata and controls
629 lines (568 loc) · 18.3 KB
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
VERSION 5.00
Begin VB.Form frmTrans
Appearance = 0 'Flat
BackColor = &H00C0C0C0&
Caption = "Trans"
ClientHeight = 6225
ClientLeft = 690
ClientTop = 615
ClientWidth = 7830
BeginProperty Font
Name = "MS Sans Serif"
Size = 8.25
Charset = 0
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
ForeColor = &H80000008&
KeyPreview = -1 'True
LinkTopic = "Form1"
PaletteMode = 1 'UseZOrder
ScaleHeight = 415
ScaleMode = 3 'Pixel
ScaleWidth = 522
Begin VB.CheckBox chkHideSurfaces
Caption = "Hide Surfaces"
Height = 255
Left = 0
TabIndex = 22
Top = 5280
Width = 2295
End
Begin VB.CommandButton cmdTransform
Caption = "Transform"
Default = -1 'True
Height = 495
Left = 600
TabIndex = 12
Top = 5640
Width = 1095
End
Begin VB.Frame Frame1
Caption = "Transformations"
Height = 2535
Left = 0
TabIndex = 7
Top = 2640
Width = 2295
Begin VB.OptionButton optTransformation
Caption = "Z Rotate"
Height = 255
Index = 8
Left = 120
TabIndex = 19
Top = 2160
Width = 2055
End
Begin VB.OptionButton optTransformation
Caption = "Y Rotate"
Height = 255
Index = 7
Left = 120
TabIndex = 18
Top = 1920
Width = 2055
End
Begin VB.OptionButton optTransformation
Caption = "X Rotate"
Height = 255
Index = 6
Left = 120
TabIndex = 17
Top = 1680
Width = 2055
End
Begin VB.OptionButton optTransformation
Caption = "Grow, Rotate"
Height = 255
Index = 5
Left = 120
TabIndex = 16
Top = 1440
Width = 2055
End
Begin VB.OptionButton optTransformation
Caption = "Wierd"
Height = 255
Index = 4
Left = 120
TabIndex = 15
Top = 1200
Width = 2055
End
Begin VB.OptionButton optTransformation
Caption = "Up, Shrink/Grow"
Height = 255
Index = 3
Left = 120
TabIndex = 11
Top = 960
Width = 2055
End
Begin VB.OptionButton optTransformation
Caption = "Up, Shrink, Twist"
Height = 255
Index = 2
Left = 120
TabIndex = 10
Top = 720
Width = 2055
End
Begin VB.OptionButton optTransformation
Caption = "Up, Shrink"
Height = 255
Index = 1
Left = 120
TabIndex = 9
Top = 480
Width = 2055
End
Begin VB.OptionButton optTransformation
Caption = "Up, Twist"
Height = 255
Index = 0
Left = 120
TabIndex = 8
Top = 240
Value = -1 'True
Width = 2055
End
End
Begin VB.Frame Frame2
Caption = "Curve"
Height = 2535
Left = 0
TabIndex = 1
Top = 0
Width = 2295
Begin VB.OptionButton optCurve
Caption = "Off Center Hexagon"
Height = 255
Index = 8
Left = 120
TabIndex = 21
Top = 2160
Width = 2055
End
Begin VB.OptionButton optCurve
Caption = "Hexagon"
Height = 255
Index = 7
Left = 120
TabIndex = 20
Top = 1920
Width = 2055
End
Begin VB.OptionButton optCurve
Caption = "Semicircle"
Height = 255
Index = 6
Left = 120
TabIndex = 14
Top = 1680
Width = 2055
End
Begin VB.OptionButton optCurve
Caption = "Triangle"
Height = 255
Index = 0
Left = 120
TabIndex = 13
Top = 240
Value = -1 'True
Width = 2055
End
Begin VB.OptionButton optCurve
Caption = "Star"
Height = 255
Index = 5
Left = 120
TabIndex = 6
Top = 1440
Width = 2055
End
Begin VB.OptionButton optCurve
Caption = "Off Center Circle"
Height = 255
Index = 4
Left = 120
TabIndex = 5
Top = 1200
Width = 2055
End
Begin VB.OptionButton optCurve
Caption = "Circle"
Height = 255
Index = 3
Left = 120
TabIndex = 4
Top = 960
Width = 2055
End
Begin VB.OptionButton optCurve
Caption = "Off Center Square"
Height = 255
Index = 2
Left = 120
TabIndex = 3
Top = 720
Width = 2055
End
Begin VB.OptionButton optCurve
Caption = "Square"
Height = 255
Index = 1
Left = 120
TabIndex = 2
Top = 480
Width = 2055
End
End
Begin VB.PictureBox picCanvas
AutoRedraw = -1 'True
Height = 5775
Left = 2400
ScaleHeight = 381
ScaleMode = 3 'Pixel
ScaleWidth = 357
TabIndex = 0
Top = 0
Width = 5415
End
End
Attribute VB_Name = "frmTrans"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
' Location of viewing eye.
Private EyeR As Single
Private EyeTheta As Single
Private EyePhi As Single
Private Const dtheta = PI / 20
Private Const Dphi = PI / 20
Private Const dR = 1
' Location of focus point.
Private Const FocusX = 0#
Private Const FocusY = 0#
Private Const FocusZ = 0#
Private Projector(1 To 4, 1 To 4) As Single
Private SelectedCurve As Integer
Private SelectedTransformation As Integer
Private NumTrans As Integer
Private trans() As Transformation
Private TheSurface As Transformed3d
' Create the selected curve.
Private Sub CreateCurve()
Dim r As Single
Dim r2 As Single
Dim dtheta As Single
Dim theta As Single
Dim Y As Single
Dim i As Integer
Select Case SelectedCurve
Case 0 ' Triangle.
TheSurface.AddCurvePoint 2 * Cos(0), 0, 2 * Sin(0)
TheSurface.AddCurvePoint 2 * Cos(4 * PI / 3), 0, 2 * Sin(4 * PI / 3)
TheSurface.AddCurvePoint 2 * Cos(2 * PI / 3), 0, 2 * Sin(2 * PI / 3)
TheSurface.AddCurvePoint 2 * Cos(0), 0, 2 * Sin(0)
Case 1 ' Square.
TheSurface.AddCurvePoint -2, 0, -2
TheSurface.AddCurvePoint -2, 0, 2
TheSurface.AddCurvePoint 2, 0, 2
TheSurface.AddCurvePoint 2, 0, -2
TheSurface.AddCurvePoint -2, 0, -2
Case 2 ' Off Center Square.
TheSurface.AddCurvePoint 1, 0, 1
TheSurface.AddCurvePoint 1, 0, 3
TheSurface.AddCurvePoint 3, 0, 3
TheSurface.AddCurvePoint 3, 0, 1
TheSurface.AddCurvePoint 1, 0, 1
Case 3 ' Circle.
r = 2
dtheta = PI / 8
For theta = 0 To 2 * PI - dtheta + 0.01 Step dtheta
TheSurface.AddCurvePoint r * Cos(theta), 0, r * Sin(theta)
Next theta
TheSurface.AddCurvePoint r, 0, 0
Case 4 ' Off Center Circle.
r = 1
dtheta = PI / 8
For theta = 0 To 2 * PI - dtheta + 0.01 Step dtheta
TheSurface.AddCurvePoint 2 + r * Cos(theta), 0, 2 + r * Sin(theta)
Next theta
TheSurface.AddCurvePoint 2 + r, 0, 2
Case 5 ' Star.
r = 2
r2 = 1
dtheta = 2 * PI / 5 / 2
theta = PI
For i = 1 To 5
TheSurface.AddCurvePoint _
r * Cos(theta), 0, r * Sin(theta)
theta = theta + dtheta
TheSurface.AddCurvePoint _
r2 * Cos(theta), 0, r2 * Sin(theta)
theta = theta + dtheta
Next i
TheSurface.AddCurvePoint _
r * Cos(PI), 0, r * Sin(PI)
Case 6 ' Semicircle.
r = 2
dtheta = PI / 8
For theta = 0 To PI - dtheta + 0.01 Step dtheta
TheSurface.AddCurvePoint r * Cos(theta), 0, r * Sin(theta)
Next theta
TheSurface.AddCurvePoint -r, 0, 0
Case 7 ' Hexagon.
r = 3
dtheta = 2 * PI / 6
theta = 0
For i = 1 To 7
TheSurface.AddCurvePoint _
r * Cos(theta), 0, r * Sin(theta)
theta = theta + dtheta
Next i
Case 8 ' Off Center Hexagon.
r = 2
dtheta = 2 * PI / 6
theta = 0
For i = 1 To 7
TheSurface.AddCurvePoint _
r * Cos(theta), 0, r + r * Sin(theta)
theta = theta + dtheta
Next i
End Select
End Sub
' Create the array of transformations.
Private Sub CreateTransformations()
Dim A(1 To 4, 1 To 4) As Single
Dim B(1 To 4, 1 To 4) As Single
Dim C(1 To 4, 1 To 4) As Single
Dim theta As Single
Dim dtheta As Single
Dim r As Single
Dim Y As Single
Dim i As Integer
Select Case SelectedTransformation
Case 0 ' Up, twist.
NumTrans = 9
ReDim trans(1 To NumTrans)
dtheta = PI / 12
For i = 1 To NumTrans
Y = i / 2
theta = i * dtheta
m3Translate A, 0, Y, 0 ' Translate.
m3YRotate B, theta ' Rotate.
m3MatMultiply trans(i).M, A, B ' Combine.
Next i
Case 1 ' Up, shrink.
NumTrans = 9
ReDim trans(1 To NumTrans)
For i = 1 To NumTrans
Y = i / 2
r = (NumTrans - i) / NumTrans
m3Scale A, r, 1, r ' Scale.
m3Translate B, 0, Y, 0 ' Translate.
m3MatMultiply trans(i).M, A, B ' Combine.
Next i
Case 2 ' Up, shrink, twist.
NumTrans = 9
ReDim trans(1 To NumTrans)
dtheta = PI / 12
For i = 1 To NumTrans
Y = i / 2
r = (NumTrans - i) / NumTrans
theta = i * dtheta
m3Scale A, r, 1, r ' Scale.
m3Translate B, 0, Y, 0 ' Translate.
m3MatMultiply C, A, B ' Combine A and B.
m3YRotate A, theta ' Rotate.
m3MatMultiply trans(i).M, C, A ' Combine all.
Next i
Case 3 ' Up, shrink/grow.
NumTrans = 18
ReDim trans(1 To NumTrans)
dtheta = PI / 12
For i = 1 To NumTrans
Y = i / 4
theta = i * dtheta
r = 1 + Sin(2 * theta) / 2
m3Scale A, r, 1, r ' Scale.
m3Translate B, 0, Y, 0 ' Translate.
m3MatMultiply trans(i).M, A, B ' Combine.
Next i
Case 4 ' Waver.
' Make the curve move upwards with
' varying rotation around the Z axis.
NumTrans = 18
ReDim trans(1 To NumTrans)
dtheta = PI / 12
r = PI / 2
For i = 1 To NumTrans
Y = i / 4
theta = i * dtheta
m3ZRotate A, r * Sin(theta) ' Rotate.
m3Translate B, 0, Y, 0 ' Translate.
m3MatMultiply trans(i).M, A, B ' Combine.
Next i
Case 5 ' Grow and rotate.
' Make the curve grow and rotate
' around the Z axis.
NumTrans = 18
ReDim trans(1 To NumTrans)
dtheta = PI / 12
r = PI / 2
For i = 1 To NumTrans
Y = i / 4
theta = i * dtheta
m3ZRotate A, r * Sin(theta) ' Rotate.
m3Scale B, i / 9, i / 9, i / 9 ' Scale
m3MatMultiply trans(i).M, A, B ' Combine.
Next i
Case 6 ' Rotate around the X axis.
' Rotate around the X axis.
NumTrans = 18
ReDim trans(1 To NumTrans)
dtheta = 2 * PI / NumTrans
r = PI / 2
For i = 1 To NumTrans
Y = i / 4
theta = i * dtheta
m3XRotate trans(i).M, theta ' Rotate.
Next i
Case 7 ' Rotate around the Y axis.
' Rotate around the Y axis.
NumTrans = 18
ReDim trans(1 To NumTrans)
dtheta = 2 * PI / NumTrans
r = PI / 2
For i = 1 To NumTrans
Y = i / 4
theta = i * dtheta
m3YRotate trans(i).M, theta ' Rotate.
Next i
Case 8 ' Rotate around the Z axis.
' Rotate around the Z axis.
NumTrans = 18
ReDim trans(1 To NumTrans)
dtheta = 2 * PI / NumTrans
r = PI / 2
For i = 1 To NumTrans
Y = i / 4
theta = i * dtheta
m3ZRotate trans(i).M, theta ' Rotate.
Next i
End Select
End Sub
Private Sub chkHideSurfaces_Click()
DrawData picCanvas
picCanvas.SetFocus
End Sub
' Create the surface.
Private Sub cmdTransform_Click()
Dim i As Integer
Screen.MousePointer = vbHourglass
DoEvents
Set TheSurface = New Transformed3d
CreateCurve
CreateTransformations
For i = 1 To NumTrans
TheSurface.SetTransformation trans(i).M
Next i
TheSurface.Transform
DrawData picCanvas
picCanvas.SetFocus
Screen.MousePointer = vbDefault
End Sub
' Save the curve choice.
Private Sub optCurve_Click(Index As Integer)
SelectedCurve = Index
End Sub
' Draw the data.
Private Sub DrawData(ByVal pic As PictureBox)
Dim X As Single
Dim Y As Single
Dim Z As Single
Dim S(1 To 4, 1 To 4) As Single
Dim T(1 To 4, 1 To 4) As Single
Dim ST(1 To 4, 1 To 4) As Single
Dim PST(1 To 4, 1 To 4) As Single
' Prevent overflow errors when drawing lines
' too far out of bounds.
On Error Resume Next
' Uncull the surface.
TheSurface.Culled = False
' Cull backfaces.
If chkHideSurfaces.value = vbChecked Then
m3SphericalToCartesian EyeR, EyeTheta, EyePhi, X, Y, Z
TheSurface.Cull X, Y, Z
End If
' Scale and translate so it looks OK in pixels.
m3Scale S, 30, -30, 1
m3Translate T, picCanvas.ScaleWidth / 2, picCanvas.ScaleHeight / 2, 0
m3MatMultiplyFull ST, S, T
m3MatMultiplyFull PST, Projector, ST
' Transform the surface and clip faces.
TheSurface.ApplyFull PST
' Clip faces behind the center of projection.
TheSurface.ClipEye EyeR
' Set the appropriate fill style.
If chkHideSurfaces.value = vbChecked Then
' Fill to cover hidden surfaces.
pic.FillStyle = vbFSSolid
pic.FillColor = RGB(&HC0, &HFF, &HC0)
Else
' Do not fill so all lines are visible.
pic.FillStyle = vbFSTransparent
End If
' Draw the surface.
pic.Cls
TheSurface.Draw pic, EyeR
pic.Refresh
End Sub
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case vbKeyLeft
EyeTheta = EyeTheta - dtheta
Case vbKeyRight
EyeTheta = EyeTheta + dtheta
Case vbKeyUp
EyePhi = EyePhi - Dphi
Case vbKeyDown
EyePhi = EyePhi + Dphi
Case Else
Exit Sub
End Select
m3PProject Projector, m3Perspective, EyeR, EyePhi, EyeTheta, FocusX, FocusY, FocusZ, 0, 1, 0
DrawData picCanvas
End Sub
Private Sub Form_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case Asc("+")
EyeR = EyeR + dR
Case Asc("-")
EyeR = EyeR - dR
Case Else
Exit Sub
End Select
m3PProject Projector, m3Perspective, EyeR, EyePhi, EyeTheta, FocusX, FocusY, FocusZ, 0, 1, 0
DrawData picCanvas
End Sub
Private Sub Form_Load()
' Initialize the eye position.
EyeR = 10
EyeTheta = PI * 0.2
EyePhi = PI * 0.1
' Initialize the projection transformation.
m3PProject Projector, m3Perspective, EyeR, EyePhi, EyeTheta, FocusX, FocusY, FocusZ, 0, 1, 0
Me.Show
optCurve_Click 0
End Sub
' Save the current transformation choice.
Private Sub optTransformation_Click(Index As Integer)
SelectedTransformation = Index
End Sub