summaryrefslogtreecommitdiff
path: root/tex/context/base/mkiv/meta-imp-experiments.mkxl
blob: 644a8fff031d2cc60e9c4a8269893734f5dc8ac9 (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
%D \module
%D   [       file=meta-imp-experiments,
%D        version=2020.03.18,
%D          title=\METAPOST\ Graphics,
%D       subtitle=Experimental Graphics,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

%D This library implements some experimental functionality that eventually
%D might end up someplace else.

\startluacode

    -- I decided to play with this kind of graphics after I read an article in a Dutch
    -- popular science journal. It was an article about virusus in the mids of the time
    -- when covid raged, so it also was a distraction.

    -- This is a typical case of first prototyping it in Lua using the 'context' command
    -- to generate MetaPost code and when that worked okay do it the following way. The
    -- way data is passed is a bit messy.

    do

        local function sum(t)
            local n = 0
            for i=1,#t do
                n = n + t[i]
            end
            return n
        end

        local function checked(data)
            if data then
                local n = #data
                for i=1,n do
                    data[i][i] = 0
                end
                for i=1,n do
                    local di = data[i]
                    for j=i+1,n do
                        local dj = data[j]
                        local dji = dj[i]
                        if not dji then
                            dji = 0
                            dj[i] = 0
                        end
                        di[j] = dji
                    end
                end
                return data
            else
                return { }
            end
        end

        local data, list = nil, nil

        function mp.lmt_overlap_prepare()
            data = checked(metapost.getparameter { "data" })
            list = { }
            for i=1,#data do
                list[i] = sum(data[i])
            end
        end

        function mp.lmt_overlap_reset()
            data = nil
            list = nil
        end

        function mp.lmt_overlap_n()
            return #list
        end

        function mp.lmt_overlap_data(i, j)
            if j then
                return data[i][j]
            else
                return list[i]
            end
        end

        local injectstring = mp.inject.string

        function mp.lmt_overlap_text(i, j)
            injectstring(data[i][j] or "")
        end

        function mp.lmt_overlap_label(i)
            local labels = metapost.getparameter { "labels" }
            injectstring(labels and labels[i] or "")
        end

        function mp.lmt_overlap_color(i)
            local colors = metapost.getparameter { "colors" }
            injectstring(colors and colors[i] or "darkgray")
        end

        function mp.lmt_overlap_total()
            return sum(list)
        end

    end
\stopluacode

\startMPextensions

presetparameters "overlap" [
    options     = "paths,lines",
    gap         = 4,
    subgap      = 2,
    offset      = 8,
    color       = "darkgray",
    alternative = "circular",
    colors      = {
        "darkred",
        "darkgreen",
        "darkblue",
        "darkyellow",
        "darkmagenta",
        "darkcyan"
    },
] ;

def lmt_overlap = applyparameters "overlap" "lmt_do_overlap" enddef ;

vardef lmt_do_overlap_circular =

    astep  := 360 / steps ;

    p := fullcircle scaled steps ;
    r := origin -- (2*steps,0) ;

    start := 0 ;
    stop  := 0 ;
    for i=1 upto n:
        stop  := start + lua.mp.lmt_overlap_data(i) ;
        first := start ;
        last  := stop ;
        for j=1 upto n:
            if i <> j :
                last := first + lua.mp.lmt_overlap_data(i,j) ;
                a := p intersectionpoint (r rotated (first * astep + 0.1)) ; % the 0.1 is somehow needed, why
                b := p intersectionpoint (r rotated (last  * astep - 0.1)) ; % the 0.1 is somehow needed, why
                qq[i][j] := (p cutafter b) cutbefore a ;
                first := last + subgap ;
            fi ;
        endfor ;
        start := stop + gap + (n - 1) * subgap ;
    endfor ;

    if hasoption "options" "paths" :

        for i=1 upto n :
            for j=1 upto n :
                if i <> j :
                    q := qq[i][j] ;
                    freelabeloffset := getparameter "offset" ;
                    freelabel(lua.mp.lmt_overlap_text(i,j), point .5 along q, origin) ;
                    if i < j :
                        s := qq[j][i] ;
                        a := point length(q) of q ;
                        b := point 0 of s ;
                        c := point length(s) of s ;
                        d := point 0 of q ;
                        q := q & a .. controls origin and origin .. b & s & c .. controls origin and origin .. d -- cycle ;
                        fill q withcolor lua.mp.lmt_overlap_color(i) withtransparency (1,.8) ;
                    fi ;
                fi ;
            endfor ;
        endfor ;

    fi ;

    if hasoption "options" "lines" :

        start := 0 ;
        stop  := 0 ;
        for i=1 upto n:
            stop := start + lua.mp.lmt_overlap_data(i) + (n - 2) * subgap ;
            a := p intersectionpoint (r rotated (start * astep)) ;
            b := p intersectionpoint (r rotated (stop  * astep)) ;
            q := (p cutbefore a) cutafter b ;
            freelabeloffset := getparameterdefault "textoffset" (4 * getparameter "offset") ;
            freelabel(lua.mp.lmt_overlap_label(i), point .5 along q, origin) ;
            draw q withcolor white withpen pencircle scaled 5 ;
            draw q withcolor getparameter "color" withpen pencircle scaled 5 ;
            start := stop + gap + subgap ;
        endfor ;

    fi ;

enddef ;

vardef lmt_do_overlap_linear =
    astep  := 1 ; % 1.25

    p := origin -- (astep * steps,0) ;
    r := origin -- (0,astep * steps) ;

    start := 0 ;
    stop  := 0 ;
    for i=1 upto n:
        stop  := start + lua.mp.lmt_overlap_data(i) ;
        first := start ;
        last  := stop ;
        for j=1 upto n:
            if i <> j :
                last := first + lua.mp.lmt_overlap_data(i,j) ;
                qq[i][j] := (first * astep,0) -- (last  * astep,0) ;
                first := last + subgap ;
            fi ;
        endfor ;
        start := stop + gap + (n - 1) * subgap ;
    endfor ;

    if hasoption "options" "paths" :

        for i=1 upto n :
            for j=1 upto n :
                if i < j :
                    qq[i][j] := qq[i][j] { up } .. { down } qq[j][i] { up } .. { down} cycle ;
                    fill qq[i][j] withcolor lua.mp.lmt_overlap_color(i) withtransparency (1,.8) ;
                fi ;
            endfor ;
        endfor ;

        for i=1 upto n :
            for j=1 upto n :
                if i < j :
                    t := thelabel(lua.mp.lmt_overlap_text(i,j), (center topboundary qq[i][j]) ) ;
                    fill boundingbox t enlarged (ExHeight/2) withcolor white ;
                    draw t ;
                fi ;
            endfor ;
        endfor ;

    fi ;

    if hasoption "options" "lines" :

        start := 0 ;
        stop  := 0 ;
        for i=1 upto n:
            stop := start + lua.mp.lmt_overlap_data(i) + (n - 2) * subgap ;
            q := (start * astep,0) -- (stop  * astep,0) ;
            freelabeloffset := getparameterdefault "textoffset" (4 * getparameter "offset") ;
            label.bot(lua.mp.lmt_overlap_label(i), (point .5 along q) shifted (0,- freelabeloffset/4)) ;
            draw q withcolor white withpen pencircle scaled 5 ;
            draw q withcolor getparameter "color" withpen pencircle scaled 5 ;
            start := stop + gap + subgap ;
        endfor ;

    fi ;

enddef ;

vardef lmt_do_overlap =
    image (

        pushparameters "overlap" ;

            save p, q, r, s, qq, a, b, c, d, t, n, gap, subgap, steps, astep, start, stop, first, last ;
            path p, q, r, s, qq[][] ;
            pair a, b, c, d ;
            picture t ;
            numeric n, gap, subgap, steps, astep, start, stop, first, last ;
            save freelabeloffset; freelabeloffset := 8 ;
            interim linecap := butt;
            interim linejoin := squared;

            lua.mp.lmt_overlap_prepare() ;

                n      := lua.mp.lmt_overlap_n();
                gap    := getparameter "gap" ;
                subgap := getparameter "subgap" ;
                steps  := lua.mp.lmt_overlap_total() + (n * gap) + n * (n - 1) * subgap ;

                if ((getparameter "alternative") = "linear") or ((getparameter "alternative") = "line") :
                    lmt_do_overlap_linear ;
                else :
                    lmt_do_overlap_circular ;
                fi ;

            lua.mp.lmt_overlap_reset() ;

        popparameters ;
    )
enddef ;

\stopMPextensions

\continueifinputfile{meta-imp-experiments.mkxl}

% \useMPlibrary[experiments]

\usemodule[article-basic]

\starttext

% todo: datafile: { data = { }, labels = { } }

%   % data   = {
%   %     {  0, 10, 30, 10, 20 },
%   %     { 10,  0, 10, 20, 10 },
%   %     { 30, 10,  0,  5, 15 },
%   %     { 10, 20,  5,  0,  5 },
%   %     { 20, 10, 15,  5,  0 }
%   % },
%   % data = {
%   %     {  0 },
%   %     { 10,  0 },
%   %     { 30, 10,  0 },
%   %     { 10, 20,  5,  0 },
%   %     { 20, 10, 15,  5,  0 }
%   % },
%     data = {
%         { },
%         { 10 },
%         { 30, 10 },
%         { 10, 20,  5 },
%         { 20, 10, 15,  5 }
%     },

\startbuffer
\startMPcode{doublefun}
    draw lmt_overlap [
        alternative = "circular",
        data        = { { }, { 10 }, { 30, 10 }, { 10, 20, 5 }, { 20, 10, 15, 5 } },
        labels      = { "one", "two", "three", "four", "five" }
    ] ;
\stopMPcode
\stopbuffer

\startlinecorrection[blank]
    \getbuffer
\stoplinecorrection

\startbuffer
\startMPcode{doublefun}
    draw lmt_overlap [
        alternative = "linear",
        data        = { { }, { 10 }, { 30, 10 }, { 10, 20, 5 }, { 20, 10, 15, 5 } },
        labels      = { "one", "two", "three", "four", "five" }
    ] ;
\stopMPcode
\stopbuffer

\startlinecorrection[blank]
    \getbuffer
\stoplinecorrection

\stoptext