Debian Codegraph @

texmf-dist/doc/metapost/featpost/example/sap.mp

raw
1 % sap.mp
2 % L. Nobre G.
3 % 2002
4
5 numeric NumberOfColors, HalfSideLength;
6 numeric ThicknesFactor, TolerancFactor;
7 NumberOfColors = 7;
8 HalfSideLength = 15mm;
9 ThicknesFactor = 0.2; % Maximum TraceThickness over Half Side
10 ThinnessFactor = 0.1; % Minimum TraceThickness over Half Side
11 TolerancFactor = 0.5 + 0.02; % Side Gaps over Thickness
12
13 def RandomStep =
14 begingroup
15 numeric margin;
16 margin = ThicknesFactor-ThinnessFactor;
17 ThinnessFactor*HalfSideLength + uniformdeviate( margin*HalfSideLength )
18 endgroup
19 enddef;
20
21 def PickInteger( expr MaxNum ) =
22 begingroup
23 save aux;
24 numeric aux;
25 aux = ceiling( uniformdeviate( MaxNum ) );
26 if aux = 0:
27 aux := 1;
28 fi;
29 ( aux )
30 endgroup
31 enddef;
32
33 def PickColor =
34 ( uniformdeviate(1) , uniformdeviate(1), uniformdeviate(1) )
35 enddef;
36
37 vardef LocateIndex[] =
38 dotlabel.urt( str @, z@ )
39 enddef;
40
41 def SpreadAllOver =
42 begingroup
43 pair urCorner, ulCorner, llCorner, lrCorner;
44 path FinalCut;
45 urCorner = ( 8.13in, 11.533in );
46 llCorner = ( 0.118in, 35.5bp );
47 ulCorner = ( xpart llCorner, ypart urCorner );
48 lrCorner = ( xpart urCorner, ypart llCorner );
49 FinalCut = urCorner--ulCorner--llCorner--lrCorner--cycle;
50
51 picture Tile[];
52 Tile[1] = currentpicture;
53 Tile[2] = currentpicture rotated 90;
54 Tile[3] = currentpicture rotated 180;
55 Tile[4] = currentpicture rotated 270;
56 currentpicture := nullpicture;
57 numeric i, j, horizo, vertic;
58 horizo = ( xpart urCorner ) + HalfSideLength;
59 vertic = ( ypart urCorner ) + HalfSideLength;
60 for i = 0 step 2HalfSideLength until horizo:
61 for j = 0 step 2HalfSideLength until vertic:
62 draw Tile[PickInteger( 4 )] shifted ( i, j );
63 endfor;
64 endfor;
65 clip currentpicture to FinalCut
66 endgroup
67 enddef;
68
69 beginfig(1);
70 linecap := butt;
71
72 numeric tracethickness;
73 tracethickness = RandomStep;
74 currentpen := pensquare rotated 45 scaled tracethickness;
75
76 numeric i, currentcoord, maincoords[], numaincoords, toleranc;
77 i = 0;
78 currentcoord = 2TolerancFactor*tracethickness;
79 toleranc = HalfSideLength - currentcoord;
80 forever:
81 i := incr( i );
82 maincoords[i] = currentcoord;
83 currentcoord := currentcoord + 4TolerancFactor*tracethickness;
84 currentcoord := currentcoord +
85 uniformdeviate( ThicknesFactor*HalfSideLength );
86 exitif currentcoord > toleranc;
87 endfor;
88 numaincoords = i;
89
90 numeric j;
91 pair Dir[];
92 for i = 1 upto numaincoords:
93 j := i;
94 z[j] = ( HalfSideLength, maincoords[i] );
95 Dir[j] = left;
96 j := i + numaincoords;
97 z[j] = ( maincoords[i], HalfSideLength );
98 Dir[j] = down;
99 j := i + 2numaincoords;
100 z[j] = ( -maincoords[i], HalfSideLength );
101 Dir[j] = down;
102 j := i + 3numaincoords;
103 z[j] = ( -HalfSideLength, maincoords[i] );
104 Dir[j] = right;
105 j := i + 4numaincoords;
106 z[j] = ( -HalfSideLength, -maincoords[i] );
107 Dir[j] = right;
108 j := i + 5numaincoords;
109 z[j] = ( -maincoords[i], -HalfSideLength );
110 Dir[j] = up;
111 j := i + 6numaincoords;
112 z[j] = ( maincoords[i], -HalfSideLength );
113 Dir[j] = up;
114 j := i + 7numaincoords;
115 z[j] = ( HalfSideLength, -maincoords[i] );
116 Dir[j] = left;
117 endfor;
118
119 % for i=1 upto 8numaincoords:
120 % LocateIndex[i];
121 % endfor;
122
123 boolean Filled[];
124 for i=1 upto 8numaincoords:
125 Filled[i] = false;
126 endfor;
127 numeric Chosen[];
128 for i=0 upto numaincoords-1:
129 Chosen[i] = 0;
130 endfor;
131 color LookUpColor[];
132 for i=1 upto NumberOfColors:
133 LookUpColor[i] = PickColor;
134 endfor;
135
136 numeric ThisInd, ThatInd, ThisCoo, ThatCoo;
137 color ThisColor, ThatColor;
138 path ActualPath;
139 for j=2 step 2 until 8numaincoords:
140
141 forever:
142 ThisInd := PickInteger( 8numaincoords );
143 exitunless Filled[ThisInd];
144 endfor;
145 Filled[ThisInd] := true;
146 ThisCoo := ThisInd mod numaincoords;
147 if Chosen[ThisCoo] = 0:
148 i := PickInteger( NumberOfColors );
149 ThisColor := LookUpColor[i];
150 Chosen[ThisCoo] := i;
151 else:
152 ThisColor := LookUpColor[Chosen[ThisCoo]];
153 fi;
154
155 forever:
156 ThatInd := PickInteger( 8numaincoords );
157 exitunless Filled[ThatInd];
158 endfor;
159 Filled[ThatInd] := true;
160 ThatCoo := ThatInd mod numaincoords;
161 if Chosen[ThatCoo] = 0:
162 i := PickInteger( NumberOfColors );
163 ThatColor := LookUpColor[i];
164 Chosen[ThatCoo] := i;
165 else:
166 ThatColor := LookUpColor[Chosen[ThatCoo]];
167 fi;
168
169 ActualPath := z[ThisInd]{Dir[ThisInd]}
170 ...z[ThatInd]{-Dir[ThatInd]};
171 draw subpath (0,0.6) of ActualPath withcolor ThisColor;
172 draw subpath (0.4,1) of ActualPath withcolor ThatColor;
173
174 endfor;
175
176 SpreadAllOver;
177 endfig;
178
179 end.