Debian Codegraph @

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

raw
1 % qap.mp
2 % L. Nobre G.
3 % IYP (2005)
4
5 def PickColor =
6 ( uniformdeviate(1) , uniformdeviate(1), uniformdeviate(1) )
7 enddef;
8
9 numeric HalfSideLength, ThinnessFactor;
10 numeric ThicknesFactor, BorderThFactor;
11
12 HalfSideLength = 10mm;
13 ThicknesFactor = 0.17; % Maximum TraceThickness over Half Side
14 ThinnessFactor = 0.12; % Minimum TraceThickness over Half Side
15 BorderThFactor = 1.35; % Borders over TraceThickness
16
17 def RandomStep =
18 begingroup
19 numeric margin;
20 margin = ThicknesFactor-ThinnessFactor;
21 ThinnessFactor*HalfSideLength + uniformdeviate( margin*HalfSideLength )
22 endgroup
23 enddef;
24
25 def PickInteger( expr MaxNum ) =
26 begingroup
27 save aux;
28 numeric aux;
29 aux = floor( uniformdeviate( MaxNum+0.995 ) );
30 ( aux )
31 endgroup
32 enddef;
33
34 vardef LocateIndex[] =
35 dotlabel.urt( str @, z@ )
36 enddef;
37
38 beginfig(1);
39 numeric QNum;
40 QNum = 5 + PickInteger( 5 );
41 numeric tracethickness, i, j, k, angadd, aa, ax, ay;
42 tracethickness = RandomStep;
43 pen tracepen, bordepen;
44 color TheColor;
45 tracepen = pencircle scaled tracethickness;
46 bordepen = tracepen scaled BorderThFactor;
47 path qpath;
48 aa = 360/QNum;
49 ax = HalfSideLength*2*cosd(0.5*aa);
50 ay = HalfSideLength*2*sind(0.5*aa);
51 z0 = (ax,0);
52 z1 = (0,ay);
53 z2 = (-ax,0);
54 z3 = (0,-ay);
55 z4 = z0;
56 qpath = z0--z1--z2--z3--cycle;
57 angadd = uniformdeviate( 20 );
58 TheColor = PickColor;
59 pair Dir[], pos[];
60 boolean Filled[];
61 for i = 0 upto 11:
62 j := floor( i/3 );
63 Dir[i] = dir(angle(z[j+1]-z[j])+80+angadd);
64 Filled[i] = false;
65 pos[i] = (0.25*(i-3*j+1))[z[j],z[j+1]];
66 endfor;
67 % for i=0 upto 5:
68 % LocateIndex[i];
69 % endfor;
70 numeric ThisInd, ThatInd;
71 path ActualPath;
72 for i=1 step 2 until 11:
73 forever:
74 ThisInd := PickInteger( 11 );
75 exitunless Filled[ThisInd];
76 endfor;
77 Filled[ThisInd] := true;
78 forever:
79 ThatInd := PickInteger( 11 );
80 exitunless Filled[ThatInd];
81 endfor;
82 Filled[ThatInd] := true;
83 ActualPath := pos[ThisInd]{Dir[ThisInd]}...pos[ThatInd]{-Dir[ThatInd]};
84 % draw ActualPath withcolor background withpen bordepen;
85 draw ActualPath withcolor TheColor withpen tracepen;
86 endfor;
87 % draw qpath;
88 % clip currentpicture to qpath;
89 pair urCorner, ulCorner, llCorner, lrCorner, cent;
90 path FinalCut;
91 urCorner = ( 8.13in, 11.533in );
92 llCorner = ( 0.118in, 35.5bp );
93 ulCorner = ( xpart llCorner, ypart urCorner );
94 lrCorner = ( xpart urCorner, ypart llCorner );
95 FinalCut = urCorner--ulCorner--llCorner--lrCorner--cycle;
96 cent = 0.25*(urCorner+ulCorner+llCorner+lrCorner);
97 picture Tile[];
98 for i=0 upto 1:
99 Tile[i] = currentpicture rotated (i*180);
100 endfor;
101 currentpicture := nullpicture;
102 pair pairh, pairv;
103 for k = 1 upto QNum:
104 pairh := HalfSideLength*2*dir((k-1)*aa);
105 pairv := HalfSideLength*2*dir(k*aa);
106 for i = 0 upto 11:
107 for j = 0 upto 11:
108 draw Tile[PickInteger( 1 )]
109 rotated ((k-0.5)*aa)
110 shifted ( cent + ax*dir((k-0.5)*aa) + i*pairh + j*pairv );
111 endfor;
112 endfor;
113 endfor;
114 clip currentpicture to FinalCut;
115 endfig;
116
117 end.