Debian Codegraph @

texmf-dist/tex/latex/tkz-grapheur/tkz-grapheur-lua.tex

raw
1 % tkz-grapheur-lua.tex
2 % Copyright 2026 Cédric Pierquet
3 % Released under the LaTeX Project Public License v1.3c or later, see http://www.latex-project.org/lppl.txt
4 % macros base [lua] de tkz-grapheur
5
6 %====COURBES IMPLICITES
7 \newlength\pflthickimpl
8 \tikzset{pflcourbeimplicit/.style={only marks,mark=*}}
9
10 \defKV[GraphiqueTikzImplicite]{%
11 CoeffPasAuto=\def\pflcoeffpasimpl{#1},step stretch=\def\pflcoeffpasimpl{#1},%
12 Pas=\def\pflpasimpl{#1},step=\def\pflpasimpl{#1},%
13 Couleur=\def\pflcouleurbimpl{#1},color=\def\pflcouleurbimpl{#1},%
14 Epaisseur=\setlength\pflthickimpl{#1},thick=\setlength\pflthickimpl{#1},%
15 Precision=\def\pflprecisiimpl{#1},prec=\def\pflprecisiimpl{#1},%
16 Tolerance=\def\pfltoleranceimplic{#1},tolerance=\def\pfltoleranceimplic{#1}
17 }
18 \setKVdefault[GraphiqueTikzImplicite]{%
19 CoeffPasAuto=1,step stretch=1,%
20 Pas=auto,step=auto,%
21 Couleur=black,color=black,%
22 Epaisseur=0.2875pt,thick=0.2875pt,%
23 Precision=8,precision=8,%
24 Tolerance=1e-10,tolerance=1e-10,%
25 lua=true
26 }
27
28 \NewDocumentCommand\TracerCourbeImplicite{ O{} m }{%with jfbu part of code for xint version !
29 % #1 = clés (Pas, Couleur, etc)
30 % #2 = expression f(x,y) (= 0 implicite)
31 \restoreKV[GraphiqueTikzImplicite]%
32 \setKV[GraphiqueTikzImplicite]{#1}%
33 \ifboolKV[GraphiqueTikzImplicite]{lua}%
34 {%
35 \ifluatex
36 \IfStrEq{\pflpasimpl}{auto}%
37 {%
38 \pgfmathsetmacro{\pfl@pasx}{\pgfmath@tonumber{\pflthickimpl} * 0.5 * (\pflcoeffpasimpl) / \pgfmath@tonumber{\pgf@xx}}%
39 \pgfmathsetmacro{\pfl@pasy}{\pgfmath@tonumber{\pflthickimpl} * 0.5 * (\pflcoeffpasimpl) / \pgfmath@tonumber{\pgf@yy}}%
40 }%
41 {%
42 \def\pfl@pasx{\pflpasimpl}%
43 \def\pfl@pasy{\pflpasimpl}%
44 }%
45 \edef\tmplistepts{\directlua{tex.sprint(pfl.marching_squares("\luaescapestring{#2}",\pflxmin,\pflxmax,\pflymin,\pflymax,\pfl@pasx,\pfl@pasy,\pfltoleranceimplic))}}%
46 \begin{scope}
47 \clip (\pflxmin,\pflymin) rectangle (\pflxmax,\pflymax);
48 \draw[\pflcouleurbimpl] plot[mark size=\pflthickimpl,pflcourbeimplicit] coordinates {\tmplistepts};
49 \end{scope}%
50 \else
51 \PackageWarning{tkz-grapheur}{.: LuaLaTeX not detected, \TracerCourbeImplicite\space with [lua=false] key only available :.}
52 \fi
53 }%
54 {%
55 % open a scope limiting group
56 \begingroup
57 % configure xint to work with less digits
58 \xintSetDigits*{\pflprecisiimpl}%
59 % définition fonction xint à 2 variables
60 \xintdeffloatfunc pflimplicitefct(x,y) = #2 ;%
61 %calcul auto du pas
62 \IfStrEq{\pflpasimpl}{auto}%
63 {%
64 \pgfmathsetmacro{\pflpasximplcalc}{\pgfmath@tonumber{\pflthickimpl} * \pflcoeffpasimpl * 1.414 / \pgfmath@tonumber{\pgf@xx}}%
65 \pgfmathsetmacro{\pflpasyimplcalc}{\pgfmath@tonumber{\pflthickimpl} * \pflcoeffpasimpl * 1.414 / \pgfmath@tonumber{\pgf@yy}}%
66 }%
67 {%
68 \def\pflpasximplcalc{\pflpasimpl}%
69 \def\pflpasyimplcalc{\pflpasimpl}%
70 }%
71 \def\tmplisteptsh{}%
72 \def\tmplisteptsv{}%
73 \def\tmplisteptsp{}%
74 % double boucle marching squares
75 \edef\tmpxcoords{\xintfloateval{\pflxmin..[\pflpasximplcalc]..\pflxmax}}%
76 \edef\tmpycoords{\xintfloateval{\pflymin..[\pflpasyimplcalc]..\pflymax}}%
77 % attention ensemble de définition ??...
78 \def\tmpj{0}%
79 % precompute values of function on left vertical border
80 \xintFor ##2 in {\tmpycoords}\do{%
81 \expandafter\edef\csname tmp_value_\tmpj\endcsname{\xintfloateval{pflimplicitefct(\pflxmin,##2)}}%
82 \edef\tmpj{\the\numexpr\tmpj+1}%
83 \xintifForLast{% one more
84 \expandafter\edef\csname tmp_value_\tmpj\endcsname{\xintfloateval{pflimplicitefct(\pflxmin,##2+\pflpasyimplcalc)}}%
85 }{}%
86 }%
87 \xintFor ##1 in {\tmpxcoords}\do{%
88 \edef\tmpC{\csname tmp_value_0\endcsname}%
89 \def\tmpj{0}%
90 \xintFor ##2 in {\tmpycoords}\do{%
91 % évaluer les 3 coins et préparer en même temps la prochaine colonne pré-calculée
92 \let\tmpA\tmpC
93 \edef\tmpB{\xintfloateval{pflimplicitefct(##1+\pflpasximplcalc,##2)}}%
94 % mettre à jour pour la prochaine fois, oui c'est rusé
95 \expandafter\let\csname tmp_value_\tmpj\endcsname\tmpB
96 \edef\tmpj{\the\numexpr\tmpj+1}%
97 % là on est encore avec la colonne en cours, déjà calculée
98 \edef\tmpC{\csname tmp_value_\tmpj\endcsname}%
99 % changement de signe horizontal
100 \xintifboolexpr{sgn(\tmpA)*sgn(\tmpB) < 0}%
101 {%
102 \xdef\tmplisteptsh{\tmplisteptsh (\xintfloateval{##1-(\tmpA)*\pflpasximplcalc/(\tmpB-\tmpA)},##2)}%
103 }{}%
104 % changement de signe vertical
105 \xintifboolexpr{sgn(\tmpA)*sgn(\tmpC) < 0}%
106 {%
107 \xdef\tmplisteptsv{\tmplisteptsv (##1,\xintfloateval{##2-(\tmpA)*\pflpasyimplcalc/(\tmpC-\tmpA)})}%
108 }{}%
109 % si pas de changement de signe détecté → test zéro exact
110 \xintifboolexpr{(sgn(\tmpA)*sgn(\tmpB) >= 0) && (sgn(\tmpA)*sgn(\tmpC) >= 0) && (abs(\tmpA) < \pfltoleranceimplic)}%
111 {%
112 \xdef\tmplisteptsp{\tmplisteptsp (##1,##2)}%
113 }{}%
114 % le dernier coup il faut en calculer un de plus
115 \xintifForLast{%
116 \expandafter\edef\csname tmp_value_\tmpj\endcsname{\xintfloateval{pflimplicitefct(##1+\pflpasximplcalc,##2+\pflpasyimplcalc)}}%
117 }{}%
118 }%
119 }%
120 \begin{scope}
121 \clip (\pflxmin,\pflymin) rectangle (\pflxmax,\pflymax) ;
122 \IfEq{\tmplisteptsp}{}{}%
123 {%
124 \draw[\pflcouleurbimpl] plot[pflcourbeimplicit] coordinates {\tmplisteptsp} ;%
125 }%
126 \IfEq{\tmplisteptsh}{}{}%
127 {%
128 \draw[\pflcouleurbimpl] plot[pflcourbeimplicit] coordinates {\tmplisteptsh} ;%
129 }%
130 \IfEq{\tmplisteptsv}{}{}%
131 {%
132 \draw[\pflcouleurbimpl] plot[pflcourbeimplicit] coordinates {\tmplisteptsv} ;%
133 }%
134 \end{scope}%
135 \endgroup%
136 }%
137 }
138 \NewCommandCopy\PlotImplicitCurve\TracerCourbeImplicite
139
140 \defKV[GraphiqueTikzNiveau]{%
141 Couleur=\def\pflniveaucouleur{#1},%
142 Couleurs=\def\pflniveaucouleurs{#1},%
143 Pas=\def\pflniveaupas{#1},%
144 Epaisseur=\setlength\pflthickimpl{#1}%
145 }
146 \setKVdefault[GraphiqueTikzNiveau]{%
147 Couleur=black,%
148 Couleurs={},%
149 Pas=auto,%
150 Epaisseur=0.2875pt%
151 }
152
153 % \NewDocumentCommand\TracerLignesNiveau{ O{} m m }{%
154 % % #1 = clés
155 % % #2 = expression f(x,y)
156 % \restoreKV[GraphiqueTikzNiveau]%
157 % \setKV[GraphiqueTikzNiveau]{#1}%
158 % % Boucle sur les valeurs
159 % \foreach \elt [count=\i from 1] in {#3}{%
160 % \IfStrEq{\pflniveaucouleurs}{}%
161 % {%
162 % \edef\pflcoulcourante{\pflniveaucouleur}%
163 % }%
164 % {%
165 % \setsepchar{,}%
166 % \readlist*\pfllistecouleurs{\pflniveaucouleurs}%
167 % %Couleur cyclique si moins de couleurs que de niveaux
168 % \pgfmathtruncatemacro{\pflcolidx}{mod(\i-1,\pfllistecouleurslen)+1}%
169 % \itemtomacro\pfllistecouleurs[\pflcolidx]{\pflcoulcourante}%
170 % }%
171 % \TracerCourbeImplicite[lua,Couleur=\pflcoulcourante,Pas=\pflniveaupas,Epaisseur=\pflthickimpl]{#2 - (\elt)}%
172 % }
173 % }
174 % \NewCommandCopy\PlotLevelCurves\TracerLignesNiveau
175
176 \NewDocumentCommand\TracerLignesNiveau{ O{} m m }{%
177 % #1 = clés
178 % #2 = expression f(x,y)
179 % #3 = liste des valeurs k
180 \restoreKV[GraphiqueTikzNiveau]%
181 \setKV[GraphiqueTikzNiveau]{#1}%
182 % calcul du pas
183 \IfStrEq{\pflniveaupas}{auto}{%
184 \pgfmathsetmacro{\pfl@pasx}{\pgfmath@tonumber{\pflthickimpl}*0.5/\pgfmath@tonumber{\pgf@xx}}%
185 \pgfmathsetmacro{\pfl@pasy}{\pgfmath@tonumber{\pflthickimpl}*0.5/\pgfmath@tonumber{\pgf@yy}}%
186 }{%
187 \pgfmathsetmacro{\pfl@pasx}{\pflniveaupas}%
188 \pgfmathsetmacro{\pfl@pasy}{\pflniveaupas}%
189 }%
190 % Appel Lua unique → grille calculée une seule fois !
191 \directlua{
192 pfl._niveau_pts = pfl.level_curves(
193 "\luaescapestring{#2}",
194 "\luaescapestring{#3}",
195 \pflxmin, \pflxmax, \pflymin, \pflymax,
196 \pfl@pasx, \pfl@pasy
197 )
198 }%
199 % Boucle tracé uniquement
200 \foreach \elt [count=\i from 1] in {#3}{%
201 \IfStrEq{\pflniveaucouleurs}{}%
202 {\edef\pflcoulcourante{\pflniveaucouleur}}%
203 {%
204 \setsepchar{,}%
205 \readlist*\pfllistecouleurs{\pflniveaucouleurs}%
206 \itemcycltomacro\pfllistecouleurs[\i]{\pflcoulcourante}%
207 %\pgfmathtruncatemacro{\pflcolidx}{mod(\i-1,\pfllistecouleurslen)+1}%
208 %\itemtomacro\pfllistecouleurs[\pflcolidx]{\pflcoulcourante}%
209 }%
210 \edef\pflkpts{\directlua{tex.sprint(pfl._niveau_pts[\i] or "")}}%
211 \begin{scope}
212 \clip (\pflxmin,\pflymin) rectangle (\pflxmax,\pflymax);
213 \draw[\pflcoulcourante]
214 plot[mark size=\pflthickimpl,pflcourbeimplicit]
215 coordinates {\pflkpts};
216 \end{scope}%
217 }%
218 }%
219 \NewCommandCopy\PlotLevelCurves\TracerLignesNiveau
220
221 \ifluatex
222 \RequirePackage{luacode}
223 % \begin{luacode*}
224 % pfl = pfl or {}
225
226 % function pfl.expr_to_func(expr_str)
227 % -- Traduction expression LaTeX → Lua
228 % -- texio.write_nl("=== expr reçue : " .. expr_str)
229 % local e = expr_str
230 % e = e:gsub("%^", "^")
231 % e = e:gsub("sin%(", "math.sin(")
232 % e = e:gsub("cos%(", "math.cos(")
233 % e = e:gsub("tan%(", "math.tan(")
234 % e = e:gsub("exp%(", "math.exp(")
235 % e = e:gsub("sqrt%(","math.sqrt(")
236 % e = e:gsub("log%(", "math.log(")
237 % e = e:gsub("abs%(", "math.abs(")
238 % e = e:gsub("pi", tostring(math.pi))
239 % e = e:gsub("%*%*", "^")
240 % -- Compilation dynamique
241 % -- texio.write_nl("=== expr traduite : " .. e)
242 % local fstr = "local x,y = ...; return " .. e
243 % -- texio.write_nl("=== fstr : " .. fstr)
244 % local f, err = load(fstr)
245 % if not f then
246 % texio.write_nl("! pfl.expr_to_func : " .. tostring(err))
247 % return nil
248 % end
249 % return f
250 % end
251
252 % function pfl.marching_squares(expr_str, xmin, xmax, ymin, ymax, pasx, pasy, eps)
253 % eps = eps or 1e-12
254 % local f = pfl.expr_to_func(expr_str)
255 % if not f then return "" end
256
257 % local ni = math.floor((xmax - xmin) / pasx)
258 % local nj = math.floor((ymax - ymin) / pasy)
259
260 % -- Phase 1 : tableau complet F[i][j]
261 % local F = {}
262 % local xs = {}
263 % local ys = {}
264 % for i = 0, ni do
265 % xs[i] = xmin + i * pasx
266 % F[i] = {}
267 % end
268 % for j = 0, nj do
269 % ys[j] = ymin + j * pasy
270 % end
271 % for i = 0, ni do
272 % for j = 0, nj do
273 % F[i][j] = f(xs[i], ys[j])
274 % end
275 % end
276
277 % -- Phase 2 : marching squares
278 % local pts = {}
279 % for i = 0, ni - 1 do
280 % for j = 0, nj - 1 do
281 % local A = F[i ][j ]
282 % local B = F[i+1][j ]
283 % local C = F[i ][j+1]
284 % local D = F[i+1][j+1]
285 % local xi = xs[i]
286 % local xip = xs[i+1]
287 % local yj = ys[j]
288 % local yjp = ys[j+1]
289 % -- Arête A→B
290 % if A * B < 0 then
291 % pts[#pts+1] = string.format("(%.3f,%.3g)",
292 % xi - A * pasx / (B - A), yj)
293 % end
294 % -- Arête A→C
295 % if A * C < 0 then
296 % pts[#pts+1] = string.format("(%.3f,%.3g)",
297 % xi, yj - A * pasy / (C - A))
298 % end
299 % -- Arête C→D
300 % -- if C * D < 0 then
301 % -- pts[#pts+1] = string.format("(%.6f,%.6g)",
302 % -- xi - C * pasx / (D - C), yjp)
303 % -- end
304 % -- Arête B→D
305 % -- if B * D < 0 then
306 % -- pts[#pts+1] = string.format("(%.6f,%.6g)",
307 % -- xip, yj - B * pasy / (D - B))
308 % -- end
309 % -- Zéro exact
310 % if A*B >= 0 and A*C >= 0 and math.abs(A) < eps then
311 % pts[#pts+1] = string.format("(%.6f,%.6g)", xi, yj)
312 % end
313 % end
314 % end
315 % local result = table.concat(pts, " ")
316 % if result == "" then
317 % return string.format("(%.6f,%.6f)", xmin - 1, ymin - 1)
318 % end
319 % return result
320 % end
321 % \end{luacode*}
322 \begin{luacode*}
323 pfl = pfl or {}
324
325 function pfl.expr_to_func(expr_str)
326 local e = expr_str
327 e = e:gsub("%^", "^")
328 e = e:gsub("sin%(", "math.sin(")
329 e = e:gsub("cos%(", "math.cos(")
330 e = e:gsub("tan%(", "math.tan(")
331 e = e:gsub("exp%(", "math.exp(")
332 e = e:gsub("sqrt%(","math.sqrt(")
333 e = e:gsub("log%(", "math.log(")
334 e = e:gsub("abs%(", "math.abs(")
335 e = e:gsub("pi", tostring(math.pi))
336 e = e:gsub("%*%*", "^")
337 local fstr = "local x,y = ...; return " .. e
338 local f, err = load(fstr)
339 if not f then
340 texio.write_nl("! pfl.expr_to_func : " .. tostring(err))
341 return nil
342 end
343 return f
344 end
345
346 function pfl.marching_squares(expr_str, xmin, xmax, ymin, ymax, pasx, pasy, eps)
347 eps = eps or 1e-12
348 local f = pfl.expr_to_func(expr_str)
349 if not f then return "" end
350 local ni = math.floor((xmax - xmin) / pasx)
351 local nj = math.floor((ymax - ymin) / pasy)
352 local F, xs, ys = {}, {}, {}
353 for i = 0, ni do xs[i] = xmin + i*pasx ; F[i] = {} end
354 for j = 0, nj do ys[j] = ymin + j*pasy end
355 for i = 0, ni do
356 for j = 0, nj do F[i][j] = f(xs[i], ys[j]) end
357 end
358 local pts = {}
359 for i = 0, ni-1 do
360 for j = 0, nj-1 do
361 local A,B,C = F[i][j],F[i+1][j],F[i][j+1]
362 local xi,yj = xs[i],ys[j]
363 if A*B < 0 then
364 pts[#pts+1] = string.format("(%.3f,%.3g)",
365 xi-A*pasx/(B-A), yj)
366 end
367 if A*C < 0 then
368 pts[#pts+1] = string.format("(%.3f,%.3g)",
369 xi, yj-A*pasy/(C-A))
370 end
371 if A*B >= 0 and A*C >= 0 and math.abs(A) < eps then
372 pts[#pts+1] = string.format("(%.6f,%.6g)", xi, yj)
373 end
374 end
375 end
376 local result = table.concat(pts, " ")
377 if result == "" then
378 return string.format("(%.6f,%.6f)", xmin-1, ymin-1)
379 end
380 return result
381 end
382
383 function pfl.level_curves(expr_str, valeurs_str, xmin, xmax, ymin, ymax, pasx, pasy, eps)
384 eps = eps or 1e-12
385 local f = pfl.expr_to_func(expr_str)
386 if not f then return {} end
387 local ni = math.floor((xmax - xmin) / pasx)
388 local nj = math.floor((ymax - ymin) / pasy)
389 local F, xs, ys = {}, {}, {}
390 for i = 0, ni do xs[i] = xmin + i*pasx ; F[i] = {} end
391 for j = 0, nj do ys[j] = ymin + j*pasy end
392 for i = 0, ni do
393 for j = 0, nj do F[i][j] = f(xs[i], ys[j]) end
394 end
395 local valeurs = {}
396 for k in valeurs_str:gmatch("[^,]+") do
397 valeurs[#valeurs+1] = tonumber(k)
398 end
399 local resultats = {}
400 for _, k in ipairs(valeurs) do
401 local pts = {}
402 for i = 0, ni-1 do
403 for j = 0, nj-1 do
404 local A,B,C = F[i][j]-k,F[i+1][j]-k,F[i][j+1]-k
405 local xi,yj = xs[i],ys[j]
406 if A*B < 0 then
407 pts[#pts+1] = string.format("(%.3f,%.3g)",
408 xi-A*pasx/(B-A), yj)
409 end
410 if A*C < 0 then
411 pts[#pts+1] = string.format("(%.3f,%.3g)",
412 xi, yj-A*pasy/(C-A))
413 end
414 if A*B >= 0 and A*C >= 0 and math.abs(A) < eps then
415 pts[#pts+1] = string.format("(%.6f,%.6g)", xi, yj)
416 end
417 end
418 end
419 local result = table.concat(pts, " ")
420 if result == "" then
421 result = string.format("(%.6f,%.6f)", xmin-1, ymin-1)
422 end
423 resultats[#resultats+1] = result
424 end
425 return resultats
426 end
427 \end{luacode*}
428 \fi
429
430 \endinput