Debian Codegraph @

texmf-dist/tex/latex/tkz-elements/tkz_elements_param_function.lua

raw
1 -- tkz_elements_param_function.lua
2 -- Copyright (c) 2026 Alain Matthes
3 -- SPDX-License-Identifier: LPPL-1.3c
4 -- Maintainer: Alain Matthes
5
6
7 -- Define a GLOBAL class: pfct
8 -- Parametric function: (x(t), y(t)) -> path() or file
9 -- Requires tkz-elements objects: path(), point(x,y)
10
11 pfct = pfct or {}
12 pfct.__index = pfct
13
14 -- ------------------------------------------------------------
15 -- helpers
16 -- ------------------------------------------------------------
17 local function is_finite_(v)
18 return (v == v) and v ~= math.huge and v ~= -math.huge
19 end
20
21 local function compile_1var_(expr, env, varname)
22 env = env or math
23 varname = varname or "t"
24 expr = tostring(expr or "0")
25
26 local chunk, err = load(
27 "return function(" .. varname .. ") return " .. expr .. " end",
28 nil, "t", env
29 )
30 if not chunk then
31 if tex and tex.error then
32 tex.error("tkz-elements: invalid parametric expression", { err or "compile error", expr })
33 end
34 return function(_) return 0/0 end
35 end
36 return chunk()
37 end
38
39 -- ------------------------------------------------------------
40 -- constructor
41 -- ------------------------------------------------------------
42 function pfct:new(exprx, expry, env)
43 local o = setmetatable({}, self)
44 o.exprx = exprx
45 o.expry = expry
46 o.env = env or math
47 o.fx, o.fy = nil, nil
48 o.sx = 1
49 o.sy = 1
50 o.mapfn = nil
51 return o
52 end
53
54 setmetatable(pfct, {
55 __call = function(_, exprx, expry, env)
56 return pfct:new(exprx, expry, env)
57 end
58 })
59 -- ------------------------------------------------------------
60 -- compile (lazy)
61 -- ------------------------------------------------------------
62 function pfct:compile()
63 if not self.fx then
64 if type(self.exprx) == "function" then
65 self.fx = self.exprx
66 else
67 self.fx = compile_1var_(self.exprx, self.env, "t")
68 end
69 end
70 if not self.fy then
71 if type(self.expry) == "function" then
72 self.fy = self.expry
73 else
74 self.fy = compile_1var_(self.expry, self.env, "t")
75 end
76 end
77 return self
78 end
79
80 function pfct:set_scale(sx, sy)
81 self.sx = tonumber(sx) or 1
82 self.sy = tonumber(sy) or 1
83 return self
84 end
85
86 function pfct:scale(sx, sy)
87 local o = self:copy()
88 return o:set_scale(sx, sy)
89 end
90
91 -- ------------------------------------------------------------
92 -- evaluation
93 -- ------------------------------------------------------------
94 function pfct:eval(t)
95 self:compile()
96
97 local okx, x = pcall(self.fx, t)
98 local oky, y = pcall(self.fy, t)
99
100 if not okx then x = 0/0 end
101 if not oky then y = 0/0 end
102
103 -- scale
104 x = (self.sx or 1) * x
105 y = (self.sy or 1) * y
106
107 -- map
108 if self.mapfn then
109 local ok, X, Y = pcall(self.mapfn, x, y, t, self)
110 if ok then
111 x, y = X, Y
112 else
113 x, y = 0/0, 0/0
114 end
115 end
116
117 return x, y
118 end
119
120 -- ------------------------------------------------------------
121 -- path
122 -- ------------------------------------------------------------
123 function pfct:path(tmin, tmax, n)
124 tmin = tonumber(tmin) or 0
125 tmax = tonumber(tmax) or 1
126 n = tonumber(n) or 200
127 if n <= 0 then n = 1 end
128
129 local p = path()
130 self:compile()
131
132 local step = (tmax - tmin) / n
133
134 for i = 0, n do
135 local t = tmin + i * step
136 local x, y = self:eval(t)
137 if is_finite_(x) and is_finite_(y) then
138 p:add_point(point(x, y))
139 end
140 end
141
142 return p
143 end
144
145 function pfct:set_map(fn)
146 if fn ~= nil and type(fn) ~= "function" then
147 error("pfct:set_map(fn): fn must be a function")
148 end
149 self.mapfn = fn
150 return self
151 end
152
153 function pfct:map(fn)
154 local o = self:copy()
155 return o:set_map(fn)
156 end
157 -- ------------------------------------------------------------
158 -- file (TikZ plot file)
159 -- ------------------------------------------------------------
160 function pfct:file(tmin, tmax, n, filename)
161 tmin = tonumber(tmin)
162 tmax = tonumber(tmax)
163 n = tonumber(n) or 200
164
165 local out = assert(io.open(filename, "w"))
166 local step = (tmax - tmin) / n
167
168 for i = 0, n do
169 local t = tmin + i * step
170 local x, y = self:eval(t)
171 if x == x and y == y then
172 out:write(x, " ", y, "\n")
173 end
174 end
175
176 out:close()
177 return filename
178 end
179
180 -- convenience: jobname-suffix.table
181 function pfct:jobfile(tmin, tmax, n, suffix)
182 local job = (tex and tex.jobname) or "job"
183 local fname = job .. "-" .. tostring(suffix or "param") .. ".table"
184 return self:file(tmin, tmax, n, fname)
185 end
186
187 -- ------------------------------------------------------------
188 -- scalar evaluations
189 -- ------------------------------------------------------------
190 function pfct:x(t)
191 local x = self:eval(t)
192 return x
193 end
194
195 function pfct:y(t)
196 local _, y = self:eval(t)
197 return y
198 end
199
200 -- ------------------------------------------------------------
201 -- point on parametric curve
202 -- ------------------------------------------------------------
203 function pfct:point(t)
204 return point(self:x(t), self:y(t))
205 end
206
207 -- alias naturel
208 pfct.at = pfct.point
209 return pfct