summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/doc/metapost/featpost/example/sap.mp
blob: 003abcd59cb2aabf73b78df4bce8eff116fa2d6f (plain)
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
% sap.mp
% L. Nobre G.
% 2002

numeric NumberOfColors, HalfSideLength;
numeric ThicknesFactor, TolerancFactor;
NumberOfColors = 7;
HalfSideLength = 15mm;
ThicknesFactor = 0.2;         % Maximum TraceThickness over Half Side
ThinnessFactor = 0.1;         % Minimum TraceThickness over Half Side
TolerancFactor = 0.5 + 0.02;  % Side Gaps over Thickness

def RandomStep =
  begingroup
    numeric margin;
    margin = ThicknesFactor-ThinnessFactor;
    ThinnessFactor*HalfSideLength + uniformdeviate( margin*HalfSideLength )
  endgroup
enddef;

def PickInteger( expr MaxNum ) =
  begingroup
    save aux;
    numeric aux;
    aux = ceiling( uniformdeviate( MaxNum ) );
    if aux = 0:
      aux := 1;
    fi;
    ( aux )
  endgroup
enddef; 

def PickColor =
  ( uniformdeviate(1) , uniformdeviate(1), uniformdeviate(1) )
enddef;

vardef LocateIndex[] =
  dotlabel.urt( str @, z@ )
enddef;

def SpreadAllOver =
  begingroup
    pair urCorner, ulCorner, llCorner, lrCorner;
    path FinalCut;
    urCorner = ( 8.13in, 11.533in );
    llCorner = ( 0.118in, 35.5bp );
    ulCorner = ( xpart llCorner, ypart urCorner );
    lrCorner = ( xpart urCorner, ypart llCorner );
    FinalCut = urCorner--ulCorner--llCorner--lrCorner--cycle;
    
    picture Tile[];
    Tile[1] = currentpicture;
    Tile[2] = currentpicture rotated 90;
    Tile[3] = currentpicture rotated 180;
    Tile[4] = currentpicture rotated 270;
    currentpicture := nullpicture;
    numeric i, j, horizo, vertic;
    horizo = ( xpart urCorner ) + HalfSideLength; 
    vertic = ( ypart urCorner ) + HalfSideLength; 
    for i = 0 step 2HalfSideLength until horizo:
      for j = 0 step 2HalfSideLength until vertic:
	draw Tile[PickInteger( 4 )] shifted ( i, j );
      endfor;
    endfor;
    clip currentpicture to FinalCut
  endgroup
enddef;

beginfig(1);
  linecap := butt;
  
  numeric tracethickness;
  tracethickness = RandomStep;
  currentpen := pensquare rotated 45 scaled tracethickness;
 
  numeric i, currentcoord, maincoords[], numaincoords, toleranc;
  i = 0;
  currentcoord = 2TolerancFactor*tracethickness;
  toleranc = HalfSideLength - currentcoord;
  forever:
    i := incr( i );
    maincoords[i] = currentcoord;
    currentcoord := currentcoord + 4TolerancFactor*tracethickness;
    currentcoord := currentcoord +
                    uniformdeviate( ThicknesFactor*HalfSideLength );
    exitif currentcoord > toleranc;
  endfor;
  numaincoords = i;

  numeric j;
  pair Dir[];
  for i = 1 upto numaincoords:
    j := i;
    z[j] = ( HalfSideLength, maincoords[i] );
    Dir[j] = left;
    j := i + numaincoords;
    z[j] = ( maincoords[i], HalfSideLength );
    Dir[j] = down;
    j := i + 2numaincoords;
    z[j] = ( -maincoords[i], HalfSideLength );
    Dir[j] = down;
    j := i + 3numaincoords;
    z[j] = ( -HalfSideLength, maincoords[i] );
    Dir[j] = right;
    j := i + 4numaincoords;
    z[j] = ( -HalfSideLength, -maincoords[i] );
    Dir[j] = right;
    j := i + 5numaincoords;
    z[j] = ( -maincoords[i], -HalfSideLength );
    Dir[j] = up;
    j := i + 6numaincoords;
    z[j] = ( maincoords[i], -HalfSideLength );
    Dir[j] = up;
    j := i + 7numaincoords;
    z[j] = ( HalfSideLength, -maincoords[i] );
    Dir[j] = left;
  endfor;

%  for i=1 upto 8numaincoords:
%    LocateIndex[i];
%  endfor;

  boolean Filled[];
  for i=1 upto 8numaincoords:
    Filled[i] = false;
  endfor;
  numeric Chosen[];
  for i=0 upto numaincoords-1:
    Chosen[i] = 0;
  endfor;
  color LookUpColor[];
  for i=1 upto NumberOfColors:
    LookUpColor[i] = PickColor;
  endfor;
  
  numeric ThisInd, ThatInd, ThisCoo, ThatCoo;
  color ThisColor, ThatColor;
  path ActualPath;
  for j=2 step 2 until 8numaincoords:
    
    forever:
      ThisInd := PickInteger( 8numaincoords );
      exitunless Filled[ThisInd];
    endfor;
    Filled[ThisInd] := true;
    ThisCoo := ThisInd mod numaincoords;
    if Chosen[ThisCoo] = 0:
      i := PickInteger( NumberOfColors );
      ThisColor := LookUpColor[i];
      Chosen[ThisCoo] := i;
    else:
      ThisColor := LookUpColor[Chosen[ThisCoo]];
    fi;
    
    forever:
      ThatInd := PickInteger( 8numaincoords );
      exitunless Filled[ThatInd];
    endfor;
    Filled[ThatInd] := true;
    ThatCoo := ThatInd mod numaincoords; 
    if Chosen[ThatCoo] = 0:
      i := PickInteger( NumberOfColors );
      ThatColor := LookUpColor[i];
      Chosen[ThatCoo] := i;
    else:
      ThatColor := LookUpColor[Chosen[ThatCoo]];
    fi;

    ActualPath := z[ThisInd]{Dir[ThisInd]}
               ...z[ThatInd]{-Dir[ThatInd]};
    draw subpath (0,0.6) of ActualPath withcolor ThisColor;
    draw subpath (0.4,1) of ActualPath withcolor ThatColor;
    
  endfor;
  
  SpreadAllOver;
endfig;

end.