Debian Codegraph @

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

raw
1 % torus.mp
2 % L. Nobre G.
3 % 2014
4
5 input featpost3Dplus2D;
6
7 % Draw a torus with NP dots, a section of 3.14*RS^2
8 % and a total diameter of 2*(RB+RS). The dots are placed
9 % on a single closed line that turns around the $z$ axis
10 % NB times and around the section NS times.
11 % figure(1) exemplifies the use of signalvertex.
12
13 def flask( expr TheVal ) =
14 begingroup
15 numeric RB, RS, RP, NB, NS, NM, phi, theta, first, second, third;
16 RB=1;
17 RS=0.3;
18 RP=0.02;
19 NB=8;
20 NS=5;
21 NM=4;
22 theta=360*NB*TheVal;
23 phi=360*NS*TheVal;
24 first =(RB+(RS+RP*sind(NM*theta))*cosd(phi))*cosd(theta);
25 second=(RB+(RS+RP*sind(NM*theta))*cosd(phi))*sind(theta);
26 third = (RS+RP*sind(NM*theta))*sind(phi);
27 ( (first,second,third) )
28 endgroup
29 enddef;
30
31 def blask( expr TheVal ) =
32 begingroup
33 numeric RB, RS, NB, NS, phi, theta, first, second, third;
34 RB=1;
35 RS=0.1;
36 NB=1;
37 NS=24;
38 theta=360*NB*TheVal;
39 phi=360*NS*TheVal;
40 first =(RB+RS*cosd(phi))*cosd(theta);
41 second=(RB+RS*cosd(phi))*sind(theta);
42 third = RS*sind(phi);
43 ( (first,second,third) )
44 endgroup
45 enddef;
46
47 beginfig(5);
48 Spread := 200;
49 numeric NP, i, varfrac;
50 path cl;
51 string comm;
52 pen bip, sp;
53 varfrac = 1.25;
54 bip = pencircle scaled 15pt;
55 sp = pencircle scaled 9pt;
56 NP=600;
57 cl = for i=1 upto NP: rp(flask(i/NP)).. endfor cycle;
58 draw cl;
59 for i=1 upto NP:
60 comm:="draw subpath ("
61 & decimal(i-0.5)
62 & ","
63 & decimal(i+0.5)
64 & ") of cl withpen bip; undraw subpath ("
65 & decimal(i-varfrac)
66 & ","
67 & decimal(i+varfrac)
68 & ") of cl withpen sp;";
69 getready( comm, flask( i/NP ) );
70 endfor;
71 closedline( true, 300, 0.5, 1 )( blask );
72 doitnow;
73 endfig;
74
75 beginfig(4);
76 f := (16,2,3.8);
77 Spread:=40;
78 pickup pencircle scaled 3pt;
79 smoothtorus( black-3*blue, blue, 4, 1 );
80 smoothtorus( black, blue, 4, 1 );
81 smoothtorus( black+3*blue, blue, 4, 1 );
82 endfig;
83
84 beginfig(3);
85 Spread := 100;
86 f := (7,3,9);
87 HoriZon := -2;
88 LightSource := (1,-1,15);
89 numeric sinthe, costhe, ux, uz, lx, lz, co, i;
90 costhe = (4*sqrt(5)-sqrt(3))/11;
91 sinthe = sqrt(1-(costhe**2));
92 ux = costhe*sqrt(2)/2;
93 uz = sinthe/2;
94 lx = sqrt(4/3)*sinthe;
95 lz = -sqrt(2/3)*costhe;
96 co = sqrt(2)*(2*sqrt(5)+5*sqrt(3))/22;
97 V1 := (ux,0,-uz);
98 V2 := (lx,0,-lz);
99 V3 := (0,ux,uz); V15:= (0,-ux,uz);
100 V4 := (0,lx,lz); V16:= (0,-lx,lz);
101 V5 := (co,co,0); V14:= (-co,-co,0);
102 V6 := (-ux,0,-uz);
103 V7 := (-lx,0,-lz);
104 V8 := (-co,co,0); V13:= (co,-co,0);
105 V9 := improvertex( V5, 1, V4, 1, V3, sqrt(3)-1, (0,1,0) );
106 V10:= (-X(V9),-Y(V9),Z(V9));
107 V11:= (Y(V9),-X(V9),-Z(V9));
108 V12:= (-Y(V9),X(V9),-Z(V9));
109 makeface1(1,2,3);
110 makeface2(1,2,5);
111 makeface3(2,3,5);
112 makeface4(9,4,5);
113 makeface5(3,4,1);
114 makeface6(1,4,5);
115 makeface7(3,6,7);
116 makeface8(3,7,8);
117 makeface9(6,7,8);
118 makeface10(9,8,4);
119 makeface11(4,6,8);
120 makeface12(3,4,6);
121 makeface13(3,5,9);
122 makeface14(3,8,9);
123 makeface15(7,8,12);
124 makeface16(7,14,12);
125 makeface17(6,8,12);
126 makeface18(6,14,12);
127 makeface19(2,5,11);
128 makeface20(2,13,11);
129 makeface21(1,5,11);
130 makeface22(1,13,11);
131 makeface23(10,15,13);
132 makeface24(10,15,14);
133 makeface25(10,16,13);
134 makeface26(10,16,14);
135 makeface27(2,1,15);
136 makeface28(6,7,15);
137 makeface29(13,15,2);
138 makeface30(14,15,7);
139 makeface31(16,1,15);
140 makeface32(16,6,15);
141 for i=1 upto 32:
142 FC[i] := TableColors;
143 FCD[i]:= true;
144 endfor;
145 ShadowOn := true;
146 setthearena(15,6);
147 fill_faces();
148 draw_invisible( false, false, TableC3, TableC0 );
149 % drawoptions( withcolor red );
150 % for i=1 upto 16:
151 % label.bot( decimal( i ), rp( V[i] ) );
152 % endfor;
153 endfig;
154
155 % Draw a torus with faces.
156
157 beginfig(2);
158
159 PrintStep := 7;
160
161 f := ( 3,1,1 );
162
163 RB:=1;
164 RS:=0.25;
165 NB:=3;
166 NS:=5;
167 jB:=360/NB;
168 jS:=360/NS;
169 iS:=jS;
170 iB:=jB;
171 counter:=0;
172 for i=1 upto NB:
173 for j=1 upto NS:
174 counter := incr(counter);
175 npf[counter]:= 4;
176 phi :=j*jS;
177 theta:=i*jB;
178 f1 :=(RB+(RS)*cosd(phi))*cosd(theta);
179 s1 :=(RB+(RS)*cosd(phi))*sind(theta);
180 t1 := (RS)*sind(phi);
181 F[counter]p[1]:=(f1,s1,t1);
182 f2 :=(RB+(RS)*cosd(phi))*cosd(theta+iB);
183 s2 :=(RB+(RS)*cosd(phi))*sind(theta+iB);
184 t2 := (RS)*sind(phi);
185 F[counter]p[2]:=(f2,s2,t2);
186 f3 :=(RB+(RS)*cosd(phi+iS))*cosd(theta+iB);
187 s3 :=(RB+(RS)*cosd(phi+iS))*sind(theta+iB);
188 t3 := (RS)*sind(phi+iS);
189 F[counter]p[3]:=(f3,s3,t3);
190 f4 :=(RB+(RS)*cosd(phi+iS))*cosd(theta);
191 s4 :=(RB+(RS)*cosd(phi+iS))*sind(theta);
192 t4 := (RS)*sind(phi+iS);
193 F[counter]p[4]:=(f4,s4,t4);
194 endfor;
195 endfor;
196 NF:=counter;
197 % faceraytrace(0.3, blue);
198 % draw_all_test(false);
199 sharpraytrace;
200 endfig;
201
202 beginfig(1);
203 color tmpcolor;
204 string tmpstr;
205 Nobjects := 0;
206 f := ( 3,1,1 );
207 Spread := 110;
208 NP:=624;
209 RB:=1;
210 RS:=0.2;
211 R3:=0.1;
212 NB:=8;
213 NS:=5;
214 jB:=360*NB/NP;
215 jS:=360*NS/NP;
216 for i=1 upto NP:
217 phi :=i*jS;
218 theta :=i*jB;
219 first :=(RB+(RS+R3*sind(4*theta))*cosd(phi))*cosd(theta);
220 second:=(RB+(RS+R3*sind(4*theta))*cosd(phi))*sind(theta);
221 third := (RS+R3*sind(4*theta))*sind(phi);
222 tmpcolor := (first,second,third);
223 tmpstr := cstr( tmpcolor );
224 getready("signalvertex(" & tmpstr &
225 ",1,black);signalvertex(" & tmpstr & ",0.7,white);", tmpcolor );
226 endfor;
227 doitnow;
228 endfig;
229
230 beginfig(6);
231 f := (6,2,4);
232 % f := (6,2,7); % NO CUSP.
233 Spread:=70;
234 HoriZon := -2;
235 ShadowOn := true;
236 numeric bigray, smaray, angstep;
237 color toruscenter, torusmoment, nearaxe, sideaxe, viewline;
238 color circlecenter, circlemoment;
239 numeric ang, ind, i, anglim;
240 path cpath, apath, opath, ipath, wp, ep;
241 pair outerp[], innerp[], refpair;
242 pen markpen;
243 boolean cuspcond;
244 picture holepic;
245 markpen = pencircle scaled 5pt;
246 pickup markpen;
247 bigray = 2;
248 smaray = 1;
249 angstep= 2;
250 toruscenter = black;
251 torusmoment = (0,1,2);
252 refpair = unitvector( rp(toruscenter+torusmoment)-rp(toruscenter) );
253 viewline = f-toruscenter;
254 ind = 0;
255 sideaxe = bigray*ncrossprod( torusmoment, viewline );
256 nearaxe = bigray*ncrossprod( sideaxe, torusmoment );
257 anglim = 0.5*angstep-180;
258 if ShadowOn:
259 for ang=anglim step angstep until -anglim:
260 circlecenter:= nearaxe*cosd(ang)+sideaxe*sind(ang);
261 circlemoment:=-nearaxe*sind(ang)+sideaxe*cosd(ang);
262 filldraw circleshadowpath(circlecenter,circlemoment,smaray);
263 endfor;
264 fi;
265 for ang=anglim step angstep until -anglim:
266 ind := incr( ind );
267 circlecenter:= nearaxe*cosd(ang)+sideaxe*sind(ang);
268 circlemoment:=-nearaxe*sind(ang)+sideaxe*cosd(ang);
269 cpath:=spatialhalfcircle(circlecenter,circlemoment,smaray,true);
270 if ang>=0:
271 outerp[ind]=point 0 of cpath;
272 innerp[ind]=point (length cpath) of cpath;
273 elseif ang<0:
274 innerp[ind]=point 0 of cpath;
275 outerp[ind]=point (length cpath) of cpath;
276 fi;
277 endfor;
278 opath = for i=1 upto ind: outerp[i].. endfor cycle;
279 ipath = for i=1 upto ind: innerp[i].. endfor cycle;
280 holepic = currentpicture;
281 clip holepic to ipath;
282 unfill opath;
283 draw holepic;
284 draw opath withcolor green;
285 draw ipath withcolor red;
286 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
287 % There is an analytic way of getting the angle of the cusp point! %
288 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
289 i := ceiling(1+180/angstep);
290 forever:
291 i := incr( i );
292 cuspcond := refpair dotprod innerp[i+1] < refpair dotprod innerp[i];
293 exitif cuspcond or ( i > ind-1 );
294 endfor;
295 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
296 ep = outerp[ind-i]--innerp[ind-i];
297 wp = innerp[i]--outerp[i];
298 unfill buildcycle(opath,ep,reverse ipath,wp);
299 draw opath withcolor green;
300 draw subpath (i,ind-i) of ipath withcolor red;
301 endfig;
302
303 beginfig(7); %%%%%%% copy of figure 6
304 f := (6,2,4);
305 Spread:=70;
306 HoriZon := -2;
307 ShadowOn := true;
308 numeric bigray, smaray;
309 color toruscenter, torusmoment;
310 pen markpen;
311 markpen = pencircle scaled 5pt;
312 bigray = 2;
313 smaray = 1;
314 toruscenter = black;
315 torusmoment = (0,1,2);
316 setthestage( 17, 8 );
317 pickup markpen;
318 smoothtorus( toruscenter, torusmoment, bigray, smaray );
319 endfig;
320
321 end.
322
323