summaryrefslogtreecommitdiff
path: root/doc/context/sources/general/manuals/luametafun/luametafun-contour.tex
blob: 11eca8b7adc1a17bab715e2323529ea906cbdbbb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
% language=us

\environment luametafun-style

\startcomponent luametafun-contour

\startchapter[title={Contour}]

This feature started out as experiment triggered by a request on the mailing
list. In the end it was a nice exploration of what is possible with a bit of
\LUA. In a sense it is more subsystem than a simple \METAPOST\ macro because
quite some \LUA\ code is involved and more might be used in the future. It's part
of the fun.

A contour is a line through equivalent values $z$ that result from applying a
function to two variables $x$ and $y$. There is quite a bit of analysis needed
to get these lines. In \METAFUN\ we currently support three methods for generating
a colorful background and three for putting lines on top:

One solution is to use the the isolines and isobands methods are described on the
marching squares page of wikipedia:

\starttyping
https://en.wikipedia.org/wiki/Marching_squares
\stoptyping

This method is relative efficient as we don't do much optimization, simply
because it takes time and the gain is not that much relevant. Because we support
filling of multiple curves in one go, we get efficient paths anyway without side
effects that normally can occur from many small paths alongside. In these days of
multi megabyte movies and sound clips a request of making a \PDF\ file small is kind
of strange anyway. In practice the penalty is not that large.

As background we can use a bitmap. This method is also quite efficient because we
use indexed colors which results in a very good compression. We use a simple
mapping on a range of values.

A third method is derived from the one that is distributed as \CCODE\ source
file at:

\starttyping
https://physiology.arizona.edu/people/secomb/contours
https://github.com/secomb/GreensV4
\stoptyping

We can create a background image, which uses a sequence of closed curves
\footnote {I have to figure out how to improve it a bit so that multiple path
don't get connected.}. It can also provide two variants of lines around the
contours (we tag them shape and shade). It's all a matter of taste. In the
meantime I managed to optimize the code a bit and I suppose that when I buy a new
computer (the code was developed on an 8 year old machine) performance is
probably acceptable.

In order of useability you can think of isoband (band) with isolines (cell),
bitmap (bitmap) with isolines (cell) and finally shapes (shape) with edges
(edge). But let's start with a couple of examples.

\startbuffer[1]
\startMPcode{doublefun}
    draw lmt_contour [
        xmin =  0, xmax = 4*pi, xstep = .05,
        ymin = -6, ymax = 6,    ystep = .05,

        levels     = 7,
        height     = 5cm,
        preamble   = "local sin, cos = math.sin, math.cos",
        function   = "cos(x) + sin(y)",
        background = "bitmap",
        foreground = "edge",
        linewidth  = 1/2,
        cache      = true,
    ] ;
\stopMPcode
\stopbuffer

\startplacefigure[reference=contour:1]
    \getbuffer[1]
\stopplacefigure

\typebuffer[1][option=TEX]

In \in {figure} [contour:1] we see the result. There is a in this case black and
white image generated and on top of that we see lines. The step determines the
resolution of the image. In practice using a bitmap is quite okay and also rather
efficient: we use an indexed colorspace and, as already was mentioned, because
the number of colors is limited such an image compresses well. A different
rendering is seen in \in {figure} [contour:2] where we use the shape method for
the background. That method creates outlines but is much slower, and when you use
a high resolution (small step) it can take quite a while to identify the shapes.
This is why we set the cache flag.

\startbuffer[2]
\startMPcode{doublefun}
    draw lmt_contour [
        xmin =  0, xmax = 4*pi, xstep = .10,
        ymin = -6, ymax = 6,    ystep = .10,

        levels     = 7,
        preamble   = "local sin, cos = math.sin, math.cos",
        function   = "cos(x) - sin(y)",
        background = "shape",
        foreground = "shape",
        linewidth  = 1/2,
        cache      = true,
    ] ;
\stopMPcode
\stopbuffer

\typebuffer[2][option=TEX]

\startplacefigure[reference=contour:2]
    \getbuffer[2]
\stopplacefigure

We mentioned colorspace but haven't seen any color yet, so let's set some in \in
{figure} [contour:3]. Two variants are shown: a background \type {shape} with
foreground \type {shape} and a background \type {bitmap} with a foreground \type
{edge}. The bitmap renders quite fast, definitely when we compare with the shape,
while the quality is as good at this size.

\startbuffer[3a]
\startMPcode{doublefun}
    draw lmt_contour [
        xmin = -10, xmax = 10, xstep =  .1,
        ymin = -10, ymax = 10, ystep =  .1,

        levels     = 10,
        height     = 7cm,
        color      = "shade({1/2,1/2,0},{0,0,1/2})",
        function   = "x^2 + y^2",
        background = "shape",
        foreground = "shape",
        linewidth  = 1/2,
        cache      = true,
    ] xsized .45TextWidth ;
\stopMPcode
\stopbuffer

\startbuffer[3b]
\startMPcode{doublefun}
    draw lmt_contour [
        xmin = -10, xmax = 10, xstep =  .1,
        ymin = -10, ymax = 10, ystep =  .1,

        levels     = 10,
        height     = 7cm,
        color      = "shade({1/2,0,0},{0,0,1/2})",
        function   = "x^2 + y^2",
        background = "bitmap",
        foreground = "edge",
        linewidth  = 1/2,
        cache      = true,
    ] xsized .45TextWidth ;
\stopMPcode
\stopbuffer

\typebuffer[3a][option=TEX]

\startplacefigure[reference=contour:3]
    \startcombination
        {\getbuffer[3a]} {\bf shape}
        {\getbuffer[3b]} {\bf bitmap}
    \stopcombination
\stopplacefigure

We use the \type {doublefun} instance because we need to be sure that we don't
run into issues with scaled numbers, the default model in \METAPOST. The
function that gets passed is {\em not} using \METAPOST\ but \LUA, so basically
you can do very complex things. Here we directly pass code, but you can for
instance also do this:

\starttyping[option=TEX]
\startluacode
    function document.MyContourA(x,y)
        return x^2 + y^2
    end
\stopluacode
\stoptyping

and then \type {function = "document.MyContourA(x,y)"}. As long as the function
returns a valid number we're okay. When you pass code directly you can use the
\type {preamble} key to set local shortcuts. In the previous examples we took
\type {sin} and \type {cos} from the math library but you can also roll out your
own functions and|/|or use the more elaborate \type {xmath} library. The color
parameter is also a function, one that returns one or three arguments. In the
next example we use \type {lin} to calculate a fraction of the current level and
total number of levels.

\startbuffer[4a]
\startMPcode{doublefun}
    draw lmt_contour [
        xmin = -3, xmax = 3, xstep =  .01,
        ymin = -1, ymax = 1, ystep =  .01,

        levels     = 10,
        default    = .5,
        height     = 5cm,
        function   = "x^2 + y^2 + x + y/2",
        color      = "lin(l), 0, 1/2",
        background = "bitmap"
        foreground = "none",
        cache      = true,
    ] xsized TextWidth ;
\stopMPcode
\stopbuffer

\typebuffer[4a][option=TEX]

\startplacefigure[reference=contour:4a]
    \getbuffer[4a]
\stopplacefigure

Instead of a bitmap we can use an isoband, which boils down to a set of tiny
shapes that make up a bigger one. This is shown in \in {figure} [contour:4b].

\startbuffer[4b]
\startMPcode{doublefun}
    draw lmt_contour [
        xmin = -3, xmax = 3, xstep =  .01,
        ymin = -1, ymax = 1, ystep =  .01,

        levels     = 10,
        default    = .5,
        height     = 5cm,
        function   = "x^2 + y^2 + x + y/2",
        color      = "lin(l), 1/2, 0",
        background = "band",
        foreground = "none",
        cache      = true,
    ] xsized TextWidth ;
\stopMPcode
\stopbuffer

\typebuffer[4b][option=TEX]

\startplacefigure[reference=contour:4b]
    \getbuffer[4b]
\stopplacefigure

You can draw several functions and see where they overlap:

\startbuffer[5]
\startMPcode{doublefun}
    draw lmt_contour [
        xmin = -pi, xmax = 4*pi, xstep = .1,
        ymin = -3,  ymax = 3,    ystep = .1,

        range      = { -.1, .1 },
        preamble   = "local sin, cos = math.sin, math.cos",
        functions  = {
            "sin(x) + sin(y)", "sin(x) + cos(y)",
            "cos(x) + sin(y)", "cos(x) + cos(y)"
        },
        background = "bitmap",
        linecolor  = "black",
        linewidth  = 1/10,
        color      = "shade({1,1,0},{0,0,1})"
        cache      = true,
    ] xsized TextWidth ;
\stopMPcode
\stopbuffer

\typebuffer[5][option=TEX]

\startplacefigure[reference=contour:5]
    \getbuffer[5]
\stopplacefigure

The range determines the $z$ value(s) that we take into account. You can also
pass a list of colors to be used. In \in {figure} [contour:6] this is
demonstrated. There we also show a variant foreground \type {cell}, which uses a
bit different method for calculating the edges. \footnote {This a bit of a
playground: more variants might show up in due time.}

\startbuffer[6]
\startMPcode{doublefun}
    draw lmt_contour [
        xmin = -2*pi, xmax =  2*pi, xstep = .01,
        ymin = -3,    ymax =  3,    ystep = .01,

        range      = { -.1, .1 },
        preamble   = "local sin, cos = math.sin, math.cos",
        functions  = { "sin(x) + sin(y)", "sin(x) + cos(y)" },
        background = "bitmap",
        foreground = "cell",
        linecolor  = "white",
        linewidth  = 1/10,
        colors     = { (1/2,1/2,1/2), red, green, blue }
        level      = 3,
        linewidth  = 6,
        cache      = true,
    ] xsized TextWidth ;
\stopMPcode
\stopbuffer

\typebuffer[6][option=TEX]

Here the number of levels depends on the number of functions as each can overlap
with another; for instance the outcome of two functions can overlap or not which
means 3 cases, and with a value not being seen that gives 4 different cases.

\startplacefigure[reference=contour:6]
    \getbuffer[6]
\stopplacefigure

\startbuffer[7]
\startMPcode{doublefun}
    draw lmt_contour [
        xmin = -2*pi, xmax =  2*pi, xstep = .01,
        ymin = -3,    ymax =  3,    ystep = .01,

        range      = { -.1, .1 },
        preamble   = "local sin, cos = math.sin, math.cos",
        functions  = {
            "sin(x) + sin(y)",
            "sin(x) + cos(y)",
            "cos(x) + sin(y)",
            "cos(x) + cos(y)"
        },
        background = "bitmap",
        foreground = "none",
        level      = 3,
        color      = "shade({2/3,0,0},{2/3,1,2/3})"
        cache      = true,
    ] xsized TextWidth ;
\stopMPcode
\stopbuffer

\typebuffer[7][option=TEX]

Of course one can wonder how useful showing many functions but it can give nice
pictures, as shown in \in {figure} [contour:7].

\startplacefigure[reference=contour:7]
    \getbuffer[7]
\stopplacefigure

\startbuffer[8]
\startMPcode{doublefun}
    draw lmt_contour [
        xmin = -2*pi, xmax =  2*pi, xstep = .01,
        ymin = -3,    ymax =  3,    ystep = .01,

        range      = { -.3, .3 },
        preamble   = "local sin, cos = math.sin, math.cos",
        functions  = {
            "sin(x) + sin(y)",
            "sin(x) + cos(y)",
            "cos(x) + sin(y)",
            "cos(x) + cos(y)"
        },
        background = "bitmap",
        foreground = "none",
        level      = 3,
        color      = "shade({1,0,0},{0,1,0})"
        cache      = true,
    ] xsized TextWidth ;
\stopMPcode
\stopbuffer

\typebuffer[8][option=TEX]

We can enlargen the window, which is demonstrated in \in {figure} [contour:8]. I
suppose that such images only make sense in educational settings.

\startplacefigure[reference=contour:8]
    \getbuffer[8]
\stopplacefigure

% \startbuffer[9a]
% \startMPcode{doublefun}
%     draw lmt_contour [
%         xmin = -10, xmax = 10, xstep = 1,
%         ymin = -10, ymax = 10, ystep = 1,
%         function  = "math.random(1,3)", levels    = 3,
%         linecolor = "white",            linewidth = 1/10,
%         width     = .3TextWidth,        legend    = "none",
%         color     = "shade({1/2,1/2,0},{0,0,1})"
%
%         background = "bitmap", foreground = "edge",
%     ] ;
% \stopMPcode
% \stopbuffer
%
% \typebuffer[9a][option=TEX]
%
% \startbuffer[9b]
% \startMPcode{doublefun}
%     draw lmt_contour [
%         xmin = -10, xmax = 10, xstep = 1,
%         ymin = -10, ymax = 10, ystep = 1,
%         function  = "math.random(1,3)", levels    = 3,
%         linecolor = "white",            linewidth = 1/10,
%         width     = .3TextWidth,        legend    = "none",
%         color     = "shade({1/2,1/2,0},{0,0,1/2})"
%
%         background = "bitmap", foreground = "cell",
%     ] ;
% \stopMPcode
% \stopbuffer
%
% \startbuffer[9c]
% \startMPcode{doublefun}
%     draw lmt_contour [
%         xmin = -10, xmax = 10, xstep = 1,
%         ymin = -10, ymax = 10, ystep = 1,
%         function  = "math.random(1,3)", levels    = 3,
%         linecolor = "white",            linewidth = 1/10,
%         width     = .3TextWidth,        legend    = "none",
%         color     = "shade({1/2,1/2,0},{0,0,1})"
%
%         background = "bitmap", foreground = "none",
%     ] ;
% \stopMPcode
% \stopbuffer
%
% \startbuffer[9d]
% \startMPcode{doublefun}
%     draw lmt_contour [
%         xmin = -10, xmax = 10, xstep = 1,
%         ymin = -10, ymax = 10, ystep = 1,
%         function  = "math.random(1,3)", levels    = 3,
%         linecolor = "white",            linewidth = 1/10,
%         width     = .3TextWidth,        legend    = "none",
%         color     = "shade({1/2,1/2,0},{0,0,1})"
%
%         background = "shape", foreground = "shape",
%     ] ;
% \stopMPcode
% \stopbuffer
%
% \startbuffer[9e]
% \startMPcode{doublefun}
%     draw lmt_contour [
%         xmin = -10, xmax = 10, xstep = 1,
%         ymin = -10, ymax = 10, ystep = 1,
%         function  = "math.random(1,3)", levels    = 3,
%         linecolor = "white",            linewidth = 1/10,
%         width     = .3TextWidth,        legend    = "none",
%         color     = "shade({1/2,1/2,0},{0,0,1})"
%
%         background = "shape", foreground = "edge",
%     ] ;
% \stopMPcode
% \stopbuffer
%
% \startbuffer[9f]
% \startMPcode{doublefun}
%     draw lmt_contour [
%         xmin = -10, xmax = 10, xstep = 1,
%         ymin = -10, ymax = 10, ystep = 1,
%         function  = "math.random(1,3)", levels    = 3,
%         linecolor = "white",            linewidth = 1/10,
%         width     = .3TextWidth,        legend    = "none",
%         color     = "shade({1/2,1/2,0},{0,0,1})"
%
%         background = "shape", foreground = "none",
%     ] ;
% \stopMPcode
% \stopbuffer
%
% \startbuffer[9g]
% \startMPcode{doublefun}
%     draw lmt_contour [
%         xmin = -10, xmax = 10, xstep = 1,
%         ymin = -10, ymax = 10, ystep = 1,
%         function  = "math.random(1,3)", levels    = 3,
%         linecolor = "white",            linewidth = 1/10,
%         width     = .3TextWidth,        legend    = "none",
%         color     = "shade({1/2,1/2,0},{0,0,1})"
%
%         background = "band", foreground = "edge",
%     ] ;
% \stopMPcode
% \stopbuffer
%
% \startbuffer[9h]
% \startMPcode{doublefun}
%     draw lmt_contour [
%         xmin = -10, xmax = 10, xstep = 1,
%         ymin = -10, ymax = 10, ystep = 1,
%         function  = "math.random(1,3)", levels    = 3,
%         linecolor = "white",            linewidth = 1/10,
%         width     = .3TextWidth,        legend    = "none",
%         color     = "shade({1/2,1/2,0},{0,0,1})"
%
%         background = "band", foreground = "cell",
%     ] ;
% \stopMPcode
% \stopbuffer
%
% \startbuffer[9i]
% \startMPcode{doublefun}
%     draw lmt_contour [
%         xmin = -10, xmax = 10, xstep = 1,
%         ymin = -10, ymax = 10, ystep = 1,
%         function  = "math.random(1,3)", levels    = 3,
%         linecolor = "white",            linewidth = 1/10,
%         width     = .3TextWidth,        legend    = "none",
%         color     = "shade({1/2,1/2,0},{0,0,1})"
%
%         background = "band", foreground = "none",
%     ] ;
% \stopMPcode
% \stopbuffer
%
% In \in {figure} [contour:9] we see that using the shape option doesn't work out
% too well here, which again demonstrates that using the bitmap method is not that
% bad. In that example we use random numbers, just to show the erratic behavior. In
% \in {figure} [contour:10] a more sane image is used. The band and bitmap examples
% are generated quite fast so no caching is used there. We only show one definition:
%
% \typebuffer[9i][option=TEX]
%
% \startplacefigure[reference=contour:9]
%     \startcombination[3*3]
%         {\pushrandomseed\setrandomseed{1}\getbuffer[9a]\poprandomseed} {\bf bitmap edge}
%         {\pushrandomseed\setrandomseed{1}\getbuffer[9b]\poprandomseed} {\bf bitmap cell}
%         {\pushrandomseed\setrandomseed{1}\getbuffer[9c]\poprandomseed} {\bf bitmap none}
%         {\pushrandomseed\setrandomseed{1}\getbuffer[9d]\poprandomseed} {\bf shape  shape}
%         {\pushrandomseed\setrandomseed{1}\getbuffer[9e]\poprandomseed} {\bf shape  edge}
%         {\pushrandomseed\setrandomseed{1}\getbuffer[9f]\poprandomseed} {\bf shape  none}
%         {\pushrandomseed\setrandomseed{1}\getbuffer[9g]\poprandomseed} {\bf band   edge}
%         {\pushrandomseed\setrandomseed{1}\getbuffer[9h]\poprandomseed} {\bf band   cell}
%         {\pushrandomseed\setrandomseed{1}\getbuffer[9i]\poprandomseed} {\bf band   none}
%     \stopcombination
% \stopplacefigure

In \in {figure} [contour:10] we see different combinations of backgrounds (in color)
and foregrounds (edges) in action.

\startbuffer[10a]
\startMPcode{doublefun}
    draw lmt_contour [
        xmin =  0, xmax = 4*pi, xstep = 0,
        ymin = -6, ymax = 6,    ystep = 0,

        levels = 5, legend = false, linewidth  = 1/2,

        preamble   = "local sin, cos = math.sin, math.cos",
        function   = "cos(x) - sin(y)",
        color      = "shade({1/2,0,0},{0,0,1/2})",

        background = "bitmap", foreground = "edge",
    ] xsized .3TextWidth ;
\stopMPcode
\stopbuffer

\startbuffer[10b]
\startMPcode{doublefun}
    draw lmt_contour [
        xmin =  0, xmax = 4*pi, xstep = 0,
        ymin = -6, ymax = 6,    ystep = 0,

        levels = 5, legend = false, linewidth  = 1/2,

        preamble   = "local sin, cos = math.sin, math.cos",
        function   = "cos(x) - sin(y)",
        color      = "shade({1/2,0,0},{0,0,1/2})",

        background = "bitmap", foreground = "cell",
    ] xsized .3TextWidth ;
\stopMPcode
\stopbuffer

\startbuffer[10c]
\startMPcode{doublefun}
    draw lmt_contour [
        xmin =  0, xmax = 4*pi, xstep = 0,
        ymin = -6, ymax = 6,    ystep = 0,

        levels = 5, legend = false, linewidth  = 1/2,

        preamble   = "local sin, cos = math.sin, math.cos",
        function   = "cos(x) - sin(y)",
        color      = "shade({1/2,0,0},{0,0,1/2})",

        background = "bitmap", foreground = "none",
    ] xsized .3TextWidth ;
\stopMPcode
\stopbuffer

\startbuffer[10d]
\startMPcode{doublefun}
    draw lmt_contour [
        xmin =  0, xmax = 4*pi, xstep = 0,
        ymin = -6, ymax = 6,    ystep = 0,

        levels = 5, legend = false, linewidth  = 1/2,

        preamble   = "local sin, cos = math.sin, math.cos",
        function   = "cos(x) - sin(y)",
        color      = "shade({1/2,0,0},{0,0,1/2})",

        background = "shape",  foreground = "shape", cache = true,
    ] xsized .3TextWidth ;
\stopMPcode
\stopbuffer

\startbuffer[10e]
\startMPcode{doublefun}
    draw lmt_contour [
        xmin =  0, xmax = 4*pi, xstep = 0,
        ymin = -6, ymax = 6,    ystep = 0,

        levels = 5, legend = false, linewidth  = 1/2,

        preamble   = "local sin, cos = math.sin, math.cos",
        function   = "cos(x) - sin(y)",
        color      = "shade({1/2,0,0},{0,0,1/2})",

        background = "shape",  foreground = "edge", cache = true,
    ] xsized .3TextWidth ;
\stopMPcode
\stopbuffer

\startbuffer[10f]
\startMPcode{doublefun}
    draw lmt_contour [
        xmin =  0, xmax = 4*pi, xstep = 0,
        ymin = -6, ymax = 6,    ystep = 0,

        levels = 5, legend = false, linewidth  = 1/2,

        preamble   = "local sin, cos = math.sin, math.cos",
        function   = "cos(x) - sin(y)",
        color      = "shade({1/2,0,0},{0,0,1/2})",

        background = "shape",  foreground = "none", cache = true,
    ] xsized .3TextWidth ;
\stopMPcode
\stopbuffer

\startbuffer[10g]
\startMPcode{doublefun}
    draw lmt_contour [
        xmin =  0, xmax = 4*pi, xstep = 0,
        ymin = -6, ymax = 6,    ystep = 0,

        levels = 5, legend = false, linewidth  = 1/2,

        preamble   = "local sin, cos = math.sin, math.cos",
        function   = "cos(x) - sin(y)",
        color      = "shade({1/2,0,0},{0,0,1/2})",

        background = "band",   foreground = "edge",
    ] xsized .3TextWidth ;
\stopMPcode
\stopbuffer

\startbuffer[10h]
\startMPcode{doublefun}
    draw lmt_contour [
        xmin =  0, xmax = 4*pi, xstep = 0,
        ymin = -6, ymax = 6,    ystep = 0,

        levels = 5, legend = false, linewidth  = 1/2,

        preamble   = "local sin, cos = math.sin, math.cos",
        function   = "cos(x) - sin(y)",
        color      = "shade({1/2,0,0},{0,0,1/2})",

        background = "band",   foreground = "cell",
    ] xsized .3TextWidth ;
\stopMPcode
\stopbuffer

\startbuffer[10i]
\startMPcode{doublefun}
    draw lmt_contour [
        xmin =  0, xmax = 4*pi, xstep = 0,
        ymin = -6, ymax = 6,    ystep = 0,

        levels = 5, legend = false, linewidth  = 1/2,

        preamble   = "local sin, cos = math.sin, math.cos",
        function   = "cos(x) - sin(y)",
        color      = "shade({1/2,0,0},{0,0,1/2})",

        background = "band",   foreground = "none",
    ] xsized .3TextWidth ;
\stopMPcode
\stopbuffer

\typebuffer[10b][option=TEX]

% \page

There are quite some settings. Some deal with the background, some with the
foreground and quite some deal with the legend.

\starttabulate[|T|T|T|p|]
\FL
\BC name             \BC type    \BC default    \BC comment \NC \NR
\ML
\NC xmin             \NC numeric \NC 0          \NC needs to be set \NC \NR
\NC xmax             \NC numeric \NC 0          \NC needs to be set \NC \NR
\NC ymin             \NC numeric \NC 0          \NC needs to be set \NC \NR
\NC ymax             \NC numeric \NC 0          \NC needs to be set \NC \NR
\NC xstep            \NC numeric \NC 0          \NC auto 1/200 when zero \NC \NR
\NC ystep            \NC numeric \NC 0          \NC auto 1/200 when zero \NC \NR
\NC checkresult      \NC boolean \NC false      \NC checks for overflow and NaN \NC \NR
\NC defaultnan       \NC numeric \NC 0          \NC the value to be used when NaN \NC \NR
\NC defaultinf       \NC numeric \NC 0          \NC the value to be used when overflow \NC \NR
\ML
\NC levels           \NC numeric \NC 10         \NC number of different levels to show \NC \NR
\NC level            \NC numeric \NC            \NC only show this level (foreground) \NC \NR
\ML
\NC preamble         \NC string  \NC            \NC shortcuts \NC \NR
\NC function         \NC string  \NC x + y      \NC the result z value \NC \NR
\NC functions        \NC list    \NC            \NC multiple functions (overlapping levels) \NC \NR
\NC color            \NC string  \NC lin(l)     \NC the result color value for level l (1 or 3 values) \NC \NR
\NC colors           \NC numeric \NC            \NC used when set \NC \NR
\ML
\NC background       \NC string  \NC bitmap     \NC band, bitmap, shape \NC \NR
\NC foreground       \NC string  \NC auto       \NC cell, edge, shape auto \NC \NR
\ML
\NC linewidth        \NC numeric \NC .25        \NC \NC \NR
%NC backgroundcolor  \NC string  \NC black      \NC \NC \NR
\NC linecolor        \NC string  \NC gray       \NC \NC \NR
\ML
\NC width            \NC numeric \NC 0          \NC automatic when zero \NC \NR
\NC height           \NC numeric \NC 0          \NC automatic when zero \NC \NR
\ML
\NC trace            \NC boolean \NC false      \NC \NC \NR
\ML
\NC legend           \NC string  \NC all        \NC x y z function range all \NC \NR
\NC legendheight     \NC numeric \NC LineHeight \NC \NC \NR
\NC legendwidth      \NC numeric \NC LineHeight \NC \NC \NR
\NC legendgap        \NC numeric \NC 0          \NC \NC \NR
\NC legenddistance   \NC numeric \NC EmWidth    \NC \NC \NR
\NC textdistance     \NC numeric \NC 2EmWidth/3 \NC \NC \NR
\NC functiondistance \NC numeric \NC ExHeight   \NC \NC \NR
\NC functionstyle    \NC string  \NC            \NC  \CONTEXT\ style name \NC \NR
\NC xformat          \NC string  \NC @0.2N      \NC number format template \NC \NR
\NC yformat          \NC string  \NC @0.2N      \NC number format template \NC \NR
\NC zformat          \NC string  \NC @0.2N      \NC number format template \NC \NR
\NC xstyle           \NC string  \NC            \NC \CONTEXT\ style name \NC \NR
\NC ystyle           \NC string  \NC            \NC \CONTEXT\ style name \NC \NR
\NC zstyle           \NC string  \NC            \NC \CONTEXT\ style name \NC \NR
\ML
\NC axisdistance     \NC numeric \NC ExHeight   \NC \NC \NR
\NC axislinewidth    \NC numeric \NC .25        \NC \NC \NR
\NC axisoffset       \NC numeric \NC ExHeight/4 \NC \NC \NR
\NC axiscolor        \NC string  \NC black      \NC \NC \NR
\NC ticklength       \NC numeric \NC ExHeight   \NC \NC \NR
\ML
\NC xtick            \NC numeric \NC 5          \NC \NC \NR
\NC ytick            \NC numeric \NC 5          \NC \NC \NR
\NC xlabel           \NC numeric \NC 5          \NC \NC \NR
\NC ylabel           \NC numeric \NC 5          \NC \NC \NR
\LL
\stoptabulate

\startplacefigure[reference=contour:10]
    \startcombination[3*3]
        {\getbuffer[10a]} {\bf bitmap edge}
        {\getbuffer[10b]} {\bf bitmap cell}
        {\getbuffer[10c]} {\bf bitmap none}
        {\getbuffer[10d]} {\bf shape  shape}
        {\getbuffer[10e]} {\bf shape  edge}
        {\getbuffer[10f]} {\bf shape  none}
        {\getbuffer[10g]} {\bf band   edge}
        {\getbuffer[10h]} {\bf band   cell}
        {\getbuffer[10i]} {\bf band   none}
    \stopcombination
\stopplacefigure

\stopchapter

\stopcomponent