luametafun-function.tex /size: 8991 b    last modification: 2021-10-28 13:50
1% language=us runpath=texruns:manuals/luametafun
2
3\environment luametafun-style
4
5\startcomponent luametafun-function
6
7\startchapter[title={Function}]
8
9It is tempting to make helpers that can do a lot. However, that also means that
10we need to explain a lot. Instead it makes more sense to have specific helpers
11and just make another one when needed. Rendering functions falls into this
12category. At some point users will come up with specific cases that other users
13can use. Therefore, the solution presented here is not the ultimate answer. We
14start with a simple example:
15
16\startbuffer[1]
17\startMPcode{doublefun}
18    draw lmt_function [
19        xmin =  0, xmax = 20, xstep = .1,
20        ymin = -2, ymax =  2,
21
22        sx = 1mm, xsmall = 80, xlarge = 20,
23        sy = 4mm, ysmall = 40, ylarge =  4,
24
25        linewidth = .025mm, offset = .1mm,
26
27        code = "1.5 * math.sind (50 * x - 150)",
28    ]
29        xsized 8cm
30    ;
31\stopMPcode
32\stopbuffer
33
34\startplacefigure
35    \getbuffer[1]
36\stopplacefigure
37
38This image is defined as follows:
39
40\typebuffer[1][option=TEX]
41
42\startbuffer[2]
43\startMPcode{doublefun}
44    draw lmt_function [
45        xmin =  0, xmax = 20, xstep = .1,
46        ymin = -2, ymax =  2,
47
48        sx = 1mm, xsmall = 80, xlarge = 20,
49        sy = 4mm, ysmall = 40, ylarge =  4,
50
51        linewidth = .025mm, offset = .1mm,
52
53        xticks    = "bottom",
54        yticks    = "left",
55        xlabels   = "nolimits",
56        ylabels   = "yes",
57        code      = "1.5 * math.sind (50 * x - 150)",
58      % frame     = "ticks",
59        frame     = "sticks",
60        ycaption  = "\strut \rotate[rotation=90]{something vertical, using $\sin{x}$}",
61        xcaption  = "\strut something horizontal",
62        functions = {
63            [ xmin =  1.0, xmax =  7.0, close = true, fillcolor = "darkred" ],
64            [ xmin =  7.0, xmax = 12.0, close = true, fillcolor = "darkgreen" ],
65            [ xmin = 12.0, xmax = 19.0, close = true, fillcolor = "darkblue" ],
66            [
67                drawcolor = "darkyellow",
68                drawsize  = 2
69            ]
70        }
71    ]
72        xsized TextWidth
73    ;
74\stopMPcode
75\stopbuffer
76
77We can draw multiple functions in one go. The next sample split the drawing over
78a few ranges and is defined as follows; in \in {figure} [function:2] we see the
79result.
80
81\typebuffer[2][option=TEX]
82
83
84\startplacefigure[reference=function:2]
85    \getbuffer[2]
86\stopplacefigure
87
88Instead of the same function, we can draw different ones and when we use transparency
89we get nice results too.
90
91\startbuffer[3]
92\definecolor[MyColorR][r=.5,t=.5,a=1]
93\definecolor[MyColorG][g=.5,t=.5,a=1]
94\definecolor[MyColorB][b=.5,t=.5,a=1]
95
96\startMPcode{doublefun}
97    draw lmt_function [
98        xmin =  0, xmax = 20, xstep = .1,
99        ymin = -1, ymax =  1,
100
101        sx = 1mm, xsmall = 80, xlarge = 20,
102        sy = 4mm, ysmall = 40, ylarge =  4,
103
104        linewidth = .025mm, offset = .1mm,
105
106        functions = {
107            [
108                code      = "math.sind (50 * x - 150)",
109                close     = true,
110                fillcolor = "MyColorR"
111            ],
112            [
113                code      = "math.cosd (50 * x - 150)",
114                close     = true,
115                fillcolor = "MyColorB"
116            ]
117        },
118    ]
119        xsized TextWidth
120    ;
121\stopMPcode
122\stopbuffer
123
124\typebuffer[3][option=TEX]
125
126\startplacefigure[reference=function:3]
127    \getbuffer[3]
128\stopplacefigure
129
130It is important to choose a good step. In \in {figure} [function:4] we show 4
131variants and it is clear that in this case using straight line segments is better
132(or at least more efficient with small steps).
133
134\startbuffer[4a]
135\startMPcode{doublefun}
136    draw lmt_function [
137        xmin =  0, xmax = 10, xstep = .1,
138        ymin = -1, ymax =  1,
139
140        sx = 1mm, sy = 4mm,
141
142        linewidth = .025mm, offset = .1mm,
143
144        code  = "math.sind (50 * x^2 - 150)",
145        shape = "curve"
146    ]
147        xsized .45TextWidth
148    ;
149\stopMPcode
150\stopbuffer
151
152\startbuffer[4b]
153\startMPcode{doublefun}
154    draw lmt_function [
155        xmin =  0, xmax = 10, xstep = .01,
156        ymin = -1, ymax =  1,
157
158        sx = 1mm, sy = 4mm,
159
160        linewidth = .025mm, offset = .1mm,
161
162        code  = "math.sind (50 * x^2 - 150)",
163        shape = "curve"
164    ]
165        xsized .45TextWidth
166    ;
167\stopMPcode
168\stopbuffer
169
170\startbuffer[4c]
171\startMPcode{doublefun}
172    draw lmt_function [
173        xmin =  0, xmax = 10, xstep = .1,
174        ymin = -1, ymax =  1,
175
176        sx = 1mm, sy = 4mm,
177
178        linewidth = .025mm, offset = .1mm,
179
180        code  = "math.sind (50 * x^2 - 150)",
181        shape = "line"
182    ]
183        xsized .45TextWidth
184    ;
185\stopMPcode
186\stopbuffer
187
188\startbuffer[4d]
189\startMPcode{doublefun}
190    draw lmt_function [
191        xmin =  0, xmax = 10, xstep = .01,
192        ymin = -1, ymax =  1,
193
194        sx = 1mm, sy = 4mm,
195
196        linewidth = .025mm, offset = .1mm,
197
198        code  = "math.sind (50 * x^2 - 150)",
199        shape = "line"
200    ]
201        xsized .45TextWidth
202    ;
203\stopMPcode
204\stopbuffer
205
206\typebuffer[4a][option=TEX]
207
208\startplacefigure[reference=function:4]
209    \startcombination[2*2]
210        {\getbuffer[4a]} {\type {xstep=.10} and \type {shape="curve"}}
211        {\getbuffer[4b]} {\type {xstep=.01} and \type {shape="curve"}}
212        {\getbuffer[4c]} {\type {xstep=.10} and \type {shape="line"}}
213        {\getbuffer[4d]} {\type {xstep=.01} and \type {shape="line"}}
214    \stopcombination
215\stopplacefigure
216
217You can manipulate the axis (a bit) by tweaking the first and last ticks. In the
218case of \in {figure} [function:5] we also put the shape on top of the axis.
219
220\startbuffer[5]
221\startMPcode{doublefun}
222    draw lmt_function [
223        xfirst =  9, xlast = 21, ylarge = 2, ysmall = 1/5,
224        yfirst = -1, ylast =  1, xlarge = 2, xsmall = 1/4,
225
226        xmin = 10, xmax = 20, xstep = .25,
227        ymin = -1, ymax =  1,
228
229        drawcolor = "darkmagenta",
230        shape     = "steps",
231        code      = "0.5 * math.random(-2,2)",
232        linewidth = .025mm,
233        offset    = .1mm,
234        reverse   = true,
235    ]
236        xsized TextWidth
237    ;
238\stopMPcode
239\stopbuffer
240
241\typebuffer[5][option=TEX]
242
243\startplacefigure[reference=function:5]
244    \getbuffer[5]
245\stopplacefigure
246
247The whole repertoire of parameters (in case of doubt just check the source code as this
248kind of code is not that hard to follow) is:
249
250\starttabulate[|T|T|T|p|]
251\FL
252\BC name        \BC type    \BC default \BC comment \NC \NR
253\ML
254\NC sx          \NC numeric \NC 1mm    \NC horizontal scale factor \NC \NR
255\NC sy          \NC numeric \NC 1mm    \NC vertical scale factor \NC \NR
256\NC offset      \NC numeric \NC 0      \NC \NC \NR
257\NC xmin        \NC numeric \NC 1      \NC \NC \NR
258\NC xmax        \NC numeric \NC 1      \NC \NC \NR
259\NC xstep       \NC numeric \NC 1      \NC \NC \NR
260\NC xsmall      \NC numeric \NC        \NC optional step of small ticks \NC \NR
261\NC xlarge      \NC numeric \NC        \NC optional step of large ticks \NC \NR
262\NC xlabels     \NC string  \NC no     \NC \type {yes}, \type {no} or \type {nolimits} \NC \NR
263\NC xticks      \NC string  \NC bottom \NC possible locations are \type {top}, \type {middle} and \type {bottom} \NC \NR
264\NC xcaption    \NC string  \NC        \NC \NC \NR
265\NC ymin        \NC numeric \NC 1      \NC \NC \NR
266\NC ymax        \NC numeric \NC 1      \NC \NC \NR
267\NC ystep       \NC numeric \NC 1      \NC \NC \NR
268\NC ysmall      \NC numeric \NC        \NC optional step of small ticks \NC \NR
269\NC ylarge      \NC numeric \NC        \NC optional step of large ticks \NC \NR
270\NC xfirst      \NC numeric \NC        \NC left of \type {xmin} \NC \NR
271\NC xlast       \NC numeric \NC        \NC right of \type {xmax} \NC \NR
272\NC yfirst      \NC numeric \NC        \NC below \type {ymin} \NC \NR
273\NC ylast       \NC numeric \NC        \NC above \type {ymax} \NC \NR
274\NC ylabels     \NC string  \NC no     \NC \type {yes}, \type {no} or \type {nolimits} \NC \NR
275\NC yticks      \NC string  \NC left   \NC possible locations are \type {left}, \type {middle} and \type {right} \NC \NR
276\NC ycaption    \NC string  \NC        \NC \NC \NR
277\NC code        \NC string  \NC        \NC \NC \NR
278\NC close       \NC boolean \NC false  \NC \NC \NR
279\NC shape       \NC string  \NC curve  \NC or \type {line} \NC \NR
280\NC fillcolor   \NC string  \NC        \NC \NC \NR
281\NC drawsize    \NC numeric \NC 1      \NC \NC \NR
282\NC drawcolor   \NC string  \NC        \NC \NC \NR
283\NC frame       \NC string  \NC        \NC options are \type {yes}, \type {ticks} and \type {sticks} \NC \NR
284\NC linewidth   \NC numeric \NC .05mm  \NC \NC \NR
285\NC pointsymbol \NC string  \NC        \NC like type {dots} \NC \NR
286\NC pointsize   \NC numeric \NC 2      \NC \NC \NR
287\NC pointcolor  \NC string  \NC        \NC \NC \NR
288\NC xarrow      \NC string  \NC        \NC \NC \NR
289\NC yarrow      \NC string  \NC        \NC \NC \NR
290\NC reverse     \NC boolean \NC false  \NC when \type {true} draw the function between axis and labels \NC \NR
291\LL
292\stoptabulate
293
294\stopchapter
295
296\stopcomponent
297