-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSelfAffine2DCircles.m
277 lines (246 loc) · 7.6 KB
/
SelfAffine2DCircles.m
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
% A simple script for self-affine sets with circles of distances
%
% Prerequisite: Image Processing Toolbox
%
% Zhou Feng @ 2020-10-12
clc, clf, clear
tic
%% settings
% IFS linear parts
linearMats = {[0.25 0; 0 0.25], ...
[0.25 0; 0 0.25], ...
[0.25 0; 0 0.25], ...
[0.25 0; 0 0.25], ...
[0.5 0; 0 0.5]};
% IFS translations
translations = {[0; 0], ...
[0.75; 0], ...
[0.75; 0.75], ...
[0; 0.75], ...
[0.25; 0.25]};
% initial polygon for iteration
shapeInit = [0 1 1 0;
0 0 1 1];
numItrs = 2; % iteration time
% plot settings
showTitle = false;
showFirstItrs = false;
numFirstItrs = 2;
% shapes
showShapes = true;
alphaFaces = 1;
colorFaces = 'none';
colorEdges = 'b';
fixAxisRatio = true;
% circles
colorCircle = 'k';
widthCircle = 0.5;
styleLine = '-';
% markers
showMarkers = true;
sizeMarker = 36;
colorMarkerFace = 'r';
colorMarkerEdge = 'r';
%% Examples
% ---------------------------------- gaskets --------------------------------- %
% % Sierpinski gasket
% cRatio = 1/2;
% linearMats = cell(1, 3);
% for i = 1:3
% linearMats{i} = cRatio * eye(2);
% end
% translations = {[0; 0], [cRatio; 0], [0; cRatio]};
% shapeInit = [0 1 0; 0 0 1];
% % Sierpinski gasket (self-affine)
% hRatio = 0.25;
% vRatio = 0.7;
% linearMats = {[hRatio 0; 0 vRatio],
% [1 - hRatio 1 - hRatio - vRatio; 0 vRatio],
% [1 - vRatio 0; 0 1 - vRatio]};
% translations = {[0; 0], [hRatio; 0], [0; vRatio]};
% shapeInit = [0 1 0; 0 0 1];
% ---------------------------------- carpets --------------------------------- %
% % Sierpinski carpet
% linearMats = cell(1, 8);
% for i = 1:8
% linearMats{i} = 1/3 * eye(2);
% end
% translations = {[0;0], [1;0], [2;0], [0;1], [2;1], [0;2], [1;2], [2;2]};
% shapeInit = [0 3 3 0; 0 0 3 3];
% % Bedford-McMullen carpet
% BMselect = [1 0;
% 0 1;
% 1 0]; % select positions
% [BMv, BMh] = size(BMselect);
% BMmat = flipud(BMselect);
% [oneRows, oneCols] = find(BMmat > 0);
% BMsize = length(oneRows);
% BMlinear = [1 / BMh 0; 0 1 / BMv];
% linearMats = cell(1, BMsize);
% translations = cell(1, BMsize);
% for i = 1:BMsize
% linearMats{i} = BMlinear;
% translations{i} = [(oneCols(i) - 1) * (1 / BMh); (oneRows(i) - 1) * (1 / BMv)];
% end
% shapeInit = [0 1 1 0; 0 0 1 1];
% % Baranski carpet (with possible overlaps)
% Bar_h = [0.1 0.3 0.4 0.2]; % horizontal scales
% Bar_v = [0.1 0.2 0.4 0.3]; % vertical scales
% Bar_select = [1 0 1 0;
% 0 1 0 1;
% 0 0 0 0;
% 0 0 1 0]; % select positions
% Bar_mat = flipud(Bar_select);
% [oneRows, oneCols] = find(Bar_mat > 0);
% Bar_size = length(oneRows);
% linearMats = cell(1, Bar_size);
% translations = cell(1, Bar_size);
% for i = 1:Bar_size
% linearMats{i} = [Bar_h(oneCols(i)) 0; 0 Bar_v(oneRows(i))];
% translations{i} = [sum(Bar_h(1:oneCols(i))) - Bar_h(oneCols(i));...
% sum(Bar_v(1:oneRows(i))) - Bar_v(oneRows(i))];
% end
% shapeInit = [0 1 1 0; 0 0 1 1];
% --------------------------------- triangles -------------------------------- %
% % Sierpinski triangle
% cRatio = 1/2;
% linearMats = cell(1, 3);
% for i = 1:3
% linearMats{i} = cRatio * eye(2);
% end
% translations = {[0; 0], [1-cRatio; 0], [0.5*(1-cRatio); (1-cRatio)*0.5*sqrt(3)]};
% shapeInit = [0 1 0.5; 0 0 0.5*sqrt(3)];
% % Sierpinski triangle (self-affine)
% ratio1st = 2/3;
% ratio2nd = 2/3;
% linearMats = {[ratio1st * (1 - ratio2nd) ratio1st * (2 * ratio2nd - 1) / sqrt(3);...
% 0 ratio1st * ratio2nd], ...
% [ratio1st * (1 - ratio2nd) 0; 0 ratio1st * (1 - ratio2nd)], ...
% [ratio1st * ratio2nd 0; 0 ratio1st * ratio2nd], ...
% [1 - ratio1st 0; 0 1 - ratio1st], ...
% [1/2 (1/2 - ratio1st) / sqrt(3); sqrt(3) / 2 - sqrt(3) * ratio1st 1/2]};
% translations = {[0; 0], [ratio1st * ratio2nd / 2; ratio1st * ratio2nd * sqrt(3) / 2],...
% [ratio1st * (1 - ratio2nd); 0], ...
% [ratio1st; 0], [ratio1st / 2; ratio1st * sqrt(3) / 2]};
% shapeInit = [0 1 0.5; 0 0 0.5 * sqrt(3)];
% ----------------------------------- dusts ---------------------------------- %
% % Cantor dust
% linearMats = cell(1, 4);
% for i = 1:4
% linearMats{i} = 0.25 * eye(2);
% end
% translations = {[0.26; 0], ...
% [0.75; 0.25], ...
% [0; 0.5], ...
% [0.5; 0.75]};
% shapeInit = [0 1 1 0; 0 0 1 1];
% % product Cantor set
% linearMats = cell(1, 4);
% for i = 1:4
% linearMats{i} = 1/3 * eye(2);
% end
% translations = {[0; 0], [2/3; 0], [0; 2/3], [2/3; 2/3]};
% shapeInit = [0 1 1 0; 0 0 1 1];
% ----------------------------------- misc ----------------------------------- %
% % Dim of self-affine sets is discontinuous
% ratioDimDiscts = 0.1;
% linearMats = {[0.5 0; 0 1/3], [0.5 0; 0 1/3]};
% translations = {[0; 2/3], [ratioDimDiscts; 0]};
% shapeInit = [0 1 1 0; 0 0 1 1];
% % attenna
% linearMats = {[0.25 0; 0 0.25], [0.25 0; 0 0.25], [0.25 0; 0 0.25],...
% [0.25 0; 0 0.25], [0.5 0; 0 0.5]};
% translations = {[0; 0], [0.75; 0], [0.75; 0.75], [0; 0.75], [0.25; 0.25]};
% shapeInit = [0 1 1 0; 0 0 1 1];
%% Error handling
isCompactible = false;
if length(linearMats) == length(translations)
isCompactible = true;
end
if ~isCompactible
error('Illegal settings. Dimensions of the parameters does not match!')
end
% generate params
spaceDim = 2;
numInitFaces = 1;
sizeIFS = length(linearMats);
numInitPts = size(shapeInit, 2);
shapeInitFaces = 1:numInitPts;
%% Generate points
ptsInit = shapeInit;
ptsNow = ptsInit;
sizeNow = numInitPts;
ptsTotal = cell(numItrs + 1, 1);
ptsTotal{1} = ptsInit;
for levelNow = 1:numItrs
ptsTmp = zeros(spaceDim, sizeNow * sizeIFS);
for indexFct = 1:sizeIFS
ptsTmp(:, (indexFct - 1) * sizeNow + 1:indexFct * sizeNow) = ...
linearMats{indexFct} * ptsNow + translations{indexFct};
end
ptsNow = ptsTmp;
sizeNow = size(ptsNow, 2);
ptsTotal{levelNow + 1} = ptsNow;
end
%% Plot
numShapes = sizeNow / numInitPts;
facesPlot = kron(ones(numShapes, 1), shapeInitFaces) + ...
kron((0:(numShapes - 1))' * numInitPts, ones(numInitFaces, 1));
figure(1)
% circles
radii = zeros(numShapes, 1);
for i = 1:numShapes
radii(i) = norm(ptsNow(:, 1+(i-1)*numInitPts));
end
viscircles(zeros(length(radii), 2), radii,...
'Color', colorCircle,...
'LineWidth', widthCircle,....
'LineStyle', styleLine);
hold on
% shapes
if showShapes
patch('Faces', facesPlot, ...
'Vertices', ptsNow', ...
'FaceColor', colorFaces, ...
'EdgeColor', colorEdges, ...
'FaceAlpha', alphaFaces)
end
% markers
if showMarkers
scatter(ptsNow(1,1:numInitPts:sizeNow), ptsNow(2,1:numInitPts:sizeNow),...
sizeMarker,...
'MarkerFaceColor', colorMarkerFace,...
'MarkerEdgeColor', colorMarkerEdge)
end
set(gca, 'XColor', 'none', 'YColor', 'none')
if fixAxisRatio
axis image
end
if showTitle
title(['Iteration time = ', num2str(numItrs)], 'Interpreter', 'latex');
end
hold off
if showFirstItrs && numItrs >= numFirstItrs
figure(2)
for i = 1:numFirstItrs
subplot(1, numFirstItrs, i)
sizeTmp = size(ptsTotal{i}, 2);
numShapesTmp = sizeTmp / numInitPts;
facesPlot = kron(ones(numShapesTmp, 1), shapeInitFaces) + ...
kron((0:(numShapesTmp - 1))' * numInitPts, ones(numInitFaces, 1));
patch('Faces', facesPlot, ...
'Vertices', ptsTotal{i}', ...
'FaceColor', colorFaces, ...
'EdgeColor', colorEdges, ...
'FaceAlpha', alphaFaces)
set(gca, 'XColor', 'none', 'YColor', 'none')
if fixAxisRatio
axis image
end
end
end
%% Show param
countPtsTotal = sizeNow;
countShapesTotal = numShapes;
tableResults = table(countPtsTotal, countShapesTotal);
disp(tableResults)