meta-fun.lua /size: 1581 b    last modification: 2023-12-21 09:44
1if not modules then modules = { } end modules ['meta-fun'] = {
2    version   = 1.001,
3    comment   = "companion to meta-fun.mkiv",
4    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
5    copyright = "PRAGMA ADE / ConTeXt Development Team",
6    license   = "see context related readme files",
7}
8
9-- very experimental, actually a joke ... see metafun manual for usage
10
11local format, load, type = string.format, load, type
12
13local context    = context
14local metapost   = metapost
15
16local metafun    = metapost.metafun or { }
17metapost.metafun = metafun
18
19function metafun.topath(t,connector)
20    context("(")
21    if #t > 0 then
22        if not connector then
23            connector = ".."
24        end
25        for i=1,#t do
26            if i > 1 then
27                context(connector)
28            end
29            local ti = t[i]
30            if type(ti) == "string" then
31                context(ti)
32            else
33                context("(%F,%F)",ti.x or ti[1] or 0,ti.y or ti[2] or 0)
34            end
35        end
36    else
37        context("origin")
38    end
39    context(")")
40end
41
42function metafun.interpolate(f,b,e,s,c)
43    local done = false
44    context("(")
45    local d = load(format("return function(x) return %s end",f))
46    if d then
47        d = d()
48        if not c then
49            c = "..."
50        end
51        for i=b,e,(e-b)/s do
52            if done then
53                context(c)
54            else
55                done = true
56            end
57            context("(%F,%F)",i,d(i))
58        end
59    end
60    if not done then
61        context("origin")
62    end
63    context(")")
64end
65