
\newpage
\section{Transfers} % (fold)
\label{sec:transfers}
\subsection{From Lua to tkz-euclide or TikZ} % (fold)
\label{sub:fom_lua_to_tkz_euclide_or_tikz}

In this section, we'll explore how to transfer points, booleans, and numerical values.

\subsubsection{Points transfer} % (fold)
\label{ssub:points_transfer}
The necessary definitions and calculations are performed with the primitive \tkzcname{directlua} or inside the environment \tkzNameEnv{tkzelements}. Then, we execute the macro \Imacro{tkzGetNodes} which transforms the affixes of the table |z| into  \tkzname{Nodes}. Finally, we proceed with the drawing.

At present, the drawing program is either \TIKZ\ or \pkg{tkz-euclide}. However, you have the option to use another package for plotting. To do so, you'll need to create a macro similar to \tkzcname{tkzGetNodes}. Of course, this package must be capable of storing points like \TIKZ\ or \pkg{tkz-euclide}. 

\vspace*{1em}

\begin{mybox}
\begin{SVerbatim}
\def\tkzGetNodes{\directlua{%
   for K,V in pairs(z) do
      local n,sd,ft
      n = string.len(K)
      if n >1 then
      _,_,ft, sd = string.find( K , "(.+)(.)" )  
     if sd == "p" then   K=ft.."'" end 
     _,_,xft, xsd = string.find( ft , "(.+)(.)" ) 
     if xsd == "p" then  K=xft.."'".."'" end 
       end    
  tex.print("\\coordinate ("..K..") at ("..V.re..","..V.im..") ;\\\\")
end}
}
\end{SVerbatim}
\end{mybox}
See the section In-depth Study \ref{sec:in_depth_study} for an explanation of the previous code.

Point names can contain the underscore |_| and the macro \tkzcname{tkzGetNodes} allows to obtain names of nodes containing \tkzname{prime} or \tkzname{double prime}. (Refer to the next example)

\vspace{6pt}

\begin{tkzexample}[latex=7cm]
  \directlua{
     init_elements()
     z.o   = point: new (0,0)
     z.a_1 = point: new (2,1)
     z.a_2 = point: new (1,2)
     z.ap  = z.a_1 + z.a_2
     z.app = z.a_1 - z.a_2
  }

  \begin{center}
    \begin{tikzpicture}[ scale = 1.5]
       \tkzGetNodes
       \tkzDrawSegments(o,a_1 o,a_2 o,a' o,a'')
       \tkzDrawSegments[red](a_1,a' a_2,a')
       \tkzDrawSegments[blue](a_1,a'' a_2,a'')
       \tkzDrawPoints(a_1,a_2,a',o,a'')
       \tkzLabelPoints(o,a_1,a_2,a',a'')
    \end{tikzpicture}
  \end{center}
\end{tkzexample}

% subsubsection points_transfer (end)
% subsection fom_lua_to_tkz_euclide_or_tikz (end)

\subsubsection{Other transfers} % (fold)
\label{ssub:other_transfers}

Sometimes it's useful to transfer angle, length measurements or boolean. For this purpose, I have created the macro (refer to \ref{sub:transfer_from_lua_to_tex})  
\IEmacro{tkzUseLua(value)}

\begin{mybox}
  \begin{SVerbatim}
  \def\tkzUseLua#1{\directlua{tex.print(tostring(#1))}} 
\end{SVerbatim}
\end{mybox}

\directlua{
z.a = point:  new (4,2)
z.b = point:  new (1,1)
z.c = point:  new (2,2)
z.d = point:  new (5,1)
L.ab = line : new (z.a,z.b)
L.cd = line : new (z.c,z.d)
det = (z.b-z.a)^(z.d-z.c)
if det == 0 then bool = true 
  else bool = false
end
x = intersection (L.ab,L.cd)
}

The intersection of the two lines lies at
a point whose affix is: \tkzUseLua{x}

\vspace{6pt}
\begin{minipage}{0.5\textwidth}
\begin{SVerbatim}
\directlua{
 z.a = point:  new (4,2)
 z.b = point:  new (1,1)
 z.c = point:  new (2,2)
 z.d = point:  new (5,1)
 L.ab = line : new (z.a,z.b)
 L.cd = line : new (z.c,z.d)
  det = (z.b-z.a)^(z.d-z.c)
  if det == 0 then bool = true 
    else bool = false
  end
  x = intersection (L.ab,L.cd)
}
The intersection of the two lines lies at
    a point whose affix is:\tkzUseLua{x}
\begin{tikzpicture}
  \tkzGetNodes
 \tkzInit[xmin =0,ymin=0,xmax=5,ymax=3]
  \tkzGrid\tkzAxeX\tkzAxeY
   \tkzDrawPoints(a,...,d)
   \ifthenelse{\equal{\tkzUseLua{bool}}{true}}{
   \tkzDrawSegments[red](a,b c,d)}{%
   \tkzDrawSegments[blue](a,b c,d)}
    \tkzLabelPoints(a,...,d)
\end{tikzpicture}
\end{SVerbatim}
\end{minipage}
\begin{minipage}{0.5\textwidth}
\begin{center}
  \begin{tikzpicture}
   \tkzGetNodes
   \tkzInit[xmin =0,ymin=0,xmax=5,ymax=3]
   \tkzGrid\tkzAxeX\tkzAxeY
   \tkzDrawPoints(a,...,d)
   \ifthenelse{\equal{\tkzUseLua{bool}}{true}}{
   \tkzDrawSegments[red](a,b c,d)}{%
   \tkzDrawSegments[blue](a,b c,d)}
   \tkzLabelPoints(a,...,d)
  \end{tikzpicture}
\end{center}


  \end{minipage}
  
% subsubsection other_transfers (end)
\subsubsection{Example 1} % (fold)
\label{ssub:example_1}

In this example, it's necessary to transfer the function to the Lua part, then retrieve the curve point coordinates from \TeX.

The main tools used are a table and its methods (\Imeth{table}{insert},\Imeth{table}{concat}) and the \Igfct{lua}{load} function.

\begin{Verbatim}
\makeatletter\let\percentchar\@percentchar\makeatother
\directlua{
  function list (f,min,max,nb)
    local tbl = {}
    for x = min, max, (max - min) / nb do
        table.insert (tbl, ('(\percentchar f,\percentchar f)'):format (x, f (x)))
    end
   return table.concat (tbl)
  end
}
\def\plotcoords#1#2#3#4{%
\directlua{%
  f = load (([[
        return function (x)
            return (\percentchar s)
        end
    ]]):format ([[#1]]), nil, 't', math) ()
tex.print(list(f,#2,#3,#4))}
}
\begin{tikzpicture}
\tkzInit[xmin=1,xmax=3,ymin=0,ymax=2]
\tkzGrid 
\tkzDrawX[right=3pt,label={$x$}]
\tkzDrawY[above=3pt,label={$f(x) = \dfrac{1-\mathrm{e}^{-x^2}}{1+\mathrm{e}^{-x^2}}$}]
\draw[cyan,thick] plot coordinates {\plotcoords{(1-exp(-x^2))/(exp(-x^2)+1)}{-3}{3}{100}};
\end{tikzpicture}
\end{Verbatim}


\makeatletter\let\percentchar\@percentchar\makeatother
\directlua{
  function list (f,min,max,nb)
    local tbl = {}
    for x = min, max, (max - min) / nb do
        table.insert (tbl, ('(\percentchar f,\percentchar f)'):format (x, f (x)))
    end
   return table.concat (tbl)
  end
}

\def\plotcoords#1#2#3#4{%
\directlua{%
  f = load (([[
        return function (x)
            return (\percentchar s)
        end
    ]]):format ([[#1]]), nil, 't', math) ()
tex.print(list(f,#2,#3,#4))}
}

\begin{center}
  \begin{tikzpicture}
  \tkzInit[xmin=1,xmax=3,ymin=0,ymax=2]
  \tkzGrid 
  \tkzDrawX[right=3pt,label={$x$}]
  \tkzDrawY[above=3pt,label={$f(x) = \dfrac{1-\mathrm{e}^{-x^2}}{1+\mathrm{e}^{-x^2}}$}]
  \draw[cyan,thick] plot coordinates {\plotcoords{(1-exp(-x^2))/(exp(-x^2)+1)}{-3}{3}{100}};
  \end{tikzpicture}
\end{center}


% subsubsection example_1 (end)

\subsubsection{Example 2} % (fold)
\label{ssub:example_2}

This consists in passing a number (the number of sides) from \TeX\ to \code{Lua}. This is made easier by using the \Iprimitive{directlua} primitive. This example is based on a answer from egreg [\href{https://tex.stackexchange.com/questions/729009/how-can-these-regular-polygons-be-arranged-within-a-page/731503#731503}{egreg--tex.stackexchange.com}]

\begin{Verbatim}
\directlua{
  z.I      = point:    new (0,0)
  z.A      = point:    new (2,0)
}
\def\drawPolygon#1{
\directlua{
  RP.six   = regular_polygon : new (z.I,z.A,#1)
  RP.six : name ("P_")
  }  
\begin{tikzpicture}[scale=.5]
 \def\nb{\tkzUseLua{RP.six.nb}}
 \tkzGetNodes
 \tkzDrawCircles(I,A)
 \tkzDrawPolygon(P_1,P_...,P_\nb)
 \tkzDrawPoints[red](P_1,P_...,P_\nb)
\end{tikzpicture}
}
\foreach [count=\i] \n in {3, 4, ..., 10} {
  \makebox[0.2\textwidth]{%
    \begin{tabular}[t]{@{}c@{}}
      $n=\n$ \\[1ex]
      \drawPolygon{\n}
    \end{tabular}%
  }\ifnum\i=4 \\[2ex]\fi
}
\end{Verbatim}

\directlua{
  z.I      = point:    new (0,0)
  z.A      = point:    new (2,0)
}
\def\drawPolygon#1{
\directlua{
  RP.six   = regular_polygon : new (z.I,z.A,#1)
  RP.six : name ("P_")
  }  
\begin{tikzpicture}[scale=.5]
 \def\nb{\tkzUseLua{RP.six.nb}}
 \tkzGetNodes
 \tkzDrawCircles(I,A)
 \tkzDrawPolygon(P_1,P_...,P_\nb)
 \tkzDrawPoints[red](P_1,P_...,P_\nb)
\end{tikzpicture}
}
\foreach [count=\i] \n in {3, 4, ..., 10} {
  \makebox[0.2\textwidth]{%
    \begin{tabular}[t]{@{}c@{}}
      $n=\n$ \\[1ex]
      \drawPolygon{\n}
    \end{tabular}%
  }\ifnum\i=4 \\[2ex]\fi
}

% subsubsection example_2 (end)

\subsubsection{Example 3} % (fold)
\label{ssub:example_3}

This time, the transfer will be carried out using an external file. The following example is based on this one, but using a table.

\directlua{
 init_elements()
 z.a = point:new(1, 0) 
 z.b = point:new(3, 2) 
 z.c = point:new(0, 2)
 A,B,C =  parabola (z.a, z.b, z.c)

 function f(t0, t1, n)
  local out=assert(io.open("tmp.table","w"))
  local y
  for t = t0,t1,(t1-t0)/n  do   
   y = A*t^2+B*t +C
    out:write(t, " ", y, " i\string\n") 
  end
  out:close()
 end
 }
 
\begin{minipage}{0.5\textwidth}
\begin{SVerbatim}
\directlua{
  init_elements()
   z.a   = point: new (1,0) 
   z.b   = point: new (3,2) 
   z.c   = point: new (0,2)
   A,B,C =  parabola (z.a,z.b,z.c)

 function f(t0, t1, n)
  local out=assert(io.open("tmp.table","w"))
  local y
  for t = t0,t1,(t1-t0)/n  do   
   y = A*t^2+B*t +C
    out:write(t, " ", y, " i\string\n") 
  end
  out:close()
 end
 }
\begin{tikzpicture}
   \tkzGetNodes
   \tkzInit[xmin=-1,xmax=5,ymin=0,ymax=5]
   \tkzDrawX\tkzDrawY
   \tkzDrawPoints[red,size=2](a,b,c)
   \directlua{f(-1,3,100)}%
   \draw[domain=-1:3] plot[smooth] file {tmp.table};
\end{tikzpicture}
\end{SVerbatim}
\end{minipage}
\begin{minipage}{0.5\textwidth}
\begin{center}
  \begin{tikzpicture}
     \tkzGetNodes
     \tkzInit[xmin=-1,xmax=5,ymin=0,ymax=5]
     \tkzDrawX\tkzDrawY
     \tkzDrawPoints[red,size=2](a,b,c)
     \directlua{f(-1,3,100)}%
     \draw[domain=-1:3] plot[smooth] file {tmp.table};
  \end{tikzpicture}
\end{center}

\end{minipage}
% subsubsection example_3 (end)

\subsubsection{Example 4} % (fold)
\label{ssub:example_4}

The result is identical to the previous one.
\begin{SVerbatim}
\directlua{
   z.a   = point: new (1,0) 
   z.b   = point: new (3,2) 
   z.c   = point: new (0,2)
   A,B,C =  parabola (z.a,z.b,z.c)

 function f(t0, t1, n)
 local tbl = {}
 for t = t0,t1,(t1-t0)/n  do
     y = A*t^2+B*t +C
     table.insert (tbl, "("..t..","..y..")")
  end
  return table.concat (tbl)
end
}
\begin{tikzpicture}
   \tkzGetNodes
   \tkzDrawX\tkzDrawY
   \tkzDrawPoints[red,size=2pt](a,b,c)
   \draw[domain=-2:3,smooth] plot coordinates {\directlua{tex.print(f(-2,3,100))}};
\end{tikzpicture}
\end{SVerbatim}
% subsubsection example_4 (end)

\subsubsection{Example 5} % (fold)
\label{ssub:example_5}

\begin{SVerbatim}
\makeatletter\let\percentchar\@percentchar\makeatother
\directlua{
function cellx (start,step,n)
return start+step*(n-1)
end
}
\def\calcval#1#2{%
\directlua{
  f = load (([[
        return function (x)
            return (\percentchar s)
        end
    ]]):format ([[#1]]), nil, 't', math) ()
x = #2
tex.print(string.format("\percentchar.2f",f(x)))}
}
\def\fvalues(#1,#2,#3,#4) {%
\def\firstline{$x$}
    \foreach \i in {1,2,...,#4}{%
      \xdef\firstline{\firstline &  \tkzUseLua{cellx(#2,#3,\i)}}}
\def\secondline{$f(x)=#1$}
     \foreach \i in {1,2,...,#4}{%
      \xdef\secondline{\secondline &    
     \calcval{#1}{\tkzUseLua{cellx(#2,#3,\i)}}}}     
\begin{tabular}{l*{#4}c}
  \toprule
  \firstline  \\
  \secondline \\
  \bottomrule
  \end{tabular}
} 
\fvalues(x^2-3*x+1,-2,.25,8)
\vspace{12pt}

\end{SVerbatim}

\makeatletter\let\percentchar\@percentchar\makeatother
\directlua{
function cellx (start,step,n)
return start+step*(n-1)
end
}
\def\calcval#1#2{%
\directlua{
  f = load (([[
        return function (x)
            return (\percentchar s)
        end
    ]]):format ([[#1]]), nil, 't', math) ()
x = #2
tex.print(string.format("\percentchar.2f",f(x)))}
}
\def\fvalues(#1,#2,#3,#4) {%
\def\firstline{$x$}
    \foreach \i in {1,2,...,#4}{%
      \xdef\firstline{\firstline &  \tkzUseLua{cellx(#2,#3,\i)}}}
\def\secondline{$f(x)=#1$}
     \foreach \i in {1,2,...,#4}{%
      \xdef\secondline{\secondline &    
     \calcval{#1}{\tkzUseLua{cellx(#2,#3,\i)}}}}     
\begin{tabular}{l*{#4}c}
  \toprule
  \firstline  \\
  \secondline \\
  \bottomrule
  \end{tabular}
} 
\fvalues(x^2-3*x+1,-2,.25,8)
\vspace{12pt}

% subsubsection example_5 (end)
% section transfers (end)

\endinput