Debian Codegraph @

texmf-dist/source/metapost/shapes/shapes.dtx

raw
1 % \iffalse
2 % +AMDG This document was begun on 1E June 11EE, the vigil
3 % of the Nativity of St. John the Baptist, and it is humbly
4 % dedicated to him and to the Immaculate Heart of Mary, for
5 % their prayers, and to the Sacred Heart of Jesus, for His
6 % mercy.
7 %
8 % This document is copyright 2015 by Donald P. Goodman, and is
9 % released publicly under the LaTeX Project Public License. The
10 % distribution and modification of this work is constrained by the
11 % conditions of that license. See
12 % http://www.latex-project.org/lppl.txt
13 % for the text of the license. This document is released
14 % under version 1.3 of that license, and this work may be distributed
15 % or modified under the terms of that license or, at your option, any
16 % later version.
17 %
18 % This work has the LPPL maintenance status 'maintained'.
19 %
20 % The Current Maintainer of this work is Donald P. Goodman
21 % ([email protected]).
22 %
23 % This work consists of the files shapes.dtx and
24 % shapes.ins, along with the generated file shapes.mp
25 % and shapes.pdf, and the README.
26 % \fi
27
28 % \iffalse
29 %<*driver>
30
31 \documentclass{ltxdoc}
32 \usepackage{docmfp}
33 \usepackage{url}
34 \usepackage[typeone]{dozenal}
35 \usepackage{verbatim}
36 \usepackage{mflogo}
37 \usepackage[everymp={input shapes;},latex]{gmp}
38 \usepackage{makeidx}
39 \makeindex
40 \long\def\demonstrate#1#2{%
41 \bigskip%
42 \hrule%
43 \hbox to\linewidth{%
44 \hbox to0.65\linewidth{%
45 \vbox to1.4in{\vfil#1\vfil}%
46 }\hbox to0.35\linewidth{%
47 \vbox to1.4in{\vfil#2\vfil}%
48 }%
49 }%
50 \hrule%
51 \bigskip%
52 }
53
54 \begin{document}
55 \DocInput{shapes.dtx}
56 \end{document}
57 %</driver>
58 % \fi
59 % \title{The \texttt{shapes} Macros, v1.0}
60 % \author{Donald P.\ Goodman III}
61 % \date{\today}
62 %
63 % \maketitle
64 % \begin{abstract}\noindent
65 % The |shapes| macros for \MP\ provide regular polygons,
66 % their corresponding reentrant stars, and images
67 % demonstrating fractions. These macros are quite
68 % configurable.
69 % \end{abstract}
70 %
71 % \tableofcontents
72 %
73 % \section{Introduction}
74 %
75 % The |shapes| macros are not revolutionary, and in fact are
76 % quite simple; however, I spent some time generalizing them
77 % for a text I'm currently working on, and so I thought they
78 % might be useful for the general populace. They are
79 % divided into two main groups: regular polygons and their
80 % corresponding reentrant star shapes; and fractionals,
81 % circles divided into a certain number of parts with the
82 % desired fraction filled in.
83 %
84 % This document was typeset in accordance with the
85 % |docstrip| utility, which allows the automatic extraction
86 % of code and documentation from the same document.
87 %
88 % \section{Prerequisites and Conventions}
89 %
90 % Some prerequesites for using this package are \MP\ itself
91 % (obviously). If you're using the package with \LaTeX, the
92 % |gmp| package would probably be helpful; be sure to use
93 % the |latex| package option.
94 % These should be packaged in any
95 % reasonably modern \LaTeX\ system, such as \TeX Live or
96 % Mik\TeX.
97 %
98 % This documentation assumes nothing about your personal
99 % \TeX\ or \MP\ environment. Con\TeX t and the various
100 % forms of Lua\TeX\ have \MP\ built-in; with pdf\LaTeX, the
101 % author's choice, one can use the |gmp| package to
102 % include the source directly in one's document (that's
103 % what's been done in this documentation) or develop a
104 % simple script to compile them afterwards and include them
105 % in the source via |\includegraphics| (probably the
106 % quickest option, since compilation is done in advance).
107 % Here, we simply post the plain vanilla \MP\ code, and let
108 % you work out those details however you prefer.
109 %
110 % \section{The Shapes Macros}
111 %
112 % We begin with the simple shapes macros, which are about as
113 % basic as they can be.
114 %
115 % All of these shapes default to
116 % circles with a one inch diameter, so you can scaled them
117 % with that kept in mind.
118 %
119 % The simplest case is with a regular
120 % polygon, like so:
121 %
122 % \demonstrate{%
123 % \hbox{|modfig(6,0);|}
124 % \hbox{|draw modfigure;|}
125 % }{%
126 % \begin{mpost}
127 % modfig(6,0); draw modfigure;
128 % \end{mpost}
129 % }%
130 %
131 % Of course, these drawn figures can be manipulated in the
132 % usual ways:
133 %
134 % \demonstrate{%
135 % \hbox{|modfig(6,0);|}
136 % \hbox{|draw modfigure rotated (45) withcolor red;|}
137 % }{%
138 % \begin{mpost}
139 % modfig(6,0); draw modfigure rotated (45) withcolor red;
140 % \end{mpost}
141 % }%
142 %
143 % The two arguments to |modfig| are simple: the first tells
144 % the number of sides desired, while the second means
145 % \emph{no numbers are printed} if 0, and \emph{print
146 % numbers} if 1:
147 %
148 % \demonstrate{%
149 % \hbox{|modfig(8,1);|}
150 % \hbox{|draw modfigure;|}
151 % }{%
152 % \begin{mpost}
153 % modfig(8,1); draw modfigure;
154 % \end{mpost}
155 % }%
156 %
157 % If you don't want the circle to be printed, simply tell
158 % \MP\ with |modcircle := false;| and it will not print it:
159 %
160 % \demonstrate{%
161 % \hbox{|modcircle := false;|}
162 % \hbox{|modfig(8,1);|}
163 % \hbox{|draw modfigure;|}
164 % }{%
165 % \begin{mpost}
166 % modcircle := false;
167 % modfig(8,1); draw modfigure;
168 % \end{mpost}
169 % }%
170 %
171 % |modcircle| defaults to |true|.
172 %
173 % Notice that |modfig| doesn't care if there's a circle or
174 % not; if you want numbers on the vertices, it will print
175 % them there.
176 %
177 % You can adjust the width of the lines by specifying
178 % |modcirclepen| and |modshapepen|:
179 %
180 % \demonstrate{%
181 % \hbox{|modcirclepen := pencircle scaled 3;|}
182 % \hbox{|modshapepen := pencircle scaled 2;|}
183 % \hbox{|modfig(4,0);|}
184 % \hbox{|draw modfigure;|}
185 % }{%
186 % \begin{mpost}
187 % modcirclepen := pencircle scaled 3;
188 % modshapepen := pencircle scaled 2;
189 % modfig(4,0); draw modfigure;
190 % \end{mpost}
191 % }%
192 %
193 % Both |modcirclepen| and |modshapepen| default to
194 % |pencircle scaled 1|.
195 %
196 % We can also put ticks around the outside of the circle,
197 % at whatever intervals we want. The parameters here are
198 % \DescribeMacro{modticks}|modticks|, which is a boolean set
199 % to |false| by default. Set it to |true| if you want
200 % ticks. You can control the number of ticks with
201 % \DescribeMacro{nummodticks}|nummodticks|, which defaults
202 % to twelve. You can control the length of those ticks with
203 % \DescribeMacro{ticklen}|ticklen|, which defaults to
204 % |0.1in|. Finally, you can control the pen that is used to
205 % draw the ticks, which is \DescribeMacro{tickpen}|tickpen|,
206 % which defaults to |pencircle scaled 1|:
207 %
208 % \demonstrate{%
209 % \hbox{|nummodticks := 6;|}
210 % \hbox{|ticklen := 0.2in;|}
211 % \hbox{|modticks := true;|}
212 % \hbox{|tickpen := pensquare scaled 2;|}
213 % \hbox{|modshapepen := pensquare scaled 4;|}
214 % \hbox{|modfig(4,0);|}
215 % \hbox{|draw modfigure;|}
216 % }{%
217 % \begin{mpost}
218 % nummodticks := 6;
219 % ticklen := 0.2in;
220 % modticks := true;
221 % tickpen := pensquare scaled 2;
222 % modshapepen := pensquare scaled 4;
223 % modfig(4,0); draw modfigure;
224 % \end{mpost}
225 % }%
226 %
227 % We can do essentially the same thing with reentrant star
228 % figures with |modstar|. Unlike |modfig|, |modstar| takes
229 % \emph{three} arguments: the number of vertices, whether
230 % or not you want those vertices numbered, and how many
231 % points you want to skip as you go around the circle.
232 %
233 % \demonstrate{%
234 % \hbox{|modstar(11,1,3);|}
235 % \hbox{|draw modfigure;|}
236 % }{%
237 % \begin{mpost}
238 % modstar(11,1,3); draw modfigure;
239 % \end{mpost}
240 % }%
241 %
242 % Note that the third argument actually skips $n - 1$
243 % points, not $n$ points. But all the same parameters we
244 % saw when looking at |modfig| will still work in the same
245 % way:
246 %
247 % \demonstrate{%
248 % \hbox{|modcirclepen := pencircle scaled 1;|}
249 % \hbox{|modshapepen := pencircle scaled 2;|}
250 % \hbox{|modstar(11,1,4);|}
251 % \hbox{|draw modfigure;|}
252 % }{%
253 % \begin{mpost}
254 % modcirclepen := pencircle scaled 1;
255 % modshapepen := pencircle scaled 2;
256 % modstar(11,1,4); draw modfigure;
257 % \end{mpost}
258 % }%
259 %
260 % And that's about all there is to it.
261 %
262 % \section{Fraction Images}
263 % \label{sect:fraimg}
264 %
265 % The following macros are useful for demonstrating the
266 % nature and size of fractions in a visible way. The name
267 % of the game here is |fraccirc|, which takes two arguments:
268 % the number of parts to be filled, and the number of parts
269 % in the whole:
270 %
271 % \demonstrate{%
272 % \hbox{|fraccirc(7)(12);|}
273 % \hbox{|draw thefrac;|}
274 % }{%
275 % \begin{mpost}
276 % fraccirc(7)(12);
277 % draw thefrac;
278 % \end{mpost}
279 % }
280 %
281 % Note that, due to the internal implementation, these two
282 % arguments must each be enclosed in their own parentheses.
283 %
284 % We can adjust these things as appropriate. For example,
285 % we can fill with blue rather than red, and use thicker
286 % lines:
287 %
288 % \demonstrate{%
289 % \hbox{|fracfillcolor := blue;|}
290 % \hbox{|fraccirclepen := pencircle scaled 1;|}
291 % \hbox{|fractionpen := pencircle scaled 2;|}
292 % \hbox{|fraccirc(4)(7);|}
293 % \hbox{|draw thefrac;|}
294 % }{%
295 % \begin{mpost}
296 % fracfillcolor := blue;
297 % fraccirclepen := pencircle scaled 1;
298 % fractionpen := pencircle scaled 2;
299 % fraccirc(4)(7);
300 % draw thefrac;
301 % \end{mpost}
302 % }
303 %
304 % As the above example suggests, |fracfillcolor| gives the
305 % color with which the portion of the fraction should be
306 % filled; it defaults to |red|. |fraccirclepen| is the pen
307 % used to draw the circle around the fraction; it defaults
308 % to |pencircle scaled 1.5|. Finally, |fractionpen| is the
309 % pen used to draw the partition and the circle immediately
310 % surrounding them; it defaults to |pencircle scaled 1|.
311 %
312 % \section{Implementation}
313 % \label{sect:implem}
314 %
315 % \begin{macrocode}
316 color fracfillcolor; fracfillcolor := red;
317 pen fraccirclepen; fraccirclepen := pencircle scaled 1.5;
318 pen fractionpen; fractionpen := pencircle scaled 1;
319 def fraccirc(suffix x)(expr y) =
320 radius := 1in;
321 ticklen := radius/24;
322 path circ; circ := fullcircle scaled radius;
323 pair p[]; pair q[]; pair r[]; pair s[];
324 p[0] := (0,0) shifted (0,radius/2);
325 q[0] := p[0] shifted (0,ticklen);
326 r[0] := p[0] shifted (0,-ticklen);
327 s[0] := r[0];
328 picture thefrac;
329 picture addition;
330 thefrac := image(pickup fraccirclepen; draw circ;);
331 addition := image(pickup fractionpen; draw p[0]--q[0];);
332 addto thefrac also addition;
333 for i=1 upto 12:
334 p[i] := p[i-1] rotatedaround ((0,0),-30);
335 q[i] := q[i-1] rotatedaround ((0,0),-30);
336 addition := image(pickup fractionpen; draw p[i]--q[i];);
337 addto thefrac also addition;
338 endfor;
339 addition := image(pickup fractionpen; draw (0,0)--r[0];);
340 addto thefrac also addition;
341 pair t; pair q;
342 addition := image(
343 t = r[0] rotatedaround ((0,0),-((360/y)*x)/2);
344 q = r[0] rotatedaround ((0,0),-((360/y)*x));
345 fill r[0]--(0,0)--q..t..cycle withcolor fracfillcolor;
346 draw r[0]--(0,0)--q..t..cycle;
347 );
348 addto thefrac also addition;
349 for i=1 upto y:
350 r[i] := r[i-1] rotatedaround ((0,0),-(360/y));
351 s[i] := r[i-1] rotatedaround ((0,0),-(360/y)/2);
352 addition := image(%
353 pickup fractionpen;%
354 draw (0,0)--r[i];
355 draw r[i-1]..s[i]..r[i];
356 );
357 addto thefrac also addition;
358 endfor;
359 enddef;
360 % put in the pens for the modular shapes
361 boolean modcircle; modcircle := true;
362 pen modcirclepen; modcirclepen := pencircle scaled 1;
363 pen modshapepen; modshapepen := pencircle scaled 1;
364 boolean modticks; modticks := false;
365 numeric nummodticks; nummodticks := 12;
366 numeric ticklen; ticklen := 0.1in;
367 pen tickpen; tickpen := pencircle scaled 1;
368 def modstar(expr numpoints,numbers,numstar) =
369 picture modfigure;
370 if (modcircle = true):
371 modfigure := image(draw fullcircle scaled 1in withpen modcirclepen;);
372 else:
373 modfigure := image();
374 fi;
375 picture modtickpic;
376 if (modticks = true):
377 modtickpic := image(
378 pair tickstart[]; pair tickend[];
379 tickstart[0] = (0,0) shifted (0,0.5in);
380 tickend[0] = tickstart[0] shifted (0,ticklen);
381 draw tickstart[0]--tickend[0] withpen tickpen;
382 for i=1 upto (nummodticks-1):
383 tickstart[i] = tickstart[i-1] rotatedaround
384 ((0,0), (360/nummodticks));
385 tickend[i] = tickend[i-1] rotatedaround
386 ((0,0), (360/nummodticks));
387 draw tickstart[i]--tickend[i] withpen tickpen;
388 endfor;
389 );
390 addto modfigure also modtickpic;
391 fi
392 pickup modshapepen;
393 pair p[]; pair q[];
394 picture addition;
395 p[0] = (0,0.5in);
396 q[0] = (0,0.6in);
397 if (numbers = 1):
398 addition := image(label("0",q[0]));
399 addto modfigure also addition;
400 fi
401 for i=1 upto numpoints:
402 p[i] = p[i-1] rotatedaround ((0,0),-(360/numpoints));
403 q[i] = q[i-1] rotatedaround ((0,0),-(360/numpoints));
404 if (numbers = 1):
405 if (i <> numpoints):
406 addition := image(label(decimal i,q[i]));
407 addto modfigure also addition;
408 fi
409 fi
410 endfor
411 for i=0 upto numpoints:
412 if (i < numstar):
413 addition := image(%
414 draw p[i]--p[i+numstar];
415 draw p[i]--p[numpoints - numstar + i];
416 );
417 addto modfigure also addition;
418 elseif (i >= (numstar*2)):
419 addition := image(draw p[i-numstar]--p[i]);
420 addto modfigure also addition;
421 fi
422 endfor
423 addto modfigure also addition;
424 enddef;
425 def modfig(expr numpoints,numbers) =
426 picture modfigure;
427 if (modcircle = true):
428 modfigure := image(draw fullcircle scaled 1in withpen modcirclepen);
429 else:
430 modfigure := image();
431 fi
432 picture modtickpic;
433 if (modticks = true):
434 modtickpic := image(
435 pair tickstart[]; pair tickend[];
436 tickstart[0] = (0,0) shifted (0,0.5in);
437 tickend[0] = tickstart[0] shifted (0,ticklen);
438 draw tickstart[0]--tickend[0] withpen tickpen;
439 for i=1 upto (nummodticks-1):
440 tickstart[i] = tickstart[i-1] rotatedaround
441 ((0,0), (360/nummodticks));
442 tickend[i] = tickend[i-1] rotatedaround
443 ((0,0), (360/nummodticks));
444 draw tickstart[i]--tickend[i] withpen tickpen;
445 endfor;
446 );
447 addto modfigure also modtickpic;
448 fi
449 pair p[]; pair q[];
450 picture addition;
451 pickup modshapepen;
452 p[0] = (0,0.5in);
453 q[0] = (0,0.6in);
454 if (numbers = 1):
455 addition := image(label("0",q[0]));
456 addto modfigure also addition;
457 fi
458 for i=1 upto numpoints:
459 p[i] = p[i-1] rotatedaround ((0,0),-(360/numpoints));
460 q[i] = q[i-1] rotatedaround ((0,0),-(360/numpoints));
461 addition := image(draw p[i-1]--p[i]);
462 addto modfigure also addition;
463 if (numbers = 1):
464 if (i <> numpoints):
465 addition := image(label(decimal i,q[i]));
466 addto modfigure also addition;
467 fi
468 fi
469 endfor;
470 enddef;
471 % \end{macrocode}