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
| Option Explicit
Option Base 1
DefSng A-Z
Const pi = 3.14159265358979
Const sin60 = 0.866025403784439
Const cos60 = 0.5
Const tg60 = 1.73205080756888
Const DotsIn1 As Integer = 100 ' ×èñëî òî÷åê â ñòîðîíå ðàâíîñòîðîííåãî òðåóãîëüíèêà
Const H1 = DotsIn1 * sin60
Const C1 = -DotsIn1 * sin60
Const C2 = DotsIn1 * sin60 * 3
Const ScaleMin = DotsIn1 / 2 ' Ìèíèìàëüíàÿ âåëè÷èíà ñòîðîíû îñêîëêà
Const ScaleKoef = DotsIn1 / 2 'Äîïîëíèòåëüíûé ñëó÷àéíûé ôàêòîð óâåëè÷åíèÿ ñòîðîíû îñêîëêà
Const N As Integer = 10 ' ×èñëî ôèãóð ïåðâè÷íîé ãåíåðàöèè
Const ÑTri As Integer = 18 ' ×èñëî çåðêàëíûõ òðåóãîëüíèêîâ â ðÿäó
Const RTri As Integer = 4 ' ×èñëî ïàð ðÿäîâ çåðêàëíûõ òðåóãîëüíèêîâ
Const ZTri As Integer = ÑTri * RTri * 2 ' ×èñëî çåðêàëíûõ òðåóãîëüíèêà
Const M As Integer = ZTri * N ' ×èñëî âñåõ ôèãóð
Const Accuracy = 0.00003
Dim NVersh(M) As Integer, X(7, M), Y(7, M)
Sub Generate()
Dim e As Integer, i As Integer, j As Integer, k As Integer, q As Integer, AB, Phi
Dim ShColor(M) As Long
Dim MyShape As Shape
Dim VertArray()
Randomize Timer
Application.ScreenUpdating = False
For Each MyShape In Worksheets(1).Shapes
If MyShape.AutoShapeType > 0 Then
MyShape.Delete
End If
Next
ReDim VertArray(1 To 2, 1 To 2)
For i = 1 To 12
VertArray(1, 1) = (i - 0.5) * DotsIn1
VertArray(1, 2) = 0
VertArray(2, 1) = (i - 0.5 + 4) * DotsIn1
VertArray(2, 2) = H1 * 8
Set MyShape = Worksheets(1).Shapes.AddPolyline(VertArray)
With MyShape
.Line.Weight = 0.5
.Line.ForeColor.RGB = &H0
End With
VertArray(1, 1) = (i - 0.5) * DotsIn1
VertArray(1, 2) = 0
VertArray(2, 1) = (i - 0.5 + 4) * DotsIn1
VertArray(2, 2) = -H1 * 8
Set MyShape = Worksheets(1).Shapes.AddPolyline(VertArray)
With MyShape
.Line.Weight = 0.5
.Line.ForeColor.RGB = &H0
End With
Next
For i = 1 To 4
VertArray(1, 1) = 0
VertArray(1, 2) = H1 * (9 - 2 * i)
VertArray(2, 1) = (i - 0.5) * DotsIn1
VertArray(2, 2) = H1 * 8
Set MyShape = Worksheets(1).Shapes.AddPolyline(VertArray)
With MyShape
.Line.Weight = 0.5
.Line.ForeColor.RGB = &H0
End With
VertArray(1, 1) = 0
VertArray(1, 2) = H1 * (2 * i - 1)
VertArray(2, 1) = (i - 0.5) * DotsIn1
VertArray(2, 2) = 0
Set MyShape = Worksheets(1).Shapes.AddPolyline(VertArray)
With MyShape
.Line.Weight = 0.5
.Line.ForeColor.RGB = &H0
End With
Next
For i = 1 To 8
VertArray(1, 1) = 0
VertArray(1, 2) = H1 * i
VertArray(2, 1) = 12 * DotsIn1
VertArray(2, 2) = H1 * i
Set MyShape = Worksheets(1).Shapes.AddPolyline(VertArray)
With MyShape
.Line.Weight = 0.5
.Line.ForeColor.RGB = &H0
End With
Next
For i = 1 To N
NVersh(i) = 3
Do
j = 1
X(j, i) = DotsIn1 * (Rnd() * 0.6 + 0.7)
Y(j, i) = H1 * Rnd()
j = 2
AB = ScaleMin + ScaleKoef * Rnd()
Phi = (Rnd() - 0.5) * 2 * pi
X(j, i) = X(1, i) + AB * Cos(Phi)
Y(j, i) = Y(1, i) + AB * Sin(Phi)
j = 3
AB = ScaleMin + ScaleKoef * (Rnd() - 0.5)
Phi = Phi + pi * (0.5 + 0.4 * (Rnd() - 0.5)) * (2 * CInt(Rnd()) - 1)
X(j, i) = X(1, i) + AB * Cos(Phi)
Y(j, i) = Y(1, i) + AB * Sin(Phi)
Loop Until CheckTriZone(i)
ShColor(i) = Color_Random(i)
Call CutPlate(i, tg60, C1, 0.5 * DotsIn1, 1 * DotsIn1, 1)
Call CutPlate(i, -tg60, C2, 1 * DotsIn1, 1.5 * DotsIn1, 1)
Call CutPlate(i, 0, 0, 0.5 * DotsIn1, 1.5 * DotsIn1, -1)
For j = 1 To NVersh(i)
X(j, i + 2 * N) = X(j, i)
Y(j, i + 2 * N) = Y(j, i)
X(j, i + 3 * N) = X(j, i)
Y(j, i + 3 * N) = -Y(j, i) + 2 * H1
Call ReflectPoint(X(j, i), Y(j, i), X(j, i + 2 * N), Y(j, i + 2 * N), C1, 1)
X(j, i + 1 * N) = X(j, i)
Y(j, i + 1 * N) = -(Y(j, i) - H1) + H1
Call ReflectPoint(X(j, i + 4 * N), Y(j, i + 4 * N), X(j, i + 2 * N), Y(j, i + 2 * N), C2, -1)
X(j, i + 5 * N) = X(j, i + 4 * N)
Y(j, i + 5 * N) = -(Y(j, i + 4 * N) - H1) + H1
Next j
NVersh(i + N) = NVersh(i)
NVersh(i + 2 * N) = NVersh(i)
NVersh(i + 3 * N) = NVersh(i)
NVersh(i + 4 * N) = NVersh(i)
NVersh(i + 5 * N) = NVersh(i)
ShColor(i + N) = ShColor(i)
ShColor(i + 2 * N) = ShColor(i)
ShColor(i + 3 * N) = ShColor(i)
ShColor(i + 4 * N) = ShColor(i)
ShColor(i + 5 * N) = ShColor(i)
Next
For i = 1 To N
k = 0
AB = -1
For e = 6 To (ÑTri - 1) * 2 Step 6
k = k + 1
AB = -AB ' 1- é çíàê ñäâèãà ïî âûñîòå
For j = 1 To NVersh(i)
Phi = -1
For q = 0 To 5
Phi = -Phi ' 2- îé çíàê ñäâèãà ïî âûñîòå
X(j, i + (e + q) * N) = X(j, i + (e - 6 + q) * N) + 1.5 * DotsIn1
Y(j, i + (e + q) * N) = Y(j, i + (e - 6 + q) * N) + H1 * AB * Phi
Next q
Next j
Next e
For e = 6 To ÑTri * 2
NVersh(i + e * N) = NVersh(i)
ShColor(i + e * N) = ShColor(i)
Next e
Next i
For i = 1 To N
For k = 1 To RTri - 1
For q = 0 To ÑTri * 2 - 1
For j = 1 To NVersh(i)
X(j, i + k * ÑTri * 2 * N + q * N) = X(j, i + q * N)
Y(j, i + k * ÑTri * 2 * N + q * N) = Y(j, i + q * N) + H1 * 2 * k
NVersh(i + k * ÑTri * 2 * N + q * N) = NVersh(i)
ShColor(i + k * ÑTri * 2 * N + q * N) = ShColor(i)
Next j
Next q
Next k
Next i
For i = 1 To M
ReDim VertArray(1 To NVersh(i) + 1, 1 To 2)
For j = 1 To NVersh(i)
VertArray(j, 1) = X(j, i)
VertArray(j, 2) = Y(j, i)
Next j
VertArray(NVersh(i) + 1, 1) = X(1, i)
VertArray(NVersh(i) + 1, 2) = Y(1, i)
Set MyShape = Worksheets(1).Shapes.AddPolyline(VertArray)
With MyShape
.Fill.ForeColor.RGB = ShColor(i)
.Fill.Transparency = 0.5
.Line.Weight = 0
End With
Next
Application.ScreenUpdating = True
End Sub
Function CheckTriZone(i As Integer) As Boolean
Dim j As Integer
CheckTriZone = False
For j = 1 To 3
If Y(j, i) - (tg60 * X(j, i) + C1 - H1 * 0.2) < -Accuracy Then
If Y(j, i) - (-tg60 * X(j, i) + C2 - H1 * 0.2) < -Accuracy Then
CheckTriZone = True
Exit Function
End If
End If
Next j
End Function
Sub CutPlate(i As Integer, Koef, C, Xmin, Xmax, UpDown As Integer)
Dim j As Integer, k As Integer
Dim x1, x2, x3, x0
Dim y1, y2, y3, y0
j = 1
Do Until j > NVersh(i)
x2 = X(j, i): y2 = Y(j, i)
If (y2 - (Koef * x2 + C)) * UpDown > Accuracy Then
x0 = X(j, i): y0 = Y(j, i)
If j = 1 Then
x1 = X(NVersh(i), i)
y1 = Y(NVersh(i), i)
Else
x1 = X(j - 1, i)
y1 = Y(j - 1, i)
End If
If j = NVersh(i) Then
x3 = X(1, i)
y3 = Y(1, i)
Else
x3 = X(j + 1, i)
y3 = Y(j + 1, i)
End If
If (y3 - (Koef * x3 + C)) * UpDown < -Accuracy Then
Call CutLine(x2, y2, x3, y3, Koef, C)
If Abs(x3 - x2) > Accuracy Or Abs(y3 - y2) > Accuracy Then
X(j, i) = x2
Y(j, i) = y2
End If
If (y1 - (Koef * x1 + C)) * UpDown < -Accuracy Then
Call CutLine(x0, y0, x1, y1, Koef, C)
If Abs(x0 - x2) > Accuracy Or Abs(y0 - y2) > Accuracy Then
If x0 >= Xmin And x0 <= Xmax Then
For k = NVersh(i) To j Step -1
X(k + 1, i) = X(k, i)
Y(k + 1, i) = Y(k, i)
Next k
NVersh(i) = NVersh(i) + 1
End If
X(j, i) = x0
Y(j, i) = y0
End If
End If
ElseIf (y1 - (Koef * x1 + C)) * UpDown < -Accuracy Then
Call CutLine(x2, y2, x1, y1, Koef, C)
If Abs(x1 - x2) > Accuracy Or Abs(y1 - y2) > Accuracy Then
X(j, i) = x2
Y(j, i) = y2
End If
End If
End If
j = j + 1
Loop
End Sub
Sub CutLine(x1, y1, x2, y2, Koef, C)
Dim Koef2, C3
If Abs(x1 - x2) < Accuracy Then
y1 = Koef * x1 + C
Else
Koef2 = (y2 - y1) / (x2 - x1)
C3 = y1 - Koef2 * x1
If Koef <> Koef2 Then
x1 = (C3 - C) / (Koef - Koef2)
y1 = Koef * x1 + C
End If
End If
End Sub
Sub ReflectPoint(xNew, yNew, xOld, yOld, C, UpDown As Integer)
Dim Cos2_m_Sin2, DobleSinCos
Cos2_m_Sin2 = Cos(-UpDown * pi / 3) ^ 2 - Sin(-UpDown * pi / 3) ^ 2
DobleSinCos = 2 * Cos(-UpDown * pi / 3) * Sin(-UpDown * pi / 3)
xNew = xOld * Cos2_m_Sin2 + (C - yOld) * DobleSinCos
yNew = xOld * -DobleSinCos + (C - yOld) * Cos2_m_Sin2 + C
End Sub
Function Color_Random(i As Integer) As Long
Dim j
Select Case i
Case 1
Color_Random = RGB(255, Rnd() * 255, 0)
Case 2
Color_Random = RGB(Rnd() * 255, 255, 0)
Case 3
Color_Random = RGB(0, 255, Rnd() * 255)
Case 4
Color_Random = RGB(0, Rnd() * 255, 255)
Case 5
Color_Random = RGB(Rnd() * 255, 0, 255)
Case 6
Color_Random = RGB(255, 0, Rnd() * 255)
Case Else
j = Rnd() * 3
Select Case j
Case 0 To 1
Color_Random = RGB(200 + Rnd() * 55, Rnd() * 255, 0)
Case 1 To 2
Color_Random = RGB(0, 200 + Rnd() * 55, Rnd() * 255)
Case 2 To 3
Color_Random = RGB(Rnd() * 255, 0, 200 + Rnd() * 55)
End Select
End Select
End Function |