Debian Codegraph @

texmf-dist/doc/metapost/fiziko/fiziko.tex

raw
1 \documentclass{ltxdoc}
2
3 \usepackage{luamplib, listings, bxtexlogo, ccicons}
4 \everymplib{verbatimtex \leavevmode etex; input fiziko.mp; beginfig(1);}
5 \everyendmplib{endfig;}
6
7 \lstset{
8 language=MetaPost,
9 numbers=left,
10 numberstyle=\tiny,
11 basicstyle=\scriptsize
12 }
13
14 \author{Sergey Slyusarev}
15 \title{``fiziko'' v. 0.2.1 package for \METAPOST}
16
17 \begin{document}
18 \maketitle
19
20 \begin{abstract}
21 This document describes a bunch of macros provided by ``fiziko'' library for \METAPOST.
22 \end{abstract}
23
24 \begin{centering}
25
26 This document is distributed under CC-BY-SA 4.0 license
27
28 \ccbysa
29
30 https://github.com/jemmybutton/fiziko
31
32 \end{centering}
33
34 \section{Introduction}
35 This \METAPOST\ library was initially written to automate some elements of black and white illustrations for a physics textbook. First and foremost it provides functions to draw things like lines of variable width, shaded spheres and tubes of different kinds, which can be used to produce images of a variety of objects. The library also contains functions to draw some objects constructed from these primitives.
36
37 \section{Usage}
38 Simply include this in the beginning of your \METAPOST\ document:
39
40 \begin{lstlisting}
41 input fiziko.mp
42 \end{lstlisting}
43
44 \section{Global variables}
45 A few global variables control different aspects of the behavior of the provided macros. Not all possible values are meaningful and some will definitely result in ugly pictures or errors.
46
47 \subsection{minStrokeWidth}
48 This variable controls minimal thickness of lines that are used for shading. Below this value lines are not getting thinner, but become dashed instead, maintaining roughly the same amount of ink per unit length as a thinner line would take. Default value is one fifth of a point. There are several things that depend on this value, so it's convenient to change it using a macro:
49
50 \begin{lstlisting}
51 defineMinStrokeWidth(1/2pt);
52 \end{lstlisting}
53
54 \subsection{lightDirection}
55 This variable controls direction from which light falls on shaded objects. It's of \texttt{pair} type and is set in radians. Default direction is top-left:
56 \begin{lstlisting}
57 lightDirection := (-1/8pi, 1/8pi);
58 \end{lstlisting}
59
60
61 \subsection{invertedLight}
62 This variable determines whether the light is inverted. This comes in handy when you need to have your shaded objects white on black. It's of \texttt{boolean} type. Default value is false:
63
64 \begin{lstlisting}
65 draw sphere.s(1cm);
66 invertedLight := true;
67 fill ((unitsquare shifted (-1/2,-1/2)) scaled 3/2cm) shifted (3/2cm, 0) withcolor black;
68 draw sphere.s(1cm) shifted (3/2cm, 0) withcolor white;
69 \end{lstlisting}
70
71 \begin{mplibcode}
72 draw sphere.c(1cm);
73 invertedLight := true;
74 fill ((unitsquare shifted (-1/2,-1/2)) scaled 3/2cm) shifted (3/2cm, 0) withcolor black;
75 draw sphere.c(1cm) shifted (3/2cm, 0) withcolor white;
76 invertedLight := false;
77 \end{mplibcode}
78
79 \section{``Lower level'' macros}
80 Currently, algorithms are quite stupid and will produce decent results only in certain simple circumstances.
81
82 \subsection{offsetPath (\emph{path})(\emph{offset function})}
83 This macro returns offset path (of type \texttt{path}) to a current path with a distance from the original path controlled by some arbitrary function; typically, it is a function of path length, set as either \texttt{offsetPathTime} or \texttt{offsetPathLength}. Former is simply \texttt{time} on current path and changes from 0 to \texttt{length(path)}, and latter changes from 0 to 1 over the path \texttt{arctime} (as a function of \texttt{arclength}).
84
85 \begin{lstlisting}
86 path p, q;
87 p := (0,0){dir(30)}..(5cm, 0)..{dir(30)}(10cm, 0);
88 q := offsetPath(p)(1cm*sin(offsetPathLength*pi));
89 draw p;
90 draw q dashed evenly;
91 \end{lstlisting}
92
93 \begin{mplibcode}
94 path p, q;
95 p := (0,0){dir(30)}..(5cm, 0)..{dir(30)}(10cm, 0);
96 q := offsetPath(p)(1cm*sin(offsetPathLength*pi));
97 draw p;
98 draw q dashed evenly;
99 \end{mplibcode}
100
101 \subsection{brush (\emph{path})(\emph{offset function})}
102 This macro returns a \texttt{picture} of a line of variable width along given path, which is controlled by some arbitrary function, analogous to \texttt{offsetPath}. If line is getting thinner than \texttt{minStrokeWith}, it is drawn dashed.
103
104 \begin{lstlisting}
105 path p;
106 p := (0,0){dir(30)}..(5cm, 0)..{dir(30)}(10cm, 0);
107 draw brush (p)(2minStrokeWidth*sin(offsetPathLength*pi));
108 \end{lstlisting}
109
110 \begin{mplibcode}
111 path p;
112 p := (0,0){dir(30)}..(5cm, 0)..{dir(30)}(10cm, 0);
113 draw brush (p)(2minStrokeWidth*sin(offsetPathLength*pi));
114 \end{mplibcode}
115
116 \subsection{sphere.c (\emph{diameter})}
117 This macro returns a \texttt{picture} of a sphere with specified diameter shaded with concentric strokes. Strokes are arranged to fit those of \texttt{tube.l}.
118
119 \begin{lstlisting}
120 for i := 1 step 1 until 6:
121 draw sphere.c(i*1/4cm) shifted (1/2cm*(i*(i+1))/2, 0);
122 endfor;
123 \end{lstlisting}
124
125 \begin{mplibcode}
126 for i := 1 step 1 until 6:
127 draw sphere.c(i*1/4cm) shifted (1/2cm*(i*(i+1))/2, 0);
128 endfor;
129 \end{mplibcode}
130
131 \subsection{sphere.s (\emph{diameter})}
132 This macro returns a \texttt{picture} of a sphere with specified diameter shaded with stipples.
133
134 \begin{lstlisting}
135 for i := 1 step 1 until 6:
136 draw sphere.s(i*1/4cm) shifted (1/2cm*(i*(i+1))/2, 0);
137 endfor;
138 \end{lstlisting}
139
140 \begin{mplibcode}
141 for i := 1 step 1 until 6:
142 draw sphere.s(i*1/4cm) shifted (1/2cm*(i*(i+1))/2, 0);
143 endfor;
144 \end{mplibcode}
145
146 \subsection{sphereLat (\emph{diameter, angle})}
147 This macro returns a \texttt{picture} of a shaded sphere with specified diameter. Unlike \texttt{sphere.c} macro, this one draws latitudinal strokes around axis rotated at specified \texttt{angle}.
148
149 \begin{lstlisting}
150 for i := 1 step 1 until 6:
151 draw sphereLat(i*1/4cm, -90 + i*30)
152 shifted (1/2cm*(i*(i+1))/2, 0);
153 endfor;
154 \end{lstlisting}
155
156 \begin{mplibcode}
157 for i := 1 step 1 until 6:
158 draw sphereLat(i*1/4cm, -90 + i*30)
159 shifted (1/2cm*(i*(i+1))/2, 0);
160 endfor;
161 \end{mplibcode}
162
163 \subsection{tube.l (\emph{path})(\emph{offset function})}
164 This macro returns a \texttt{picture} of a shaded ``tube'' of a variable width along a given path, which is controlled by some arbitrary function, analogous to \texttt{offsetPath}. ``Tube'' drawn by this macro is shaded be longitudal strokes. Once tube is generated, you can call \texttt{tubeOutline} path global variable, if you need one.
165
166 \begin{lstlisting}
167 path p;
168 p := (0,0){dir(30)}..(5cm, 0)..{dir(30)}(10cm, 0);
169 draw tube.l (p)(1/2cm*sin(offsetPathLength*pi));
170 \end{lstlisting}
171
172 \begin{mplibcode}
173 path p;
174 p := (0,0){dir(30)}..(5cm, 0)..{dir(30)}(10cm, 0);
175 draw tube.l (p)(1/2cm*sin(offsetPathLength*pi));
176 \end{mplibcode}
177
178 \subsection{tube.t (\emph{path})(\emph{offset function})}
179 This macro returns a \texttt{picture} of a shaded ``tube'' of variable width along given path, which is controlled by some arbitrary function, analogous to \texttt{offsetPath}. ``Tube'' drawn by this macro is shaded be transverse strokes. Once tube is generated, you can call \texttt{tubeOutline} path global variable, if you need one.
180
181 \begin{lstlisting}
182 path p;
183 p := (0,0){dir(30)}..(5cm, 0)..{dir(30)}(10cm, 0);
184 draw tube.t (p)(1/2cm*sin(offsetPathLength*pi));
185 \end{lstlisting}
186
187 \begin{mplibcode}
188 path p;
189 p := (0,0){dir(30)}..(5cm, 0)..{dir(30)}(10cm, 0);
190 draw tube.t (p)(1/2cm*sin(offsetPathLength*pi));
191 \end{mplibcode}
192
193 \subsection{tube.s (\emph{path})(\emph{offset function})}
194 This macro returns a \texttt{picture} of a shaded ``tube'' of variable width along given path, which is controlled by some arbitrary function, analogous to \texttt{offsetPath}. ``Tube'' drawn by this macro is shaded with stipples. Once tube is generated, you can call \texttt{tubeOutline} path global variable, if you need one.
195
196 \begin{lstlisting}
197 path p;
198 p := (0,0){dir(30)}..(5cm, 0)..{dir(30)}(10cm, 0);
199 draw tube.s (p)(1/2cm*sin(offsetPathLength*pi));
200 \end{lstlisting}
201
202 \begin{mplibcode}
203 path p;
204 p := (0,0){dir(30)}..(5cm, 0)..{dir(30)}(10cm, 0);
205 draw tube.s (p)(1/2cm*sin(offsetPathLength*pi));
206 \end{mplibcode}
207
208 \subsection{tube.e (\emph{path})(\emph{offset function})}
209 This macro returns the outline of a tube as a path.
210
211 \section{``Higher level'' macros}
212 Using macros described in the previous section it is possible to construct more complex images. Macros for drawing some often used images are present in this package.
213
214 \subsection{eye (\emph{angle})}
215 This macro returns a \texttt{picture} of an eye pointed at the direction \texttt{angle} (in degrees). Eye size is controlled by a global variable \texttt{eyescale}, which has default value of \texttt{eyescale := 1/2cm;}.
216
217 \begin{lstlisting}
218 save eyescale;
219 for i := 1 step 1 until 6:
220 eyescale := 1/6cm*i;
221 draw eye(i*60) shifted (1/2cm*(i*(i+1))/2, 0);
222 endfor;
223 \end{lstlisting}
224
225 \begin{mplibcode}
226 save eyescale;
227 for i := 1 step 1 until 6:
228 eyescale := 1/6cm*i;
229 draw eye(i*60) shifted (1/2cm*(i*(i+1))/2, 0);
230 endfor;
231 \end{mplibcode}
232
233 \subsection{pulley (\emph{diameter, angle})}
234 This macro returns a \texttt{picture} of a pulley with specified \texttt{diameter} and its support pointed at the direction \texttt{angle} (in degrees). Note that pulley's support protrudes from its center by \texttt{pulleySupportSize*diameter} and by default \texttt{pulleySupportSize} = 3/2. Once pulley is generated, you can call \texttt{pulleyOutline} path global variable, if you need one.
235
236 \begin{lstlisting}
237 draw (-1/8cm, 0)--(12cm, 0);
238 for i := 1 step 1 until 6:
239 r := 1/7cm*i;
240 draw image(
241 draw pulley(2r, 0) shifted (0, -4/3r);
242 draw (r, -4/3r) -- (r, -2cm);
243 drawarrow (-r, -4/3r) -- (-r, -2cm);
244 ) shifted (1/2cm*(i*(i+1))/2, 0);
245 endfor;
246 \end{lstlisting}
247
248 \noindent\begin{mplibcode}
249 draw (-1/8cm, 0)--(12cm, 0);
250 for i := 1 step 1 until 6:
251 r := 1/7cm*i;
252 draw image(
253 draw pulley(2r, 0) shifted (0, -4/3r);
254 draw (r, -4/3r) -- (r, -2cm);
255 drawarrow (-r, -4/3r) -- (-r, -2cm);
256 ) shifted (1/2cm*(i*(i+1))/2, 0);
257 endfor;
258 \end{mplibcode}
259
260 \subsection{pulleyWheel (\emph{diameter})}
261 This macro returns a \texttt{picture} of a pulley wheel with specified \texttt{diameter}.
262
263 \begin{lstlisting}
264 for i := 1 step 1 until 6:
265 r := 1/7cm*i;
266 draw image(
267 draw pulleyWheel(2r);
268 draw (r, 0) -- (r, 1cm);
269 drawarrow (-r, 0) -- (-r, 1cm);
270 ) shifted (1/2cm*(i*(i+1))/2, 0);
271 endfor;
272 \end{lstlisting}
273
274 \begin{mplibcode}
275 for i := 1 step 1 until 6:
276 r := 1/7cm*i;
277 draw image(
278 draw pulleyWheel(2r);
279 draw (r, 0) -- (r, 1cm);
280 drawarrow (-r, 0) -- (-r, 1cm);
281 ) shifted (1/2cm*(i*(i+1))/2, 0);
282 endfor;
283 \end{mplibcode}
284
285 \subsection{wheel (\emph{diameter, angle})}
286 This macro returns a \texttt{picture} of a wheel with specified \texttt{diameter} and its support pointed at the direction \texttt{angle} (in degrees).
287
288 \begin{lstlisting}
289 draw (-1/8cm, 0)--(12cm, 0);
290 for i := 1 step 1 until 6:
291 r := 1/7cm*i;
292 draw wheel(2r, 0) shifted (0, -r) shifted (1/2cm*(i*(i+1))/2, 0);
293 endfor;
294 \end{lstlisting}
295
296
297 \noindent\begin{mplibcode}
298 draw (-1/8cm, 0)--(12cm, 0);
299 for i := 1 step 1 until 6:
300 r := 1/7cm*i;
301 draw wheel(2r, 0) shifted (0, -r) shifted (1/2cm*(i*(i+1))/2, 0);
302 endfor;
303 \end{mplibcode}
304
305
306 \subsection{weight.s (\emph{height})}
307 This macro returns a \texttt{picture} of a weight of a specific \texttt{height} that is standing on the point \texttt{(0, 0)}.
308
309 \begin{lstlisting}
310 for i := 1 step 1 until 6:
311 draw weight.s(1/4cm + i*1/4cm) shifted (1/2cm*(i*(i+1))/2, 0);
312 endfor;
313 draw (-1/8cm, 0)--(12cm, 0);
314 \end{lstlisting}
315
316 \noindent\begin{mplibcode}
317 for i := 1 step 1 until 6:
318 draw weight.s(1/4cm + i*1/4cm) shifted (1/2cm*(i*(i+1))/2, 0);
319 endfor;
320 draw (-1/8cm, 0)--(12cm, 0);
321 \end{mplibcode}
322
323 \subsection{weight.h (\emph{height})}
324 This macro returns a \texttt{picture} of a weight of a specific \texttt{height} that is is hanging from the point \texttt{(0, 0)}.
325
326 \begin{lstlisting}
327 for i := 1 step 1 until 6:
328 draw weight.h(1/4cm + i*1/4cm) shifted (1/2cm*(i*(i+1))/2, 0);
329 endfor;
330 draw (12cm, 0)--(-1/8cm, 0);
331 \end{lstlisting}
332
333 \noindent\begin{mplibcode}
334 for i := 1 step 1 until 6:
335 draw weight.h(1/4cm + i*1/4cm) shifted (1/2cm*(i*(i+1))/2, 0);
336 endfor;
337 draw (12cm, 0)--(-1/8cm, 0);
338 \end{mplibcode}
339
340
341 \subsection{spring (\emph{point a, point b, number of steps})}
342 This macro returns a \texttt{picture} of a spring stretched between points \texttt{a} and \texttt{b} (of type \texttt{pair}), with specified \texttt{number of steps}. Spring width is controlled by global variable \texttt{springwidth} with the default value \texttt{springwidth := 1/8cm;}.
343
344 \begin{lstlisting}
345 pair a, b;
346 a := (0, 0);
347 for i := 1 step 1 until 6:
348 springwidth := 1/16cm + i*1/48cm;
349 b := (i*1/3cm, - i*1/5cm);
350 draw spring (a, b, 10) shifted (2/5cm*(i*(i+1))/2, 0);
351 endfor;
352 \end{lstlisting}
353
354 \begin{mplibcode}
355 pair a, b;
356 a := (0, 0);
357 for i := 1 step 1 until 6:
358 springwidth := 1/16cm + i*1/48cm;
359 b := (i*1/3cm, - i*1/5cm);
360 draw spring (a, b, 10) shifted (2/5cm*(i*(i+1))/2, 0);
361 endfor;
362 \end{mplibcode}
363
364 \subsection{solidSurface (\emph{path})}
365 This macro returns a \texttt{picture} of a solid surface on the right side of a given \texttt{path}.
366
367 \begin{lstlisting}
368 path p;
369 p := (0,0){dir(30)}..(5cm, 0)..{dir(30)}(10cm, 0);
370 draw solidSurface(p);
371 \end{lstlisting}
372
373 \begin{mplibcode}
374 path p;
375 p := (0,0){dir(30)}..(5cm, 0)..{dir(30)}(10cm, 0);
376 draw solidSurface(p);
377 \end{mplibcode}
378
379 \subsection{solid (\emph{path, angle, type})}
380 Fills given \texttt{path} with strokes of specific type at a given \texttt{angle}. \texttt{type} can be 0 (``solid'' strokes) and 1 (``glass'' strokes).
381
382 \begin{lstlisting}
383 path p[];
384 p1 := unitsquare scaled 2cm;
385 p2 := p1 shifted (4cm, 0);
386 draw solid(p1, 45, 0);
387 draw solid(p2, -45, 1);
388 \end{lstlisting}
389
390 \begin{mplibcode}
391 path p[];
392 p1 := unitsquare scaled 2cm;
393 p2 := p1 shifted (4cm, 0);
394 draw solid(p1, 45, 0);
395 draw solid(p2, -45, 1);
396 \end{mplibcode}
397
398 \subsection{woodBlock (\emph{width, height})}
399 Returns a \texttt{picture} of a rectangular block of wood with its bottom-left corner in the origin.
400
401 \begin{lstlisting}
402 draw woodBlock(10cm, 1/2cm);
403 \end{lstlisting}
404
405 \begin{mplibcode}
406 draw woodBlock(10cm, 1/2cm);
407 \end{mplibcode}
408
409 \subsection{woodenThing (\emph{path, angle})}
410 Returns a \texttt{picture} of a wood texture at a given \texttt{angle} fitted into a given \texttt{path}.
411
412 \begin{lstlisting}
413 path p, q;
414 p := dir(-60) scaled 1/2 -- dir(90) scaled 2/3 -- dir (-120) scaled 3/5 -- cycle;
415 for i := 1 step 1 until 6:
416 q := (p scaled 3/2cm) rotated (i*60);
417 draw woodenThing (q, i*60) shifted (2cm*i, 0);
418 endfor;
419 \end{lstlisting}
420
421 \begin{mplibcode}
422 path p, q;
423 p := dir(-60) scaled 1/2 -- dir(90) scaled 2/3 -- dir (-120) scaled 3/5 -- cycle;
424 for i := 1 step 1 until 6:
425 q := (p scaled 3/2cm) rotated (i*60);
426 draw woodenThing (q, i*60) shifted (2cm*i, 0);
427 endfor;
428 \end{mplibcode}
429
430 \subsection{woodenSurface (\emph{path})}
431
432 Returns a \texttt{picture} similar to that of a solid surface on the right side of a given \texttt{path}, but with wood texture .
433
434 \begin{lstlisting}
435 path p;
436 p := (0,0){dir(30)}..(5cm, 0)..{dir(30)}(10cm, 0);
437 draw woodenSurface(p);
438 \end{lstlisting}
439
440 \begin{mplibcode}
441 path p;
442 p := (0,0){dir(30)}..(5cm, 0)..{dir(30)}(10cm, 0);
443 draw woodenSurface(p);
444 \end{mplibcode}
445
446 \subsection{globe (\emph{radius, longitude, latitude})}
447 This macro returns a \texttt{picture} of the globe of specified \texttt{radius} centered at specific \texttt{longitude} and \texttt{latitude};
448
449 \begin{lstlisting}
450 for i := 1 step 1 until 6:
451 draw globe(i*1/4cm, i*60, -90 + i*30)
452 shifted (1/2cm*(i*(i+1))/2, 0);
453 endfor;
454 \end{lstlisting}
455
456 \begin{mplibcode}
457 for i := 1 step 1 until 6:
458 draw globe(i*1/4cm, i*60, -90 + i*30)
459 shifted (1/2cm*(i*(i+1))/2, 0);
460 endfor;
461 \end{mplibcode}
462
463 \subsection{Knots}
464 There are two macros to handle knot drawing: \texttt{addStrandToKnot} and \texttt{knot\-From\-Strands}. Currently the algorithm is not especially stable.
465
466 % \subsubsection{initKnot (\emph{knotName})}
467 % This macro creates an empty knot with no strands named \texttt{knotName} and returns nothing. Useful if a knot is redefined with the same name repeatedly.
468
469 \subsubsection{addStrandToKnot (\emph{knotName}) (\emph{path, ropeWidth, ropeType, intersectionOrder})}
470 This macro adds a strand to knot named \texttt{knotName} and returns nothing. Strand follows the given \texttt{path} and has a given \texttt{ropeWidth}. \texttt{ropeType} can be \texttt{"l"}, \texttt{"t"} (as in \texttt{tube.l} and \texttt{tube.t}) or \texttt{"e"} (for an unshaded strand). \texttt{intersectionOrder} is a string of comma separated numbers which represent a ``layer'' to which intersections along the strand go.
471
472 \subsubsection{knotFromStrands (\emph{knotName})}
473 This macro returns a picture of a knot with a given \texttt{knotName}.
474
475 \begin{lstlisting}
476 path p[];
477 p1 := (dir(90)*4/3cm) {dir(0)} .. tension 3/2
478 .. (dir(90 + 120)*4/3cm){dir(90 + 30)} .. tension 3/2
479 .. (dir(90 - 120)*4/3cm){dir(-90 - 30)} .. tension 3/2
480 .. cycle;
481 p2 := (fullcircle scaled 3cm) shifted (0, -3/2cm);
482 p3 := (fullcircle scaled 4cm);
483 addStrandToKnot (theknot) (p1 shifted (4cm, -4cm), 1/5cm, "l",
484 "-1,1,-1,1,-1,1,-1,1,-1");
485 addStrandToKnot (theknot) (p2 shifted (4cm, -4cm), 1/6cm, "s",
486 "");
487 addStrandToKnot (theknot) (p3 shifted (4cm, -4cm), 1/7cm, "e",
488 "-1,1");
489 draw knotFromStrands (theknot);
490 \end{lstlisting}
491
492 \begin{mplibcode}
493 path p[];
494 p1 := (dir(90)*4/3cm) {dir(0)} .. tension 3/2
495 .. (dir(90 + 120)*4/3cm){dir(90 + 30)} .. tension 3/2
496 .. (dir(90 - 120)*4/3cm){dir(-90 - 30)} .. tension 3/2
497 .. cycle;
498 p2 := (fullcircle scaled 3cm) shifted (0, -3/2cm);
499 p3 := (fullcircle scaled 4cm);
500 addStrandToKnot (theknot) (p1 shifted (4cm, -4cm), 1/5cm, "l",
501 "-1,1,-1,1,-1,1,-1,1,-1");
502 addStrandToKnot (theknot) (p2 shifted (4cm, -4cm), 1/6cm, "s",
503 "");
504 addStrandToKnot (theknot) (p3 shifted (4cm, -4cm), 1/7cm, "e",
505 "-1,1");
506 draw knotFromStrands (theknot);
507 \end{mplibcode}
508
509 \subsection{Other 3D contraptions}
510 Some macros can be used to shade 3D polygons. Currently only flat surfaces re supported
511
512 \subsubsection{flatSurface\emph{\#@}(\emph{surface outline path, normal vector, hatch angle})}
513 This macro returns a \texttt{picture} of a flat surface with the specified \texttt{surface outline path} with the given \texttt{normal vector}, illuminated from the direction determined by \texttt{lightDirection} and with hatches aligned at the andle \texttt{hatch angle}. If \texttt{\#@} is \texttt{".hatches"} then the surface is shaded with hatches, if it's \texttt{".stipples"}, then the surface is shaded with stipples.
514
515 \begin{mplibcode}
516 path p[];
517 p1 := unitsquare xscaled 1cm yscaled 2cm;
518 p2 := p1 shifted (1cm, 0);
519 draw flatSurface.hatches(p1, (-1,0,1), 45);
520 draw flatSurface.hatches(p2, (1,0,1), 45);
521 draw p1; draw p2;
522 \end{mplibcode}
523
524 \section{Other macros}
525 Some macros that are not directly related to drawing are listed below
526
527 \subsection{refractionPath (\emph{initial ray, shape, refraction coefficient})}
528 This macro returns a \texttt{path} that represent refraction of some \texttt{ray} (any variable of type \texttt{path}, point next to last in a given path is considered a source of a ``ray'', and last point determines its direction) through some \texttt{shape} with given \texttt{refraction coefficient}. When appropriate, the ``ray'' is fully internally reflected.
529
530 Setting \texttt{refraction coefficient} to 0 results in reflection instead of refraction in all cases.
531
532 \begin{lstlisting}
533 path r, p;
534 p := fullcircle scaled 2.1cm;
535 draw p;
536 for i:= 1cm step -1/4cm until -1cm:
537 r := (-4cm, i) -- (-1cm, i);
538 draw refractionPath(r, p, 1.5);
539 endfor;
540 \end{lstlisting}
541
542 \begin{mplibcode}
543 path r, p;
544 p := fullcircle scaled 2.1cm;
545 draw p;
546 for i:= 1cm step -1/4cm until -1cm:
547 r := (-4cm, i) -- (-1cm, i);
548 draw refractionPath(r, p, 1.5);
549 endfor;
550 \end{mplibcode}
551
552
553 \subsection{lens (\emph{(left radius, right radius), thickness, diameter, units})}
554 This macro returns a \texttt{path} that represent a section of a lens with given radii of curvature (positive value for convex, negative --- for concave), thickness (i. e. distance benween sides' centers) and diameter (i. e. height) in given arbitrary units.
555
556 \begin{lstlisting}
557 draw lens((5, 10), 1/2, 2, cm);
558 draw lens((-10, -5), 1/4, 2, cm) shifted (2cm, 0);
559 draw lens((infinity, 7), 1/4, 2, cm) shifted (4cm, 0);
560 \end{lstlisting}
561
562 \begin{mplibcode}
563 draw lens((5, 10), 1/2, 2, cm);
564 draw lens((-10, -5), 1/4, 2, cm) shifted (2cm, 0);
565 draw lens((infinity, 7), 1/4, 2, cm) shifted (4cm, 0);
566 \end{mplibcode}
567
568 \section{Auxilary macros}
569 Some macros that not related to physical problems at all are listed below.
570
571 \subsection{\emph{picture} maskedWith \emph{path}}
572 This macro masks a part of a \texttt{picture} with closed \texttt{path}. In fact this is inversion of \METAPOST's built-in \texttt{clip} but, in contrast to the latter, it does not modify original image. Note that it requires that counter-clockwise \texttt{path} to work properly.
573
574 \subsection{\emph{path} firstIntersectionTimes \emph{path}}
575 This macro is similar to \METAPOST's \texttt{intersectiontimes} but it returns intersection times with smallest time on first path.
576
577 \subsection{pathSubdivide \emph{path, n}}
578 This macro returns original \texttt{path} with \texttt{n}-times more points.
579
580 \subsection{drawmidarrow (\emph{path})}
581 Draws \texttt{path} with arrows in the middles of segments with length no less than \texttt{midArrowLimit} (another global variable, 1cm by default).
582
583 \subsection{markAngle (\emph{point a, point o, point b})(\emph{text})}
584 This macro marks an angle \texttt{aob} (counter-clockwise) with some \texttt{text}
585
586 \begin{lstlisting}
587 pair a, o, b;
588 for i:= 30 step 60 until 390:
589 o := (10cm*(i/360), 0);
590 a := dir(i/2)*4/3cm shifted o;
591 b := dir(i)*4/3cm shifted o;
592 draw (a--o--b);
593 markAngle(a, o, b)(btex $\alpha$ etex);
594 endfor;
595 \end{lstlisting}
596
597 \begin{mplibcode}
598 pair a, o, b;
599 for i:= 30 step 60 until 390:
600 o := (10cm*(i/360), 0);
601 a := dir(i/2)*4/3cm shifted o;
602 b := dir(i)*4/3cm shifted o;
603 draw (a--o--b);
604 markAngle(a, o, b)(btex $\alpha$ etex);
605 endfor;
606 \end{mplibcode}
607
608
609 \section{Some examples}
610
611 \subsection{Gregory-Maksutov type telescope}
612 Lines 3--11 define parameters of lenses and mirrors\footnote{Taken from here http://www.google.ru/patents/US2701983}. Lines 12--16 generate lenses. On line 20 shape of prism is defined. Line 22 cuts part of the rear mirror. Line 26 describes mirror part of the front lens. On lines 31--33 all the glass parts are drawn. On lines 34--36 all the mirror ones. On lines 39--44 rays are traced through all system in order specified in loop on line 41. Lines 49--56 are about the telescope frame.
613 \begin{lstlisting}
614 path p[], q[], axis[], f[]; pair o;
615 u := 6mm;
616 r1 := -36.979; r2 := -r1; t1 := 0.7; n1 := 1.517; d1 := 5;
617 l1 := 8.182;
618 r3 := -11.657; r4 := r3; t2 := 0.2; n2 := n1; d2 := 3/2;
619 l2 := 0.4;
620 r5 := -30.433; r6 := 9.598; t3 := 0.39; n3 := 1.621; d3 := d2;
621 l3 := 0.828;
622 r7 := -35.512; r8 := infinity; t4 := 0.7; n4 := 0; d4 := d1;
623 l4 := 5.272;
624 ll0 := 0;
625 for i := 1 upto 4:
626 ll[i] := ll[i-1] + t[i] + l[i];
627 p[i] := lens ((r[i*2 - 1], r[i*2]), t[i], d[i], u)
628 shifted (ll[i-1]*u, 0);
629 endfor;
630 axis1 := (0, 0) -- (ll4*u, 0);
631 axis2 := reverse(axis1);
632 ll5 := ll4 - 1/2l4;
633 p5 := ((-1,-1) -- (1,1) -- (-1,1) -- cycle)
634 scaled (1/2d2*u) shifted (ll5*u, 0);
635 p6 := (subpath
636 (ypart((axis1 shifted (0, 1/2d2*u)) firstIntersectionTimes p4),
637 ypart((axis2 shifted (0, 1/2d2*u)) firstIntersectionTimes p4))
638 of p4) -- cycle;
639 p7 := (subpath
640 (ypart((axis2 shifted (0, 3/4d2*u)) firstIntersectionTimes p1),
641 ypart((axis2 shifted (0, -3/4d2*u)) firstIntersectionTimes p1))
642 of p1);
643 p7 := p7 -- (reverse(p7) shifted ((-1/3t1)*u, 0)) -- cycle;
644 for i := 1, 2, 3, 5:
645 draw p[i] withpen thickpen; draw solid (p[i], 45, 1);
646 endfor;
647 draw solid (p7, -45, 0);
648 draw p6 withpen thickpen; draw solid (p6, -45, 0);
649 draw p6 yscaled -1 withpen thickpen;
650 draw solid (p6 yscaled -1, -45, 0);
651 n7 := 0; n5 := n1;
652 for i := 0, 1:
653 q[i] := (-3/2u, -2u + i*4u) -- (16u, -2u + i*4u);
654 for j = 1, 4, 7, 2, 3, 5:
655 q[i] := refractionPath(q[i], p[j], n[j]);
656 endfor;
657 endfor;
658 o := whatever[point length(q0) of q0, point length(q0)-1 of q0]
659 = whatever[point length(q1) of q1, point length(q1)-1 of q1];
660 for i := 0, 1: drawmidarrow (q[i] -- o) withpen thinpen; endfor;
661 draw eye(-91) shifted o shifted (0, u);
662 f1 := (-1/4u, 1/2d1*u) -- ((ll3 + t4)*u, 1/2d1*u)
663 -- ((ll3 + t4)*u, 1/2d2*u);
664 f2 := (ll1*u - 1/8u, 1/2d2*u) -- (ll5*u - 1/2d2*u, 1/2d2*u)
665 -- (ll5*u - 1/2d2*u, ypart(o));
666 f3 := (ll1*u - 1/8u, -1/2d2*u) -- (ll5*u + 1/2d2*u, -1/2d2*u)
667 -- (ll5*u + 1/2d2*u, ypart(o));
668 draw f1 withpen fatpen; draw f1 yscaled -1 withpen fatpen;
669 draw f2 withpen fatpen; draw f3 withpen fatpen;
670 \end{lstlisting}
671
672 \begin{mplibcode}
673 path p[], q[], axis[], f[]; pair o;
674 u := 6mm;
675 r1 := -36.979; r2 := -r1; t1 := 0.7; n1 := 1.517; d1 := 5;
676 l1 := 8.182;
677 r3 := -11.657; r4 := r3; t2 := 0.2; n2 := n1; d2 := 3/2;
678 l2 := 0.4;
679 r5 := -30.433; r6 := 9.598; t3 := 0.39; n3 := 1.621; d3 := d2;
680 l3 := 0.828;
681 r7 := -35.512; r8 := infinity; t4 := 0.7; n4 := 0; d4 := d1;
682 l4 := 5.272;
683 ll0 := 0;
684 for i := 1 upto 4:
685 ll[i] := ll[i-1] + t[i] + l[i];
686 p[i] := lens ((r[i*2 - 1], r[i*2]), t[i], d[i], u)
687 shifted (ll[i-1]*u, 0);
688 endfor;
689 axis1 := (0, 0) -- (ll4*u, 0);
690 axis2 := reverse(axis1);
691 ll5 := ll4 - 1/2l4;
692 p5 := ((-1,-1) -- (1,1) -- (-1,1) -- cycle)
693 scaled (1/2d2*u) shifted (ll5*u, 0);
694 p6 := (subpath
695 (ypart((axis1 shifted (0, 1/2d2*u)) firstIntersectionTimes p4),
696 ypart((axis2 shifted (0, 1/2d2*u)) firstIntersectionTimes p4))
697 of p4) -- cycle;
698 p7 := (subpath
699 (ypart((axis2 shifted (0, 3/4d2*u)) firstIntersectionTimes p1),
700 ypart((axis2 shifted (0, -3/4d2*u)) firstIntersectionTimes p1))
701 of p1);
702 p7 := p7 -- (reverse(p7) shifted ((-1/3t1)*u, 0)) -- cycle;
703 for i := 1, 2, 3, 5:
704 draw p[i] withpen thickpen; draw solid (p[i], 45, 1);
705 endfor;
706 draw solid (p7, -45, 0);
707 draw p6 withpen thickpen; draw solid (p6, -45, 0);
708 draw p6 yscaled -1 withpen thickpen;
709 draw solid (p6 yscaled -1, -45, 0);
710 n7 := 0; n5 := n1;
711 for i := 0, 1:
712 q[i] := (-3/2u, -2u + i*4u) -- (16u, -2u + i*4u);
713 for j = 1, 4, 7, 2, 3, 5:
714 q[i] := refractionPath(q[i], p[j], n[j]);
715 endfor;
716 endfor;
717 o := whatever[point length(q0) of q0, point length(q0)-1 of q0]
718 = whatever[point length(q1) of q1, point length(q1)-1 of q1];
719 for i := 0, 1: drawmidarrow (q[i] -- o) withpen thinpen; endfor;
720 draw eye(-91) shifted o shifted (0, u);
721 f1 := (-1/4u, 1/2d1*u) -- ((ll3 + t4)*u, 1/2d1*u)
722 -- ((ll3 + t4)*u, 1/2d2*u);
723 f2 := (ll1*u - 1/8u, 1/2d2*u) -- (ll5*u - 1/2d2*u, 1/2d2*u)
724 -- (ll5*u - 1/2d2*u, ypart(o));
725 f3 := (ll1*u - 1/8u, -1/2d2*u) -- (ll5*u + 1/2d2*u, -1/2d2*u)
726 -- (ll5*u + 1/2d2*u, ypart(o));
727 draw f1 withpen fatpen; draw f1 yscaled -1 withpen fatpen;
728 draw f2 withpen fatpen; draw f3 withpen fatpen;
729 \end{mplibcode}
730
731 \subsection{L'H\^{o}pital's Pulley Problem}
732 Line 3 describe initial setup. Line 4 is problem's solution. Lines 8--11 set all the points where they belong. Lines 12--16 are needed to decide where to put the pulley.
733
734 \begin{lstlisting}
735 pair p[], o[];
736 numeric a, d[], l[], x[], y[];
737 l0 := 6; l1 := 4; l2 := 4;
738 x1 := (l1**2 + abs(l1)*((sqrt(8)*l0)++l1))/4l0;
739 y1 := l1+-+x1;
740 y2 := l2 - ((l0-x1)++y1);
741 d1 := 2/3cm; d2 := 4/3cm; d3 := 5/6d1;
742 p1 := (0, 0);
743 p2 := (l0*cm, 0);
744 p3 := (x1*cm, -y1*cm);
745 p4 := p3 shifted (0, -y2*cm);
746 o1 := (unitvector(p4-p3) rotated 90 scaled 1/2d3);
747 o2 := (unitvector(p3-p2) rotated 90 scaled 1/2d3);
748 p5 := whatever [p3 shifted o1, p4 shifted o1]
749 = whatever [p3 shifted o2, p2 shifted o2];
750 a := angle(p1-p3);
751 draw solidSurface(11/10[p1,p2] -- 11/10[p2, p1]);
752 draw pulley (d1, a - 90) shifted p5;
753 draw image(
754 draw p1 -- p3 -- p2 withpen thickpen;
755 draw p3 -- p4 withpen thickpen;
756 ) maskedWith (pulleyOutline shifted p5);
757 draw sphere.c(d2) shifted p4 shifted (0, -1/2d2);
758 dotlabel.llft(btex $A$ etex, p1);
759 dotlabel.lrt(btex $B$ etex, p2);
760 dotlabel.ulft(btex $C$ etex, p4);
761 label.llft(btex $l$ etex, 1/2[p1, p3]);
762 markAngle(p3, p1, p2)(btex $\alpha$ etex);
763 \end{lstlisting}
764
765 \begin{mplibcode}
766 pair p[], o[];
767 numeric a, d[], l[], x[], y[];
768 l0 := 6; l1 := 4; l2 := 4;
769 x1 := (l1**2 + abs(l1)*((sqrt(8)*l0)++l1))/4l0;
770 y1 := l1+-+x1;
771 y2 := l2 - ((l0-x1)++y1);
772 d1 := 2/3cm; d2 := 4/3cm; d3 := 5/6d1;
773 p1 := (0, 0);
774 p2 := (l0*cm, 0);
775 p3 := (x1*cm, -y1*cm);
776 p4 := p3 shifted (0, -y2*cm);
777 o1 := (unitvector(p4-p3) rotated 90 scaled 1/2d3);
778 o2 := (unitvector(p3-p2) rotated 90 scaled 1/2d3);
779 p5 := whatever [p3 shifted o1, p4 shifted o1]
780 = whatever [p3 shifted o2, p2 shifted o2];
781 a := angle(p1-p3);
782 draw solidSurface(11/10[p1,p2] -- 11/10[p2, p1]);
783 draw pulley (d1, a - 90) shifted p5;
784 draw image(
785 draw p1 -- p3 -- p2 withpen thickpen;
786 draw p3 -- p4 withpen thickpen;
787 ) maskedWith (pulleyOutline shifted p5);
788 draw sphere.c(d2) shifted p4 shifted (0, -1/2d2);
789 dotlabel.llft(btex $A$ etex, p1);
790 dotlabel.lrt(btex $B$ etex, p2);
791 dotlabel.ulft(btex $C$ etex, p4);
792 label.llft(btex $l$ etex, 1/2[p1, p3]);
793 markAngle(p3, p1, p2)(btex $\alpha$ etex);
794 \end{mplibcode}
795
796 \subsection{Hooke's law}
797
798 \begin{lstlisting}
799 numeric l[],d, h;
800 pair p[], q[];
801 l0 := 3/2cm; l1 := 1cm;
802 d := 1cm; h := 7/8cm;
803 p1 := (0, 0); p2 := (0, -l0);
804 p3 := (d, 0); p4 := (d, -l0-l1);
805 p5 := (2d, 0); p6 := (2d, -l0-2l1); p7 := (2d, -l0-2l1-h);
806 draw solidSurface((2d + 1/2cm, 0)--(-1/2cm, 0));
807 draw spring(p1, p2, 20);
808 draw spring(p3, p4, 20);
809 draw weight.h(h) shifted p4;
810 draw spring(p5, p6, 20);
811 draw weight.h(h) shifted p6;
812 draw weight.h(h) shifted p7;
813 q1 := (0, ypart(p2));
814 q2 := (0, ypart(p4));
815 q3 := (0, ypart(p6));
816 draw p2 -- q1 withpen thinpen;
817 draw p4 -- q2 withpen thinpen;
818 draw p6 -- q3 withpen thinpen;
819 drawdblarrow q1--q2 withpen thinpen;
820 drawdblarrow q2--q3 withpen thinpen;
821 label.lft (btex $x$ etex, 1/2[q1, q2]);
822 label.lft (btex $x$ etex, 1/2[q2, q3]);
823 \end{lstlisting}
824
825 \begin{mplibcode}
826 numeric l[],d, h;
827 pair p[], q[];
828 l0 := 3/2cm; l1 := 1cm;
829 d := 1cm; h := 7/8cm;
830 p1 := (0, 0); p2 := (0, -l0);
831 p3 := (d, 0); p4 := (d, -l0-l1);
832 p5 := (2d, 0); p6 := (2d, -l0-2l1); p7 := (2d, -l0-2l1-h);
833 draw solidSurface((2d + 1/2cm, 0)--(-1/2cm, 0));
834 draw spring(p1, p2, 20);
835 draw spring(p3, p4, 20);
836 draw weight.h(h) shifted p4;
837 draw spring(p5, p6, 20);
838 draw weight.h(h) shifted p6;
839 draw weight.h(h) shifted p7;
840 q1 := (0, ypart(p2));
841 q2 := (0, ypart(p4));
842 q3 := (0, ypart(p6));
843 draw p2 -- q1 withpen thinpen;
844 draw p4 -- q2 withpen thinpen;
845 draw p6 -- q3 withpen thinpen;
846 drawdblarrow q1--q2 withpen thinpen;
847 drawdblarrow q2--q3 withpen thinpen;
848 label.lft (btex $x$ etex, 1/2[q1, q2]);
849 label.lft (btex $x$ etex, 1/2[q2, q3]);
850 \end{mplibcode}
851
852 \subsection{Weight on a cart}
853
854 \begin{lstlisting}
855 numeric l, w, r, h;
856 l := 4cm;
857 w := 1/4cm;
858 r := 2/3cm;
859 h := 1cm;
860 draw solidSurface((-1/5l, 0) -- (6/5l, 0));
861 draw woodBlock (l, w) shifted (0, r);
862 draw wheel (r, 0) shifted (r, 1/2r);
863 draw wheel (r, 0) shifted (l-r, 1/2r);
864 draw weight.s(h) shifted (1/2l, r + w);
865 \end{lstlisting}
866
867 \begin{mplibcode}
868 numeric l, w, r, h;
869 l := 4cm;
870 w := 1/4cm;
871 r := 2/3cm;
872 h := 1cm;
873 draw solidSurface((-1/5l, 0) -- (6/5l, 0));
874 draw woodBlock (l, w) shifted (0, r);
875 draw wheel (r, 0) shifted (r, 1/2r);
876 draw wheel (r, 0) shifted (l-r, 1/2r);
877 draw weight.s(h) shifted (1/2l, r + w);
878 \end{mplibcode}
879
880 \subsection{Some knots}
881
882 \begin{lstlisting}
883 path p[];
884 p1 := (dir(90)*4/3cm) {dir(0)} .. tension 3/2
885 .. (dir(90 + 120)*4/3cm){dir(90 + 30)} .. tension 3/2
886 .. (dir(90 - 120)*4/3cm){dir(-90 - 30)} .. tension 3/2
887 .. cycle;
888 p1 := p1 scaled 6/5;
889 addStrandToKnot (primeOne) (p1, 1/4cm, "l", "1, -1, 1");
890 draw knotFromStrands (primeOne);
891 p2 := (0, 2cm) .. (1/2cm, 3/2cm) .. (-1/2cm, 0)
892 .. (1/2cm, -2/3cm) .. (4/3cm, 0) .. (0, 17/12cm)
893 .. (-4/3cm, 0) .. (-1/2cm, -2/3cm) .. (1/2cm, 0)
894 .. (-1/2cm, 3/2cm) .. cycle;
895 p2 := p2 scaled 6/5;
896 addStrandToKnot (primeTwo) (p2, 1/4cm, "l", "1, -1, 1, -1, 1");
897 draw knotFromStrands (primeTwo) shifted (4cm, 0);
898 p3 := (dir(0)*3/2cm) .. (dir(1*72)*2/3cm)
899 .. (dir(2*72)*3/2cm) .. (dir(3*72)*2/3cm)
900 .. (dir(4*72)*3/2cm) .. (dir(0)*2/3cm)
901 .. (dir(1*72)*3/2cm) .. (dir(2*72)*2/3cm)
902 .. (dir(3*72)*3/2cm) .. (dir(4*72)*2/3cm)
903 .. cycle;
904 p3 := (p3 rotated (72/4)) scaled 6/5;
905 addStrandToKnot (primeThree) (p3, 1/4cm, "l", "-1, 1, -1, 1, -1");
906 draw knotFromStrands (primeThree) shifted (8cm, 0);
907 \end{lstlisting}
908
909 \begin{mplibcode}
910 path p[];
911 p1 := (dir(90)*4/3cm) {dir(0)} .. tension 3/2
912 .. (dir(90 + 120)*4/3cm){dir(90 + 30)} .. tension 3/2
913 .. (dir(90 - 120)*4/3cm){dir(-90 - 30)} .. tension 3/2
914 .. cycle;
915 p1 := p1 scaled 6/5;
916 addStrandToKnot (primeOne) (p1, 1/4cm, "l", "1, -1, 1");
917 draw knotFromStrands (primeOne);
918 p2 := (0, 2cm) .. (1/2cm, 3/2cm) .. (-1/2cm, 0)
919 .. (1/2cm, -2/3cm) .. (4/3cm, 0) .. (0, 17/12cm)
920 .. (-4/3cm, 0) .. (-1/2cm, -2/3cm) .. (1/2cm, 0)
921 .. (-1/2cm, 3/2cm) .. cycle;
922 p2 := p2 scaled 6/5;
923 addStrandToKnot (primeTwo) (p2, 1/4cm, "s", "1, -1, 1, -1, 1");
924 draw knotFromStrands (primeTwo) shifted (4cm, -2cm);
925 p3 := (dir(0)*3/2cm) .. (dir(1*72)*2/3cm)
926 .. (dir(2*72)*3/2cm) .. (dir(3*72)*2/3cm)
927 .. (dir(4*72)*3/2cm) .. (dir(0)*2/3cm)
928 .. (dir(1*72)*3/2cm) .. (dir(2*72)*2/3cm)
929 .. (dir(3*72)*3/2cm) .. (dir(4*72)*2/3cm)
930 .. cycle;
931 p3 := (p3 rotated (72/4)) scaled 6/5;
932 addStrandToKnot (primeThree) (p3, 1/4cm, "e", "-1, 1, -1, 1, -1");
933 draw knotFromStrands (primeThree) shifted (8cm, 0);
934 \end{mplibcode}
935
936
937 \end{document}