Debian Codegraph @

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

raw
1 % hap.mp
2 % L. Nobre G.
3 % IYP (2005)
4
5 numeric HalfSideLength;
6 numeric ThicknesFactor, BorderThFactor;
7
8 HalfSideLength = 6mm;
9 ThicknesFactor = 0.5;
10 BorderThFactor = 1.85; % Borders over TraceThickness
11
12 def PickInteger( expr MaxNum ) = floor( uniformdeviate( MaxNum+1 ) ) enddef;
13
14 def PickColor =
15 begingroup
16 numeric c[], i, min, max, ref;
17 ref = 0.76;
18 for i=1 upto 3:
19 c[i] = uniformdeviate(1);
20 endfor;
21 min = c1;
22 max = c1;
23 for i=2 upto 3:
24 if c[i] < min: min := c[i]; fi;
25 if c[i] > max: max := c[i]; fi;
26 endfor;
27 for i=1 upto 3:
28 c[i] := ref+(0.94-ref)*(c[i]-min)/(max-min);
29 endfor;
30 ( ( c1 , c2, c3 ) )
31 endgroup
32 enddef;
33
34 vardef LocateIndex[] =
35 dotlabel.urt( str @, z@ )
36 enddef;
37
38 beginfig(1);
39 numeric tracethickness, i, j, hexconst, angaux, cou, nupersi;
40 numeric ThisInd, ThatInd, dist[], ang[];
41 color TheColor;
42 pen tracepen, bordepen;
43 path hexpath, ActualPath, FinalCut;
44 pair corn[], nco, aco, vec, Dir[], urCorner, ulCorner, llCorner, lrCorner;
45 boolean Filled[];
46 picture Tile[];
47 hexconst = sqrt(3)/2;
48 nupersi = PickInteger( 2 );
49 tracethickness = ThicknesFactor*
50 (0.5+uniformdeviate(0.5))*
51 HalfSideLength/(nupersi+1);
52 TheColor = PickColor;
53 tracepen = pencircle scaled tracethickness;
54 bordepen = tracepen scaled BorderThFactor;
55 for i=0 upto 6:
56 corn[i] = 2*HalfSideLength*dir(i*60);
57 endfor;
58 hexpath = corn0--corn1--corn2--corn3--corn4--corn5--cycle;
59 dist0 = 0;
60 ang0 = 40+uniformdeviate( 100 );
61 for i=1 upto nupersi:
62 dist[i] = dist[i-1]+(0.7+uniformdeviate(0.45))*HalfSideLength/(nupersi+1);
63 angaux := 40+20*i/nupersi;
64 ang[i] = angaux + uniformdeviate( 180-2*angaux );
65 endfor;
66 for i=0 upto 5:
67 aco := corn[i];
68 nco := corn[1+i];
69 vec := nco-aco;
70 z[i] = aco+0.5*vec;
71 Dir[i] = dir(angle(vec)+ang0);
72 Filled[i] = false;
73 endfor;
74 cou = 5;
75 for i=0 upto 5:
76 aco := corn[i];
77 nco := corn[i+1];
78 vec := unitvector(nco-aco);
79 for j=1 upto nupersi:
80 cou := incr( cou );
81 z[cou] = 0.5[aco,nco]+dist[j]*vec;
82 Dir[cou] = dir(angle(vec)+ang[j]);
83 Filled[cou] = false;
84 cou := incr( cou );
85 z[cou] = 0.5[aco,nco]-dist[j]*vec;
86 Dir[cou] = dir(angle(vec)+ang[j]);
87 Filled[cou] = false;
88 endfor;
89 endfor;
90 % for i=0 upto cou:
91 % LocateIndex[i];
92 % endfor;
93 for i=1 step 2 until cou:
94 forever:
95 ThisInd := PickInteger( cou );
96 exitunless Filled[ThisInd];
97 endfor;
98 Filled[ThisInd] := true;
99 forever:
100 ThatInd := PickInteger( cou );
101 exitunless Filled[ThatInd];
102 endfor;
103 Filled[ThatInd] := true;
104 ActualPath := z[ThisInd]{Dir[ThisInd]}
105 ...z[ThatInd]{-Dir[ThatInd]};
106 draw ActualPath withcolor background withpen bordepen;
107 draw ActualPath withcolor TheColor withpen tracepen;
108 endfor;
109 clip currentpicture to hexpath;
110 urCorner = ( 8.13in, 11.533in );
111 llCorner = ( 0.118in, 35.5bp );
112 ulCorner = ( xpart llCorner, ypart urCorner );
113 lrCorner = ( xpart urCorner, ypart llCorner );
114 FinalCut = urCorner--ulCorner--llCorner--lrCorner--cycle;
115 for i=0 upto 5:
116 Tile[i] = currentpicture rotated (i*60);
117 endfor;
118 currentpicture := nullpicture;
119 numeric j, horizo, vertic, vertigo;
120 horizo = ( xpart urCorner ) + 2HalfSideLength;
121 vertic = ( ypart urCorner ) + 2HalfSideLength;
122 vertigo = 0.5*horizo;
123 pair paird, pairv;
124 pairv = up*4*HalfSideLength*hexconst;
125 paird = dir(30)*4*HalfSideLength*hexconst;
126 numeric numd, numv;
127 numv = (vertic+2*vertigo)/(4*HalfSideLength*hexconst);
128 numd = horizo/(3*HalfSideLength);
129 for i = 0 upto numd:
130 for j = 0 upto numv:
131 draw Tile[PickInteger( 5 )] shifted ( (0,-vertigo) + i*paird + j*pairv );
132 endfor;
133 endfor;
134 clip currentpicture to FinalCut;
135 endfig;
136
137 end.