\section{Graphics Methods}

Polygonal lines, curves, paths, points, and labels can be created.

\subsection{Polygonal Lines}

\subsubsection{Dpolyline}

\textbf{A polygonal line is a list (table) of connected components, and a connected component is a list (table) of complex numbers that represent the affixes of the points}. For example, the instruction:
\begin{Luacode}
local Z = luadraw.cpx.Z
local L = { {Z(-4,0), Z(0,2), Z(1,3)}, {Z(0,0), Z(4,-2), Z(1,-1)} }
\end{Luacode}
creates a polygonal line with two components in a variable \emph{L}.

\paragraph{Drawing a polygonal line.}

This is the \cmdln{g:Dpolyline(L \fac{, close, draw\_options, clip})} method (where \emph{g} denotes the graphic being created), the argument \argu{L} is a polygonal line, \argu{close} is an optional argument equal to \true or \false indicating whether the line should be closed or not (\false by default), and \argu{draw\_options} is a string that will be passed directly to the \drawcmd instruction in the export (empty by default). The argument \argu{clip} must contain either \nil (default value) or a table of the form \code{\{x1,x2,y1,y2\}}, in the first case the line is clipped by the current 2D window \textbf{after} its transformation by the 2D matrix of the graph, in the second case the line is clipped by the window $[x_1;x_2]\times[y_1;y_2]$ \textbf{before} being transformed by the graph matrix.


\subsubsection{Options for drawing a polygonal line}

Drawing options can be passed directly to the \drawcmd instruction in the export, but they will have only a local effect. These options can be modified globally using the method
\cmdln{g:Lineoptions(style \fac{, color, width, arrows})}
 when one of the arguments is nil, its default value is used.

\begin{itemize}
    \item \argu{color} is a string representing a color known to TikZ (default is black),
    \item \argu{style} is a string representing the type of line to draw. This style can be:
    \begin{itemize}
        \item \val{"noline"}: undrawn line,
        \item \val{"solid"}: solid line (default value),
        \item \val{"dotted"}: dotted line,
        \item \val{"dashed"}: dashed line,
        \item custom style: the \argu{style} argument can be a string of the form (example): \code{"\{2.5pt\}\{2pt\}"} which means: a 2.5pt line followed by a 2pt space, the number of values ​​can be greater than 2, e.g.: \code{"\{2.5pt\}\{2pt\}\{1pt\}\{2pt\}"} (succession of on, off).
    \end{itemize}
    \item \argu{width} is a number representing the line thickness expressed in tenths of a point, for example $8$ for an actual thickness of 0.8pt (default value of $4$),
    \item \argu{arrows} is a string that specifies the type of arrow that will be drawn, this can be:
    \begin{itemize}
        \item \val{"-"} which means no arrow (default value),
        \item \val{"->"} which means an arrow at the end,
        \item \val{"<-"} which means an arrow at the beginning,
        \item \val{"<->"} which means an arrow at each end.\\
    \textbf{WARNING}: The arrow is not drawn when the argument \argu{close} is \true. 
    \end{itemize}
\end{itemize}

The options can be modified individually using the following methods:
\begin{itemize}
    \item \cmd{g:Linecolor(color)},
    \item \cmd{g:Linestyle(style)},
    \item \cmd{g:Linewidth(width)},
    \item \cmd{g:Arrows(arrows)},
    \item plus the following methods:
\begin{itemize}
    \item \cmd{g:Lineopacity(opacity)}, which sets the opacity of the line drawing. The \argu{opacity} argument must be a value between $0$ (fully transparent) and $1$ (fully opaque). The default value is $1$.     
    \item \cmd{g:Linecap(style)}, to adjust the line ends, the \argu{style} argument is a string that can be:
\begin{itemize}
    \item \val{"butt"} (straight end at the breakpoint, default value),
    \item \val{"round"} (rounded semicircle end),
    \item \val{"square"} (rounded square end).
\end{itemize}

    \item \cmd{g:Linejoin(style)}, to adjust the join between segments, the \argu{style} argument is a string that can be:
\begin{itemize}
    \item \val{"miter"} (pointed corner, default value),
    \item \val{"round"} (or rounded corner),
    \item \val{"bevel"} (cut corner).
\end{itemize} 
\end{itemize}
\end{itemize}

\paragraph{Polygonal line fill options.}

This is the \cmdln{g:Filloptions(style \fac{, color, opacity, evenodd})} method (which uses the TikZ \emph{patterns} library, which is loaded with the package). When one of the arguments is \nil, its default value is used:

\begin{itemize}
    \item \argu{color} is a string representing a color known to TikZ (default is \val{"black"}).
    \item \argu{style} is a string representing the fill type. This style can be:
\begin{itemize}
    \item \val{"none"}: no fill, this is the default value,
    \item \val{"full"}: full fill,
    \item \val{"bdiag"}: descending hatching from left to right,
    \item \val{"fdiag"}: ascending hatching from left to right,
    \item \val{"horizontal"}: horizontal hatching,
    \item \val{"vertical"}: vertical hatching,
    \item \val{"hvcross"}: horizontal and vertical hatching,
    \item \val{"diagcross"}: descending and ascending diagonals,
    \item \val{"gradient"}: in this case, the fill is done with a gradient defined with the method \cmd{g:Gradstyle(string)}, this style is passed as is to the \drawcmd instruction. By default, the string defining the gradient style is:
     \codeln{"left color=white,right color=red"}
    \item Any other style known from the \emph{patterns} library is also possible.
    \item \argu{opacity}: a number between $0$ and $1$ ($1$ by default).
    \item \argu{evenodd}: a boolean indicating whether the \emph{even odd rule} option should be used for fill (\false by default).
\end{itemize}
\end{itemize}

Some options can be modified individually with the methods:
\begin{itemize}
    \item \cmd{g:Fillopacity(opacity)},
    \item \cmd{g:Filleo(evenodd)}.
\end{itemize}

\begin{demo}{Vector field, integral curve of $y'= 1-xy^2$}
\begin{luadraw}{name=champ}
local ld = luadraw
local cpx = ld.cpx
local Z = cpx.Z
local g = ld.graph:new{window={-5,5,-5,5},bg="Cyan!30",size={10,10}}
local f = function(x,y) -- ode y'= 1-x*y^2=f(x,y)
    return 1-x*y^2
end
local A = Z(-1,1) -- A = -1+i
local deltaX, deltaY, long = 0.5, 0.5, 0.4
local champ = function(f)
    local vecteurs, v = {}
    for y = g:Yinf(), g:Ysup(), deltaY do
        for x = g:Xinf(), g:Xsup(), deltaX do
            v = Z(1,f(x,y)) -- coordinates 1 and f(x,y)
            v = v/cpx.abs(v)*long -- normalization of v
            table.insert(vecteurs, {Z(x,y), Z(x,y)+v} )
        end
    end
    return vecteurs -- vecteurs is a polygonal line
end

g:Daxes( {0,1,1}, {labelpos={"none","none"}, arrows="->"} )
g:Dpolyline( champ(f), "->,blue")
g:Dodesolve(f, A.re, A.im, {t={-2.75,5},draw_options="red,line width=0.8pt"})
g:Dlabeldot("$A$", A, {pos="S"})
g:Show()
\end{luadraw}
\end{demo}
\label{field}


\subsection{Segments and Lines}

In this section we will use the alias \code{local ld = luadraw}.

\textbf{A segment is a list (table) of two complex numbers representing the endpoints. A line is a list (table) of two complex numbers, the first representing a point on the line, and the second a direction vector of the line (this must be non-zero).}

\subsubsection{Dangle}
\begin{itemize}
    \item The \cmd{g:Dangle(B, A, C \fac{, r, draw\_options})} method draws the angle \(BAC\) with a parallelogram (only two sides are drawn). The optional argument \argu{r} specifies the length of one side ($0.25$ by default). The argument \argu{draw\_options} is a string (empty by default) that will be passed as is to the \drawcmd instruction.
     \item The function \cmd{ld.angleD(B, A, C, r)} returns the list of points of this angle.
\end{itemize}

\newcommand*{\arguScale}{%
The argument \argu{scale} (which defaults to $1$) is either a number (percentage) to vary the length of the displayed segment (from its midpoint), or an array of two numbers (percentages) \{scaleA, scaleB\} to vary the length of the displayed segment at each of its endpoints.
}%

\subsubsection{Dbissec}
\begin{itemize}
    \item The method \cmd{g:Dbissec(B, A, C, interior, draw\_options, scale)} draws a bisector of the geometric angle BAC, interior if the optional argument \argu{interior} is \true (default value), exterior otherwise. The argument \argu{draw\_options} is a string (empty by default) that will be passed as is to the instruction \drawcmd. \arguScale
    \item The function \cmd{ld.bissec(B, A, C, interior)} returns this bisector as a list $\{A,u\}$ where $u$ is a direction vector of the line. 
\end{itemize}

\subsubsection{Dhline}
The method \cmd{g:Dhline(d \fac{, draw\_options, scale})} draws a half-line. The argument \argu{d} must be a list of two complex numbers $\{A,B\}$ variables. The half-line $[A,B)$ is drawn.

Variant: \cmd{g:Dhline(A, B \fac{, draw\_options, scale})}. The argument \argu{draw\_options} is a string (empty by default) that will be passed as is to the \drawcmd instruction. \arguScale

\subsubsection{Dline}
The \cmd{g:Dline(d \fac{, draw\_options, scale})} method draws the line \argu{d}, which is a list of type $\{A,u\}$ where $A$ represents a point on the line (a complex number) and $u$ a direction vector (a non-zero complex number).

Variant: the \cmd{g:Dline(A, B \fac{, draw\_options, scale})} method draws the line passing through the points \argu{A} and \argu{B} (two complex numbers). The argument \argu{draw\_options} is a string (empty by default) that will be passed as is to the \drawcmd instruction. \arguScale

\subsubsection{DlineEq}
\begin{itemize}
    \item The \cmd{g:DlineEq(a, b, c \fac{, draw\_options, scale})} method draws the line with the equation \(ax+by+c=0\). The \argu{draw\_options} argument is a string (empty by default) that will be passed as is to the \drawcmd instruction. \arguScale
    
    \item The \cmd{ld.lineEq(a, b, c \fac{, inequality})} function returns the line with the equation \(ax+by+c=0\) as a list $\{A,u\}$ where $A$ is a point on the line and $u$ is a direction vector. The optional argument \argu{inequality} (\nil by default) can also take the value \val{'<'} or \val{'>'}, the direction vector $u$ is then chosen such that the half-plane satisfying the inequality $ax+by+c<0$ (or $ax+by+c>0$ in the other case) is to the left of $u$ (in the trigonometric direction).
\end{itemize}

\subsubsection{Dmarkarc}
The method \cmd{g:Dmarkarc(b, a, c, r, n \fac{, length, space, draw\_options})} marks the arc of a circle with center \argu{a}, radius \argu{r}, extending from \argu{b} to \argu{c}, with \argu{n} small segments. By default, the \argu{length} is $0.25$, and the spacing (argument \argu{space}) is $0.0625$. The argument \argu{draw\_options} is a string (empty by default) that will be passed as is to the instruction \drawcmd.

\subsubsection{Dmarkseg}
The method \cmd{g:Dmarkseg(a, b, n \fac{, length, space, angle, draw\_options})} marks the segment defined by \argu{a} and \argu{b} with \argu{n} small segments inclined at \argu{angle} degrees (45° by default). By default, the \argu{length} is $0.25$, and the spacing (argument \argu{space}) is $0.125$. The argument \argu{draw\_options} is a string (empty by default) that will be passed as is to the instruction \drawcmd.

\subsubsection{Dmed}
\begin{itemize}
    \item The \cmd{g:Dmed(A, B \fac{, draw\_options, scale})} method draws the perpendicular bisector of the segment $[A;B]$.

Variant: \cmd{g:Dmed(seg \fac{, draw\_options, scale})} where segment is a list of two points representing the segment. The \argu{draw\_options} argument is a string (empty by default) that will be passed as is to the \drawcmd instruction. \arguScale

     \item The function \cmd{ld.med(A, B)} (or \cmd{ld.med(seg)}) returns the perpendicular bisector of the segment $[A;B]$ as a list $\{C,u\}$ where $C$ is a point on the line and $u$ is a direction vector.
\end{itemize}

\subsubsection{Dparallel}
\begin{itemize}
    \item The method \cmd{g:Dparallel(d, A \fac{, draw\_options, scale})} draws the parallel to \argu{d} passing through \argu{A}. The argument \argu{d} can be either a line (a list consisting of a point and a direction vector) or a non-zero vector. The argument \argu{draw\_options} is a string (empty by default) that will be passed as is to the instruction \drawcmd. \arguScale
    
    \item The function \cmd{ld.parallel(d, A)} returns the parallel to \argu{d} passing through \argu{A} as a list $\{B,u\}$ where $B$ is a point on the line and $u$ is a direction vector.
\end{itemize}

\subsubsection{Dperp}
\begin{itemize}
    \item The method \cmd{g:Dperp(d, A \fac{, draw\_options, scale})} draws the perpendicular to \argu{d} passing through \argu{A}. The argument \argu{d} can be either a line (a list consisting of a point and a direction vector) or a non-zero vector. The argument \argu{draw\_options} is a string (empty by default) that will be passed as is to the instruction \drawcmd. \arguScale
    
    \item The function \cmd{ld.perp(d, A)} returns the perpendicular to \argu{d} passing through \argu{A} as a list $\{B,u\}$ where $B$ is a point on the line and $u$ is a direction vector.
\end{itemize}

\subsubsection{Dseg}
The \cmd{g:Dseg(seg \fac{, scale, draw\_options})} method draws the segment defined by the \argu{seg} argument, which must be a list of two complex numbers. The \argu{draw\_options} argument is a string (empty by default) that will be passed as is to the \drawcmd instruction. \arguScale

\subsubsection{Dtangent}
\begin{itemize}
    \item The \cmd{g:Dtangent(p, t0 \fac{, length, draw\_options})} method draws the tangent to the curve parameterized by \argu{p}: \(t \mapsto p(t)\) (with complex values), at the parameter point \argu{t0}. If the argument \argu{length} is \nil (the default value), then the entire line is drawn; otherwise, it is a segment of \argu{length}. The argument \argu{draw\_options} is a string (empty by default) that will be passed as is to the \drawcmd instruction.
    \item The function \cmd{ld.tangent(p, t0 \fac{, length})} returns either the line or a segment (depending on whether \argu{length} is \nil or not).
\end{itemize}

\subsubsection{DtangentC}
\begin{itemize}
    \item The method \cmd{g:DtangentC(f, x0 \fac{, length, draw\_options})} draws the tangent to the Cartesian curve with equation \(y=f(x)\), at the abscissa point \argu{x0}. If the argument \argu{length} is \nil (the default value), then the entire line is drawn; otherwise, it is a segment of \argu{length}. The argument \argu{draw\_options} is a string (empty by default) that will be passed as is to the instruction \drawcmd.
    \item The function \cmd{ld.tangentC(f, x0 \fac{, length})} returns either the line or a segment (depending on whether \argu{length} is \nil or not).
\end{itemize}

\subsubsection{DtangentI}
\begin{itemize}
    \item The method \cmd{g:DtangentI(f, x0, y0 \fac{, length, draw\_options})} draws the tangent to the implicit curve with equation \(f(x,y)=0\), at the assumed point \argu{(x0,y0)} on the curve. If the argument \argu{length} is \nil (the default value), then the entire line is drawn; otherwise, it is a segment of \argu{length}. The argument \argu{draw\_options} is a string (empty by default) that will be passed as is to the instruction \drawcmd.
    \item The function \cmd{ld.tangentI(f, x0, y0 \fac{, length})} returns either the line or a segment (depending on whether \argu{length} is \nil or not).
\end{itemize}


\subsubsection{Dtangent\_from}

\begin{itemize}
    \item The method \cmd{g:Dtangent\_from(A, p, t1, t2 \fac{, dp, draw\_options, out, scale})} draws the tangent(s) to the curve parameterized by \argu{p}: \(t \mapsto p(t)\) (with complex values) originating from the point \argu{A} (a complex number). The arguments \argu{t1} and \argu{t2} represent the bounds of the search interval. The optional argument \argu{dp} is a function representing the derivative of the function \argu{p}; by default, this argument is \nil, and the derivative of \argu{p} is replaced by an approximation. The optional argument \argu{draw\_options} is a string (empty by default) that will be passed as is to the \drawcmd instruction. The optional argument \argu{out}, if used, must be the name of a variable. This variable must be an array, and at the end of execution, it will contain the points on the curve for which the tangent passes through \argu{A}. \arguScale

    \item The function \cmd{ld.tangent\_from(A, p, t1, t2 \fac{, dp})} returns the list of points on the curve parameterized by \argu{p}, on the interval \([t_1;t_2]\), for which the tangent passes through \argu{A} (a complex number). If there are no points, the function returns an empty list.
\end{itemize}

\begin{demo}{Tangents from a point}
\begin{luadraw}{name=tangent_from}
local ld = luadraw
local cpx = ld.cpx
local i, cos, sin, pi = cpx.I, math.cos, math.sin, math.pi
local g = ld.graph:new{window={-5,5,-5,5}, size={10,10}}
local p1 = function(t) return t+i*(t^2/4-1) end -- parabola
local p2 = function(t) return 1/2+i+ld.rotate(2*cos(t)+i*sin(t),15) end -- ellipse
local p3 = function(t) return math.sinh(t)+i*math.cosh(t)-i*2 end -- branch of a hyperbola
local p4 = function(t) return 2*cos(3*t)*cpx.exp(i*t) end -- other
local P = 0.5-1.25*i
local draw = function(p,t1,t2)
    local axis_style = { arrows='-Stealth', legend={'$x$','$y$'} }
    local S = {}
    g:Daxes({0, 1, 1}, axis_style)
    g:Dparametric(p,{t={t1,t2}, draw_options="line width=0.8pt, Crimson"})
    g:Dtangent_from(P,p,t1,t2,"blue",S)
    g:Ddots(S,"Crimson"); g:Ddots(P); g:Dlabel("$A$",P,{pos="S"})
end
g:Saveattr(); g:Viewport(-5,-0.25,0.25,5); g:Coordsystem(-3,4,-3,4); draw(p1,-4,4); g:Restoreattr()
g:Saveattr(); g:Viewport(0.25,5,0.25,5); g:Coordsystem(-3,3,-3,3); draw(p2,-pi,pi); g:Restoreattr()
g:Saveattr(); g:Viewport(-5,-0.25,-5,-0.25); g:Coordsystem(-3,4,-3,3); draw(p3,-4,4); g:Restoreattr()
g:Saveattr(); g:Viewport(0.25,5,-5,-0.25); g:Coordsystem(-3,3,-3,3); draw(p4,-pi,pi); g:Restoreattr()
g:Show()
\end{luadraw}
\end{demo}

\subsubsection{Dnormal}
\begin{itemize}
    \item The method \cmd{g:Dnormal(p, t0 \fac{, long, draw\_options})} draws the normal to the curve parameterized by \argu{p}: \(t \mapsto p(t)\) (with complex values), at the point parameter \argu{t0}. If the argument \argu{long} is \nil (the default value), then the entire line is drawn; otherwise, it is a segment of length \argu{long}. The argument \argu{draw\_options} is a string (empty by default) that will be passed as is to the instruction \drawcmd.
    \item The function \cmd{ld.normal(p, t0 \fac{, long})} returns either the line or a segment (depending on whether \argu{long} is \nil or not).
\end{itemize}

\subsubsection{DnormalC}
\begin{itemize}
    \item The \cmd{g:DnormalC(f, x0 \fac{, long, draw\_options})} method draws the normal to the Cartesian curve with equation \(y=f(x)\), at the abscissa point \argu{x0}. If the argument \argu{long} is \nil (the default value), then the entire line is drawn; otherwise, it is a segment of length \argu{long}. The argument \argu{draw\_options} is a string (empty by default) that will be passed as is to the \drawcmd instruction.
    \item The function \cmd{ld.normalC(f, x0 \fac{, long})} returns either the line or a segment (depending on whether \argu{long} is \nil or not).
\end{itemize}

\subsubsection{DnormalI}
\begin{itemize}
    \item The \cmd{g:DnormalI(f, x0, y0 \fac{, long, draw\_options})} method draws the normal to the implicit curve with equation \(f(x,y)=0\), at the assumed point \argu{(x0,y0)} on the curve. If the \argu{long} argument is \nil (the default value), then the entire line is drawn; otherwise, it is a segment of length \argu{long}. The \argu{draw\_options} argument is a string (empty by default) that will be passed as is to the \drawcmd instruction.
    \item The function \cmd{ld.normalI(f, x0, y0, long)} returns either a line or a segment (depending on whether \argu{long} is \nil or not).
\end{itemize}

\begin{demo}{Symmetric of the orthocenter}
\begin{luadraw}{name=orthocentre}
local ld = luadraw
local cpx = ld.cpx
local g = ld.graph:new{window={-5,5,-5,5},bg="cyan!30",size={10,10}}
local i = cpx.I
local A, B, C = 4*i, -2-2*i, 3.5
local h1, h2 = ld.perp({B,C-B},A), ld.perp({A,B-A},C) -- altitudes of the triangle
local A1, F = ld.proj(A,{B,C-B}), ld.proj(C,{A,B-A}) -- projected
local H = ld.interD(h1,h2) -- orthocenter
local A2 = 2*A1-H -- symmetric to H with respect to BC
g:Dpolyline({A,B,C},true, "draw=none,fill=Maroon,fill opacity=0.3") -- background of the triangle
g:Linewidth(6); g:Filloptions("full", "blue", 0.2)
g:Dangle(C,A1,A,0.25); g:Dangle(B,F,C,0.25) -- right angles
g:Linecolor("black"); g:Filloptions("full","cyan",0.5)
g:Darc(H,C,A2,1); g:Darc(B,A,A1,1) -- arcs
g:Filloptions("none","black",1) -- The opacity is restored to 1
g:Dmarkarc(H,C,A1,1,2); g:Dmarkarc(A1,C,A2,1,2) -- markers
g:Dmarkarc(B,A,H,1,2)
g:Linewidth(8); g:Linecolor("black")
g:Dseg({A,B},1.25); g:Dseg({C,B},1.25); g:Dseg({A,C},1.25) -- sides
g:Linecolor("red"); g:Dcircle(A,B,C) -- circle
g:Linecolor("blue"); g:Dline(h1); g:Dline(h2) -- altitudes of the triangle
g:Dseg({A2,C}); g:Linecolor("red"); g:Dseg({H,A2}) -- segments
g:Dmarkseg(H,A1,2); g:Dmarkseg(A1,A2,2) -- markers
g:Labelcolor("blue") -- for the label
g:Dlabel("$A$",A, {pos="NW",dist=0.1}, "$B$",B, {pos="SW"}, "$A_2$",A2,{pos="E"},
"$C$", C, {pos="S" }, "$H$", H, {pos="NE"}, "$A_1$", A1, {pos="SW"})
g:Linecolor("black"); g:Filloptions("full"); g:Ddots({A,B,C,H,A1,A2})
g:Show()
\end{luadraw}
\end{demo}

\subsection{Geometric Figures}

\subsubsection{Darc}
\begin{itemize}
    \item The \cmd{g:Darc(B, A, C, r \fac{, sens, draw\_options})} method draws an arc of a circle with center \argu{A} (complex number), radius \argu{r}, going from \argu{B} (complex number) to \argu{C} (complex) counterclockwise if the argument \argu{sens} is $1$ (default value), and counterclockwise otherwise. The argument \argu{draw\_options} is a string (empty by default) that will be passed as is to the \drawcmd instruction.
    \item The \cmd{ld.arc(B, A, C, r, sens)} function returns the list of points on this arc (polygonal line).
    \item The function \cmd{ld.arcb(B, A, C, r, sens)} returns this arc as a path (see \emph{Dpath} page \pageref{Dpath}) using Bézier curves.
\end{itemize}

\subsubsection{Dcircle}
\begin{itemize}
    \item The method \cmd{g:Dcircle(c, r \fac{, d, draw\_options})} draws a circle. When the argument \argu{d} is \nil, it is the circle with center \argu{c} (complex number) and radius \argu{r}; when \argu{d} is specified (complex number), it is the circle passing through the affix points \argu{c}, \argu{r}, and \argu{d}. The argument \argu{draw\_options} is a string (empty by default) that will be passed as is to the instruction \drawcmd. Another possible syntax: \cmd{g:Dcircle(C \fac{, draw\_options})} where \argu{C}$=\{c,r \fac{,d}\}$.
    \item The \cmd{ld.circle(c, r \fac{, d})} function returns the list of points on this circle (polygonal line). 
    \item The function \cmd{ld.circle(\{c, r \fac{, d}\}, nbdots)} returns the list of points of this circle (polygonal line) with the \argu{nbdots} points.
    \item The \cmd{ld.circleb(c, r \fac{, d})} function returns this circle as a path (see \emph{Dpath} page \pageref{Dpath}) using Bézier curves.
\end{itemize}

\subsubsection{Dellipse}
\begin{itemize}
    \item The \cmd{g:Dellipse(c, rx, ry \fac{, inclin, draw\_options})} method draws the ellipse centered at \argu{c} (complex number). The arguments \argu{rx} and \argu{ry} specify the two radii (on $x$ and $y$). The optional argument \argu{inclin} is an angle in degrees that indicates the inclination of the ellipse relative to the \(Ox\) axis (zero by default). The argument \argu{draw\_options} is a string (empty by default) that will be passed as is to the \drawcmd instruction.
    \item The \cmd{ld.ellipse(c, rx, ry \fac{, inclin})} function returns the list of points on this ellipse (polygonal line). 
    \item The function \cmd{ld.ellipse(\{c, rx, ry \fac{, inclin}\}, nbdots)} returns the list of points of this ellipse (polygonal line) with the \argu{nbdots} points.
    \item The function \cmd{ld.ellipseb(c, rx, ry \fac{, inclination})} returns this ellipse as a path (see \emph{Dpath} page \pageref{Dpath}) using Bézier curves.
\end{itemize}

\subsubsection{Dellipticarc}
\begin{itemize}
    \item The method \cmd{g:Dellipticarc(B, A, C, rx, ry \fac{, sens, inclination, draw\_options})} draws an arc of an ellipse centered at \argu{A} (complex number) and radii at \argu{rx} and \argu{ry}, making an angle equal to \argu{inclination} with respect to the \(Ox\) axis (zero by default), going from \argu{B} (complex number) to \argu{C} (complex number) in the counterclockwise direction if the argument \argu{sens} is $1$ (default value), and the opposite direction otherwise. The \argu{draw\_options} argument is a string (empty by default) that will be passed as is to the \drawcmd instruction.
    \item The \cmd{ld.ellipticarc(B, A, C, rx, ry \fac{, sens, inclination})} function returns the list of points of this arc (polygonal line).
    \item The \cmd{ld.ellipticarcb(B, A, C, rx, ry \fac{, sens, inclination})} function returns this arc as a path (see \emph{Dpath} page \pageref{Dpath}) using Bézier curves.
\end{itemize}

\subsubsection{Dparallelogram}
\begin{itemize}
    \item The method \cmd{g:Dparallelogram(a, u, v \fac{, draw\_options})} where \argu{a}, \argu{u}, \argu{v} represent three complex numbers denoting a vertex and two vectors, draws the parallelogram with vertices $\{a,a+u,a+u+v,a+v\}$. The argument \argu{draw\_options} is a string (empty by default) which will be passed as is to the \drawcmd instruction.
    \item The function \cmd{ld.parallelogram(a, u, v)} returns the list of vertices of this parallelogram.
\end{itemize}

\subsubsection{Dpolyreg}
\begin{itemize}
    \item The \cmd{g:Dpolyreg(vertex1, vertex2, nbsides, direction \fac{, draw\_options})} or \par \cmd{g:Dpolyreg(center, vertex, nbsides \fac{, draw\_options})} method draws a regular polygon. The \argu{draw\_options} argument is a string (empty by default) that will be passed as is to the \drawcmd instruction.
    \item The \cmd{ld.polyreg(vertex1, vertex2, nbsides, direction)} function and the \cmd{ld.polyreg(center, vertex, nbsides)} function return the list of vertices of this regular polygon. \end{itemize}

\subsubsection{Drectangle}
\begin{itemize}
    \item The \cmd{g:Drectangle(a, b, c \fac{, draw\_options})} method draws a rectangle with consecutive vertices \argu{a} and \argu{b}, and whose opposite side passes through \argu{c} (complex numbers). The \argu{draw\_options} argument is a string (empty by default) that will be passed as is to the \drawcmd instruction. \par
    The \cmd{ld.rectangle(a, b, c)} function returns the list of vertices of this rectangle.
    
    \item The method \cmd{g:Drectangle(a, b \fac{, draw\_options})} draws the rectangle whose sides are parallel to the axes and whose opposite vertices are \argu{a} and \argu{b}. ​​The argument \argu{draw\_options} is a string (empty by default) which is passed as is to the \drawcmd instruction. \par
    The function \cmd{ld.rectangle(a, b)} returns the list of vertices of this rectangle.
\end{itemize}

\subsubsection{Dsequence}
\begin{itemize}
    \item The \cmd{g:Dsequence(f, u0, n, draw\_options)} method draws the "staircases" of the recurrent sequence defined by its first term \argu{u0} and the relation \(u_{k+1}=f(u_k)\). The argument \argu{f} must be a function of a real-valued variable, and the argument \argu{n} is the number of terms calculated. The argument \argu{draw\_options} is a string (empty by default) that will be passed as is to the \drawcmd instruction.
    \item The \cmd{ld.sequence(f, u0, n)} function returns the list of points constituting these "staircases".
\end{itemize}

\begin{demo}{Sequence $u_{n+1}=\cos(u_n)$}
\begin{luadraw}{name=sequence}
local ld = luadraw
local cpx = ld.cpx
local g = ld.graph:new{window={-0.1,1.7,-0.1,1.1},size={10,10,0}}
local i, pi, cos = cpx.I, math.pi, math.cos
local f = function(x) return cos(x)-x end
local ell = ld.solve(f,0,pi/2)[1]
local L = ld.sequence(cos,0.2,5) -- u_{n+1}=cos(u_n), u_0=0.2
local seg, z = {}, L[1]
for k = 2, #L do
    table.insert(seg,{z,L[k]})
    z = L[k]
end -- seg is a list of segments (staircases)
local styleA = "\\tikzset{->-/.style={decoration={markings, mark="
local styleB = "at position #1 with {\\arrow{Stealth}}}, postaction={decorate}}}"
g:Writeln(styleA..styleB)
g:Daxes({0,1,1}, {arrows="-Stealth"})
g:DlineEq(1,-1,0,"line width=0.8pt,ForestGreen")
g:Dcartesian(cos, {x={0,pi/2},draw_options="line width=1.2pt,Crimson"})
g:Dpolyline(seg,false,"->-=0.65,blue")
g:Dlabel("$u_0$",0.2,{pos="S",node_options="blue"})
g:Dseg({ell, ell*(1+i)},1,"dashed,gray")
g:Dlabel("$\\ell\\approx"..ld.round(ell,3).."$", ell,{pos="S"})
g:Ddots(ell*(1+i)); g:Labelcolor("Crimson")
g:Dlabel("${\\mathcal C}_{\\cos}$",1+i*cos(1),{pos="E"})
g:Labelcolor("ForestGreen"); g:Labelangle(g:Arg(1+i)*180/pi)
g:Dlabel("$y=x$",0.4+i*0.4,{pos="S",dist=0.1})
g:Show()
\end{luadraw}
\end{demo}

The \cmd{g:Arg(z)} method calculates and returns the \textit{real} argument of the complex $z$, that is, its argument (in radians) when exported to the TikZ coordinate system (to do this, you must apply the graph's transformation matrix to $z$, then convert the coordinate system to that of TikZ). If the graph coordinate system is orthonormal and the transformation matrix is ​​the identity matrix, then the result is identical to that of \code{cpx.arg(z)} (this is not the case in the example above).

Similarly, the \cmd{g:Abs(z)} method calculates and returns the \textit{real} modulus of the complex $z$, that is, its modulus when exported to the TikZ coordinate system; it is therefore a length in centimeters. If the graph coordinate system is orthonormal with 1 cm per unit on each axis, and if the transformation matrix is ​​an isometry, then the result is identical to that of \code{cpx.abs(z)}.

\subsubsection{Dsquare}
\begin{itemize}
    \item The \cmd{g:Dsquare(a, b \fac{, sens, draw\_options})} method draws the square with consecutive vertices \argu{a} and \argu{b} (complex numbers), in the counterclockwise direction when \argu{sens} is $1$ (the default value). The argument \argu{draw\_options} is a string (empty by default) that will be passed as is to the \drawcmd instruction.
    \item The \cmd{ld.square(a, b, sens)} function returns the list of vertices of this square.
\end{itemize}

\subsubsection{Dwedge}
The method \cmd{g:Dwedge(B, A, C, r \fac{, sens, draw\_options})} draws an angular sector with center \argu{A} (complex number), radius \argu{r}, going from \argu{B} (complex number) to \argu{C} (complex number) counterclockwise if the argument \argu{sens} is $1$, and counterclockwise otherwise. The argument \argu{draw\_options} is a string (empty by default) that will be passed as is to the instruction \drawcmd.

\subsection{Curves}

\subsubsection{Parametric: Dparametric}

\begin{itemize}
    \item The function \cmd{ld.parametric(p, t1, t2 \fac{, nbdots, discont, nbdiv})} calculates the points of the curve and returns a polygonal line (a list of lists of complex numbers, no drawing).
\begin{itemize}
    \item The argument \argu{p} is the parameterization; it must be a function of a real variable $t$ and complex-valued variables, for example:
\mintinline{Lua}{local p = function(t) return cpx.exp(t*cpx.I) end}
    \item The arguments \argu{t1} and \argu{t2} are required with \(t_1 < t_2\); they form the bounds of the interval for the parameter.
     \item The argument \argu{nbdots} is optional; it is the (minimum) number of points to calculate; it is $40$ by default.
    \item The argument \argu{discont} is an optional Boolean that indicates whether there are discontinuities or not; it is \false by default.
    \item The argument \argu{nbdiv} is a positive integer that is $5$ by default and indicates the number of times the interval between two consecutive values ​​of the parameter can be cut in two (dichotomized) when the corresponding points are too far apart.
\end{itemize}

    \item The method \cmd{g:Dparametric(p, options)} calculates the points and draws the curve parametrized by \argu{p}. The parameter \argu{options} is a table whose fields are the possible options. These are, with their default values:

\begin{itemize}
    \item \opt{t= \{g:Xinf(),g:Xsup()\}}, this is the interval for the parameter $t$ (by default, it's the entire range of the window's x-coordinates),
    \item \opt{nbdots=40}, 
    \item \opt{discont=false},
    \item \opt{nbdiv=5},
    \item \opt{draw\_options=""}, string which will be passed as is to the instruction \drawcmd,
    \item \opt{clip=nil}, this option is either \nil (default value) or a table \code{\{x1,x2,y1,y2\}}. In the first case, the line is clipped by the current 2D window \textbf{after} its transformation by the graph's 2D matrix. In the second case, the line is clipped by the window $[x_1;x_2]\times[y_1;y_2]$ \textbf{before} being transformed by the graph's matrix.
\end{itemize}
\end{itemize}


\subsubsection{Polars: Dpolar}

\begin{itemize}
    \item The function \cmd{ld.polar(rho, t1, t2 \fac{, nbdots, discont, nbdiv})} calculates the points of the curve and returns a polygonal line (no drawing). The argument \argu{rho} is the polar parameterization of the curve; it must be a function of a real variable $t$ and with real values, for example:

\mintinline{Lua}{local rho = function(t) return 4*math.cos(2*t) end}

The other arguments are identical to those for parameterized curves.
    \item The method \cmd{g:Dpolar(rho, options)} calculates the points and draws the polar curve parameterized by \emph{rho}. The parameter \argu{options} is a table whose fields are the possible options. These are, with their default values:

\begin{itemize}
    \item \opt{t= \{-pi,pi\}}, this is the interval for the parameter $t$,
    \item \opt{nbdots=40}, 
    \item \opt{discont=false},
    \item \opt{nbdiv=5},
    \item \opt{draw\_options=""}, string which will be passed as is to the instruction \drawcmd,
    \item \opt{clip=nil}, this option is either \nil (default value) or a table \code{\{x1,x2,y1,y2\}}. In the first case, the line is clipped by the current 2D window \textbf{after} its transformation by the graph's 2D matrix. In the second case, the line is clipped by the window $[x_1;x_2]\times[y_1;y_2]$ \textbf{before} being transformed by the graph's matrix.
\end{itemize}
\end{itemize}

\subsubsection{Cartesian: Dcartesian}

\begin{itemize}
    \item The function \cmd{ld.cartesian(f, x1, x2 \fac{, nbdots, discont, nbdiv})} calculates the points and returns a polygonal line (no drawing). The argument \argu{f} is the function whose curve we want to obtain. It must be a function of a real variable $x$ and with real values, for example:

\mintinline{Lua}{local f = function(x) return 1+3*math.sin(x)*x end}

The arguments \argu{x1} and \argu{x2} are required and form the bounds of the interval for the variable. The other arguments are identical to those for parametric curves.

    \item The method \cmd{g:Dcartesian(f, options)} calculates the points and draws the curve of \emph{f}.  The argument \argu{options} is a table whose fields are the possible options. These are, with their default values:

\begin{itemize}
    \item \opt{x= \{g:Xinf(),g:Xsup()\}}, this is the interval for the parameter $t$ (by default, it's the entire range of the window's x-coordinates),
    \item \opt{nbdots=40}, 
    \item \opt{discont=false},
    \item \opt{nbdiv=5},
    \item \opt{draw\_options=""}, string which will be passed as is to the instruction \drawcmd,
    \item \opt{clip=nil}, this option is either \nil (default value) or a table \code{\{x1,x2,y1,y2\}}. In the first case, the line is clipped by the current 2D window \textbf{after} its transformation by the graph's 2D matrix. In the second case, the line is clipped by the window $[x_1;x_2]\times[y_1;y_2]$ \textbf{before} being transformed by the graph's matrix.
\end{itemize}
\end{itemize}

\subsubsection{Periodic Functions: Dperiodic}

\begin{itemize}
    \item The function \cmd{ld.periodic(f, period, x1, x2 \fac{, nbdots, discont, nbdiv})} calculates the points of the curve and returns a polygonal line (no drawing).

\begin{itemize}
    \item The argument \argu{f} is the function whose curve we want; it must be a function of a real variable $x$ and with real values.
    \item The argument \argu{period} is a table of the type $\{a,b\}$, with \(a<b\) representing a period of the function \argu{f}.
    \item The arguments \argu{x1} and \argu{x2} are required and form the bounds of the interval for the variable.
    \item The other arguments are identical to those for parametric curves.
\end{itemize}
    \item The method \cmd{g:Dperiodic(f, period, options)} calculates the points and draws the curve of \argu{f}. The argument \argu{options} is a table whose fields are the possible options. These are, with their default values:

\begin{itemize}
    \item \opt{x= \{g:Xinf(),g:Xsup()\}}, this is the interval for the parameter $t$ (by default, it's the entire range of the window's x-coordinates),
    \item \opt{nbdots=40}, 
    \item \opt{discont=false},
    \item \opt{nbdiv=5},
    \item \opt{draw\_options=""}, string which will be passed as is to the instruction \drawcmd,
    \item \opt{clip=nil}, this option is either \nil (default value) or a table \code{\{x1,x2,y1,y2\}}. In the first case, the line is clipped by the current 2D window \textbf{after} its transformation by the graph's 2D matrix. In the second case, the line is clipped by the window $[x_1;x_2]\times[y_1;y_2]$ \textbf{before} being transformed by the graph's matrix.
\end{itemize}
\end{itemize}

\subsubsection{Step Functions: Dstepfunction}

\begin{itemize}
    \item The \cmd{ld.stepfunction(def, discont)} function calculates the points of the curve and returns a polygonal line (no drawing).

\begin{itemize}
    \item The \argu{def} argument defines the step function; it is a table with two elements:

\begin{TeXcode}
{ {x1,x2,x3,...,xn}, {c1,c2,...} }
\end{TeXcode}

The first element \code{\{x1,x2,x3,\ldots,xn\}} must be a subdivision of the segment \([x_1;x_n]\).

The second element \code{\{c1,c2,\ldots\}} is the list of constants, with \code{c1} for the segment $[x_1;x_2]$, \code{c2} for the segment $[x_2;x_3]$, etc.

    \item The argument \argu{discont} is a Boolean that defaults to \true.
\end{itemize}

    \item The method \cmd{g:Dstepfunction(def, options)} calculates the points and draws the curve of the step function.

\begin{itemize}
    \item The argument \argu{def} is the same as the one described above (definition of the step function).     
    \item The argument \argu{options} is a table whose fields are the possible options. These are, with their default values:

\begin{itemize}
    \item \opt{discont=true},
    \item \opt{draw\_options=""}, string which will be passed as is to the instruction \drawcmd,
    \item \opt{clip=nil}, this option is either \nil (default value) or a table \code{\{x1,x2,y1,y2\}}. In the first case, the line is clipped by the current 2D window \textbf{after} its transformation by the graph's 2D matrix. In the second case, the line is clipped by the window $[x_1;x_2]\times[y_1;y_2]$ \textbf{before} being transformed by the graph's matrix.
\end{itemize}
\end{itemize}
\end{itemize}

\subsubsection{Piecewise Affine Functions: Daffinebypiece}

\begin{itemize}
    \item The function \cmd{ld.affinebypiece(def, discont)} calculates the points of the curve and returns a polygonal line (no drawing).

\begin{itemize}
    \item The argument \argu{def} defines the step function; it is a two-field table:

\begin{TeXcode}
{ {x1,x2,x3,...,xn}, { {a1,b1}, {a2,b2},...} }
\end{TeXcode}

The first element \code{\{x1,x2,x3,\ldots,xn\}} must be a subdivision of the segment \([x_1;x_n]\).

The second element \code{\{ \{a1,b1\},\{a2,b2\},\ldots\}} means that on $[x_1;x_2]$ the function is \(x\mapsto a_1x+b_1\), on $[x_2;x_3]$ the function is \(x\mapsto a_2x+b_2\), etc.

    \item The argument \argu{discont} is a boolean that defaults to \true.
\end{itemize}

    \item The method \cmd{g:Daffinebypiece(def, options)} calculates the points and draws the curve of the piecewise affine function.

\begin{itemize}
    \item The argument \argu{def} is the same as the one described above (definition of the piecewise affine function).
    \item The argument \argu{options} is a table whose fields are the possible options. These are, with their default values:

\begin{itemize}
    \item \opt{discont=true},
    \item \opt{draw\_options=""}, string which will be passed as is to the instruction \drawcmd,
    \item \opt{clip=nil}, this option is either \nil (default value) or a table \code{\{x1,x2,y1,y2\}}. In the first case, the line is clipped by the current 2D window \textbf{after} its transformation by the graph's 2D matrix. In the second case, the line is clipped by the window $[x_1;x_2]\times[y_1;y_2]$ \textbf{before} being transformed by the graph's matrix.
\end{itemize}
\end{itemize}
\end{itemize}

\subsubsection{Differential Equations: Dodesolve}

\begin{itemize}
    \item The function \cmd{ld.odesolve(f, t0, Y0, tmin, tmax, nbdots \fac{, method})} allows an approximate solution of the differential equation \(Y'(t)=f(t,Y(t))\) in the interval $[t_{\mathrm{min}};t_{\mathrm{max}}]$ which must contain \argu{t0}, with the initial condition $Y(t_0)=Y_0$.

\begin{itemize}
    \item The argument \argu{f} is a function \(f: (t,Y) -> f(t,Y)\) with values ​​in \(\mathbf R^n\) and where $Y$ is also in \(\mathbf R^n\): $Y=\{y_1,y_2,\ldots, y_n\}$, but when $n=1$, $Y$ is a real number.
    \item The arguments \argu{t0} and \argu{Y0} give the initial conditions with $Y_0=\{y_1(t_0), \ldots, y_n(t_0)\}$ (the $y_i$ numbers are real), or $Y_0=y_1(t_0)$ when $n=1$.
    \item The arguments \argu{tmin} and \argu{tmax} define the resolution interval; this must contain \argu{t0}.
    \item The argument \argu{nbdots} indicates the number of points calculated on either side of \argu{t0}.
    \item The optional argument \emph{method} is a string that can be \val{"rkf45"} (default), or \val{"rk4"}. In the first case, we use the Runge Kutta-Fehlberg method (with variable step size), in the second case, it is the classic Runge-Kutta method of order $4$.
    \item As output, the function returns the following matrix (list of lists of real numbers):

\begin{TeXcode}
{ {tmin,...,tmax}, {y1(tmin),...,y1(tmax)}, {y2(tmin),...,y2(tmax)},...}
\end{TeXcode}

The first component is the list of values ​​of $t$ (in ascending order), the second is the list of (approximate) values ​​of the component $y_1$ corresponding to these values ​​of $t$, etc.
\end{itemize}

    \item The method \cmd{g:DplotXY(X, Y \fac{, draw\_options, clip})}, where the arguments \argu{X} and \argu{Y} are two lists of real numbers of the same length, draws the polygonal line consisting of the points $(X[k],Y[k])$. The argument \argu{draw\_options} is a string (empty by default) that will be passed as is to the \drawcmd instruction. The argument \argu{clip} is either \nil (default value) or a table \code{\{x1,x2,y1,y2\}}. In the first case, the line is clipped by the current 2D window \textbf{after} its transformation by the graph's 2D matrix. In the second case, the line is clipped by the window $[x_1;x_2]\times[y_1;y_2]$ \textbf{before} being transformed by the graph's matrix.

\begin{demo}{A Lotka-Volterra differential system}
\begin{luadraw}{name=lotka_volterra}
local ld = luadraw
local cpx = ld.cpx
local g = ld.graph:new{window={-5,50,-0.5,5},size={10,10,0}, border=true}
local i = cpx.I
local f = function(t,y) return {y[1]-y[1]*y[2],-y[2]+y[1]*y[2]} end
g:Labelsize("footnotesize")
g:Daxes({0,10,1},{limits={{0,50},{0,4}}, nbsubdiv={4,0}, legendsep={0.1,0},
originpos={"center","center"}, legend={"$t$",""}})
local y0 = {2,2}
local M = ld.odesolve(f,0,y0,0,50,250) -- approximate resolution
-- M est une table à 3 éléments: t, x et y
g:Lineoptions("solid","blue",8)
g:Dseg({5+3.5*i,10+3.5*i}); g:Dlabel("$x$",10+3.5*i,{pos="E"})
g:DplotXY(M[1],M[2]) -- points (t,x(t))
g:Linecolor("red"); g:Dseg({5+3*i,10+3*i}); g:Dlabel("$y$",10+3*i,{pos="E"})
g:DplotXY(M[1],M[3]) -- points (t,y(t))
g:Lineoptions(nil,"black",4)
g:Saveattr(); g:Viewport(20,50,3,5) -- change of view
g:Coordsystem(-0.5,3.25,-0.5,3.25) -- new associated landmark
g:Daxes({0,1,1},{legend={"$x$","$y$"},arrows="->"})
g:Lineoptions(nil,"ForestGreen",8); g:DplotXY(M[2],M[3]) -- points (x(t),y(t))
g:Restoreattr() -- back to the old view
g:Dlabel("$\\begin{cases}x'=x-xy\\\\y'=-y+xy\\end{cases}$", 5+4.75*i,{})
g:Show()
\end{luadraw}
\end{demo}
    
    \item The method \cmd{g:Dodesolve(f, t0, Y0, options)} allows the drawing of a solution to the equation \(Y'(t)=f(t,Y(t))\).
\begin{itemize}
    \item The required argument \argu{f} is a function \(f: (t,Y) -> f(t,Y)\) with values ​​in \(\mathbf R^n\) and where $Y$ is also in \(\mathbf R^n\): $Y=\{y_1,y_2,\ldots,y_n\}$, but when $n=1$, $Y$ is a real number.
    \item The arguments \argu{t0} and \argu{Y0} give the initial conditions with $Y_0=\{y_1(t_0),\ldots,y_n(t_0)\}$ (the $y_i$ are real), or $Y_0=y_1(t_0)$ when $n=1$.
    \item The argument \argu{options} is a table whose fields are the possible options. These are, with their default values:

    \begin{itemize}
        \item \opt{t=\{g:Xinf(), g:Xsup()\}}, determines the interval for the variable $t$,
        \item \opt{out=\{1,2\}}, table of two integers $\{i_1,i_2\}$. If $M$ denotes the matrix returned by the \code{odesolve} function, the points drawn will have the $M[i_1]$ as abscissas and the $M[i_2]$ as ordinates. By default, we have $i_1=1$ and $i_2=2$, which corresponds to the $y_1$ function as a function of $t$,
        \item \opt{nbdots=50}, determines the number of points to calculate for the function,
        \item \opt{method="rkf45"}, determines the method to use; possible values ​​are \val{"rkf45"} (default value), or \val{"rk4"},
        \item \opt{draw\_options=""}, is a string (empty by default) that will be passed as is to the \drawcmd instruction,
        \item \opt{clip=nil}, this option is either \nil (default value) or a table \code{\{x1,x2,y1,y2\}}. In the first case, the line is clipped by the current 2D window \textbf{after} its transformation by the graph's 2D matrix. In the second case, the line is clipped by the window $[x_1;x_2]\times[y_1;y_2]$ \textbf{before} being transformed by the graph's matrix.
    \end{itemize}
\end{itemize}
\end{itemize}

\subsubsection{Implicit Curves: Dimplicit}

\begin{itemize}
    \item The function \cmd{ld.implicit(f, x1, x2, y1, y2, grid)} calculates and returns a polygonal line constituting the implicit curve with equation $f(x,y)=0$ in the box $[x_1,x_2]\times[y_1,y_2]$. This box is split according to the argument \argu{grid}.

\begin{itemize}
    \item The argument \argu{f} is a function \(f: (x,y) -> f(x,y)\) with values ​​in \(\mathbf R\).
    \item The arguments \argu{x1}, \argu{x2}, \argu{y1}, \argu{y2} define the plot window, which will be the $[x_1,x_2]\times[y_1,y_2]$ box. We must have \(x_1<x_2\) and \(y_1<y_2\).
    \item The argument \argu{grid} is a table containing two positive integers: $\{n_1,n_2\}$. The first integer indicates the number of subdivisions following the $x$-axis, and the second the number of subdivisions following the $y$-axis.
\end{itemize}

    \item The \cmd{g:Dimplicit(f, options)} method draws the implicit curve of equations $f(x,y)=0$.
\begin{itemize}
    \item The argument \argu{f} is a function \(f: (x,y) -> f(x,y)\) with values ​​in \(\mathbf R\).
    \item The argument \argu{options} is a table whose fields are the possible options. These are, with their default values:

\begin{itemize}
    \item \opt{view=\{g:Xinf(), g:Xsup(), g:Yinf(), g:Ysup()\}}, table of the form \code{\{x1,x2,y1,y2\}} which determines the drawing area $[x_1,x_2]\times[y_1,y_2]$,
    \item \opt{grid=\{50,50\}}, determines the subdivisions,
    \item \opt{draw\_options=""}, string (empty by default) that will be passed as is to the \drawcmd instruction.
\end{itemize}
\end{itemize}
\end{itemize}

\subsubsection{Contour Lines: Dcontour}

The \cmd{g:Dcontour(f, z, options)} method draws \textbf{contour lines} of the function \(f: (x,y) -> f(x,y)\) with real values.

\begin{itemize}
    \item The argument \argu{f} is the function.
    \item The argument \argu{z} is the list of different levels to plot.
    \item The argument \argu{options} is a table whose fields are the possible options. These are, with their default values:
    \begin{itemize}
        \item \opt{view=\{g:Xinf(), g:Xsup(), g:Yinf(), g:Ysup()\}}, table of the form \code{\{x1,x2,y1,y2\}} which determines the drawing area $[x_1,x_2]\times[y_1,y_2]$,
        \item \opt{grid=\{50,50\}}, determines the subdivisions,
        \item \opt{colors=\{\}}, list of colors per level. By default, this list is empty and the current drawing color is used.
        \item \opt{draw\_options=""}, string (empty by default) that will be passed as is to the  \drawcmd instruction.
    \end{itemize}
\end{itemize}

\begin{demo}{Example with Dcontour}
\begin{luadraw}{name=Dcontour}
local ld = luadraw
local cpx = ld.cpx
local g = ld.graph:new{window={-1,6.5,-1.5,11},size={10,10,0}}
local i, sin, cos = cpx.I, math.sin, math.cos
local f = function(x,y) return (x+y)/(2+cos(x)*sin(y)) end
local Lz = ld.range(1,10) -- levels to be drawn
local Colors = ld.getpalette(ld.palRainbow,10) -- 10 evenly distributed colors in the palRainbow palette
g:Dgradbox({0,5+10*i,1,1},{legend={"$x$","$y$"}, grid=true, title="$z=\\frac{x+y}{2+\\cos(x)\\sin(y)}$"})
g:Linewidth(12); g:Dcontour(f,Lz,{view={0,5,0,10}, colors=Colors})
for k = 1, 10 do
    local y = (2*k+4)/3*i
    g:Dseg({5.25+y,5.5+y},1,"color="..Colors[k])
    g:Labelcolor(Colors[k])
    g:Dlabel("$z="..k.."$",5.5+y,{pos="E"})
end
g:Show()
\end{luadraw}
\end{demo}

\subsubsection{Parameterization of a Polygonal Line: \emph{curvilinear\_param}}
Let $L$ be a list of complex numbers representing a \og continuous \fg line. It is possible to obtain a parameterization of this line based on a parameter $t$ between $0$ and $1$ ($t$ is the curvilinear abscissa divided by the total length of $L$).

The function \cmd{ld.curvilinear\_param(L \fac{, close})} returns a function of one variable $t\in[0;1]$ and values ​​on the line \argu{L} (list of complex numbers). The value at $t=0$ is the first point of \argu{L}, and the value at $t=1$ is the last point; this function is followed by a number representing the total length of \argu{L}. The optional argument \argu{close} indicates whether the line \argu{L} should be closed (\false by default).

\begin{demo}{Points distributed on a polygonal line}
\begin{luadraw}{name=curvilinear_param}
local ld = luadraw
local cpx = ld.cpx
local g = ld.graph:new{bbox=false,size={10,10}}
local i = cpx.I; g:Linewidth(8)
local L = ld.cartesian(math.sin,-5,5)[1]
table.append(L, {5-2*i, -5-2*i})
local f = ld.curvilinear_param(L, true)
local I = ld.map(f,ld.linspace(0,1,20)) -- 20 points distributed over L
g:Shift(4*i)
g:Lineoptions(nil,"ForestGreen",6); g:Dpolyline(L,true)
g:Filloptions("full","white"); g:Ddots(I) -- The first and last points coincide because L is closed
-- Another example of use:
local nb = 16 -- number of arrows
local t = ld.linspace(0,1,3*nb+1)
g:Shift(-4*i)
for k = 0,nb-1 do
g:Dparametric(f,{t={t[3*k+1],t[3*k+3]},nbdots=10,nbdiv=2,draw_options="-stealth"})
end
g:Show()
\end{luadraw}
\end{demo}


\subsection{Domains related to Cartesian curves}

\subsubsection{Ddomain1}

\begin{itemize}
    \item The function \cmd{ld.domain1(f, a, b \fac{, nbdots, discont, nbdiv})} returns a list of complex numbers that represents the contour of the part of the plane bounded by the curve of the function \argu{f} on an interval determined by \argu{a} and \argu{b}, the $x$-axis, and the lines \(x=a\), \(x=b\).

    \item The method \cmd{g:Ddomain1(f, options)} draws this contour. The argument \argu{options} is a table whose fields are the possible options. These are, with their default values:

    \begin{itemize}
        \item \opt{x= \{g:Xinf(),g:Xsup()\}}, this is the interval for the parameter $t$ (by default, it's the entire range of the window's x-coordinates),
        \item \opt{nbdots=40}, 
        \item \opt{discont=false},
        \item \opt{nbdiv=5},
        \item \opt{draw\_options=""}, string which will be passed as is to the instruction \drawcmd.
\end{itemize}
\end{itemize}

\subsubsection{Ddomain2}

\begin{itemize}
    \item The \cmd{ld.domain2(f, g, a, b \fac{, nbdots, discont, nbdiv})} function returns a list of complex numbers that represents the contour of the part of the plane bounded by the curve of the function \argu{f}, the curve of the function \argu{g}, and the lines \(x=a\), \(x=b\).

    \item The \cmd{g:Ddomain2(f, g, options)} method draws this contour. The argument \argu{options} is a table whose fields are the possible options. These are, with their default values:

    \begin{itemize}
        \item \opt{x= \{g:Xinf(),g:Xsup()\}}, this is the interval for the parameter $t$ (by default, it's the entire range of the window's x-coordinates),
        \item \opt{nbdots=40}, 
        \item \opt{discont=false},
        \item \opt{nbdiv=5},
        \item \opt{draw\_options=""}, string which will be passed as is to the instruction \drawcmd.
\end{itemize}
\end{itemize}

\subsubsection{Ddomain3}

\begin{itemize}
    \item The \cmd{ld.domain3(f, g, a, b \fac{, nbdots, discont, nbdiv})} function returns a list of complex numbers that represents the contour of the part of the plane bounded by the curve of the function \argu{f} and that of the function \argu{g}, searching for intersection points in the interval determined by \argu{a} and \argu{b}.

    \item The \cmd{g:Ddomain3(f, g, options)} method draws this contour. The argument \argu{options} is a table whose fields are the possible options. These are, with their default values:

    \begin{itemize}
        \item \opt{x= \{g:Xinf(),g:Xsup()\}}, this is the interval for the parameter $t$ (by default, it's the entire range of the window's x-coordinates),
        \item \opt{nbdots=40}, 
        \item \opt{discont=false},
        \item \opt{nbdiv=5},
        \item \opt{draw\_options=""}, string which will be passed as is to the instruction \drawcmd.
\end{itemize}
\end{itemize}

\begin{demo}{Floor function, Ddomain1 and Ddomain3 functions}
\begin{luadraw}{name=courbe}
local ld = luadraw
local Z = ld.cpx.Z
local g = ld.graph:new{ window={-5,5,-5,5}, bg="", size={10,10} }
local f = function(x) return (x-2)^2-2 end
local h = function(x) return 2*math.cos(x-2.5)-2.25 end
g:Daxes( {0,1,1},{grid=true,gridstyle="dashed", arrows="->"})
g:Filloptions("full","brown",0.3)
g:Ddomain1( math.floor, { x={-2.5,3.5} })
g:Filloptions("none","white",1); g:Lineoptions("solid","red",12)
g:Dstepfunction({ld.range(-5,5), ld.range(-5,4)},{draw_options="arrows={Bracket-Parenthesis}"})
g:Labelcolor("red")
g:Dlabel("Floor function",Z(-3,3),{node_options="fill=white"})
g:Ddomain3(f,h,{draw_options="fill=blue,fill opacity=0.6"})
g:Dcartesian(f, {x={0,5}, draw_options="blue"})
g:Dcartesian(h, {x={0,5}, draw_options="green"})
g:Show()
\end{luadraw}
\end{demo}

\subsubsection{Dinequalities}

\begin{itemize}

\item The function \cmd{ld.inequalities(constraints, x1, x2, y1, y2)} returns a list of complex numbers representing the boundary of the portion of the plane located within the cuboid $[x_1;x_2]\times[y_1;y_2]$ and satisfying the constraints expressed in the argument \argu{constraints}. This argument is a list of the form \code{\{f1,sg1,f2,sg2,...fn,sgn\}} where the \argu{fi} are functions ($f_i\colon x \mapsto f_i(x)\in \mathbf R$), and the \argu{sgi} are either \val{">"} or \val{"<"}. The first constraint is given by \argu{f1} and \argu{sg1}, and this constraint is either $y > f_1(x)$, or $y < f_1(x)$ depending on the value of \argu{sg1}. The same applies to the following constraints.

\item The \cmd{g:Dinequalities(constraints, options)} method allows you to draw this contour. The argument \argu{options} is a table whose fields are the possible options. These are, with their default values:
    \begin{itemize}
        \item \opt{view=\{g:Xinf(),g:Xsup(),g:Yinf(),g:Ysup()\}}, this is the window in which the resolution will take place. By default, it is the current 2D window.
        \item \opt{draw\_options=""}, a string that will be passed as is to the \drawcmd instruction.
        \item \opt{useclip=\false}, with the value \false the method uses the previous function (which calculates the contour of the solution), with the value \true the method does not calculate the contour but uses clips (one per constraint).
    \end{itemize}
\end{itemize}

\begin{demo}{Dinequalities}
\begin{luadraw}{name=Dinequalities}
local ld = luadraw
local Z = ld.cpx.Z
local g = ld.graph:new{window={-2,5,-4,9}, size={10,10}}
g:Daxes({0,1,1}, {grid=true, arrows="-Stealth", legend={"$x$","$y$"}})
local f = function(x) return x*x - 2*x - 2 end
local h = function(x) return x*x + 2 end
local k = function(x) return -x*x+5*x + 2 end
g:Dinequalities({f,'>', h,'<', k,'<'}, {draw_options="draw=none,fill=pink,fill opacity=0.6"})
g:Dcartesian(h, {draw_options="blue, line width=0.8pt"}); g:Dlabel("$C_h$",Z(3,8.75),{node_options="blue"})
g:Dcartesian(f, {draw_options="red, line width=0.8pt"}); g:Dlabel("$C_f$",Z(2,-2),{pos="E",node_options="red"})
g:Dcartesian(k, {draw_options="green, line width=0.8pt"}); g:Dlabel("$C_k$",Z(1,7),{node_options="green"})
g:Show()
\end{luadraw}
\end{demo}

\subsubsection{Dimplicit\_inequalities}

\begin{itemize}
    \item The function \cmd{ld.implicit\_inequality(f, sg, x1, x2, y1, y2, grid)} returns a \textbf{path} representing the boundary of the portion of the plane located within the tile $[x_1;x_2]\times[y_1;y_2]$ and satisfying the condition $f(x,y)>=0$ if \argu{sg}=">", or $f(x,y)<=0$ if \argu{sg}="<". The argument \argu{f} is a function of two variables \argu{f}$\colon (x,y)\mapsto f(x,y)\in \mathbf R$. The argument \argu{grid} is a table of two integers: \argu{grid}=\{\argu{n1},\argu{n2}\}, the first indicates the number of subdivisions of the interval $[x_1;x_2]$, and the second, the number of subdivisions of the interval $[y_1;y_2]$.
    
    \item The method \cmd{g:Dimplicit\_inequalities(constraints, options)} allows you to fill in a portion of the design that satisfies the constraints expressed in the argument \argu{constraints}. This argument is a list of the form \code{\{f1,sg1,f2,sg2,...fn,sgn\}} where the \argu{fi} are functions ($f_i\colon (x,y) \mapsto f_i(x,y)\in \mathbf R$), and the \argu{sgi} are either \val{">"} or \val{"<"}. The first constraint is given by \argu{f1} and \argu{sg1}, and this constraint is either $f_1(x,y)>0$ or $f_1(x,y)<0$ depending on the value of \argu{sg1}. The argument \argu{options} is a table whose fields are the possible options. These are, with their default values:

        \begin{itemize}
        \item \opt{view=\{g:Xinf(),g:Xsup(),g:Yinf(),g:Ysup()\}}, this is the window in which the resolution will take place. By default, it is the current 2D window.

        \item \opt{draw\_options=""}, a string that will be passed as is to the \drawcmd instruction.

        \item \opt{grid=\{50,50\}}: a table of two integers, the first indicating the number of subdivisions of the interval of $x$, and the second, the number of subdivisions of the interval of $y$.
    \end{itemize}
\end{itemize}

\textbf{NB}: This method does not calculate the contour but uses clipping (one per constraint).

\begin{demo}{The \cmd{g:Dimplicit\_inequalities} method}
\begin{luadraw}{name=implicit_inequalities}
local ld = luadraw
local cpx = ld.cpx
local Z = cpx.Z
local g = ld.graph:new{ window = {-3, 4, -3, 5}, size = {10, 10} }
local f1 = function(x,y) return -x*y+y^2-x^2-1  end
local f2 = function(x,y) return x^3-x-y^2+4 end
local filloptions = "draw=none,pattern= north west lines, pattern color=cyan"
g:Daxes()
g:Dimplicit_inequalities( {f1,'<',f2,">"}, {view={-2,5,-5,5}, draw_options=filloptions}) 
g:Dimplicit(f1, {draw_options="red,thick"})
g:Dimplicit(f2, {view={-2,5,-5,5}, draw_options="violet,thick"})
local eq = \luastringO{$\begin{cases}y^2-x^2-xy < 1\\ x^3-x+4 > y^2\end{cases}$}
g:Dlabel( eq, Z(2.25,1), {node_options="fill=white"})
g:Show()
\end{luadraw}
\end{demo}

\subsection{Points (Ddots) and Labels (Dlabel)}

\begin{itemize}
    \item The method for drawing one or more points is: \cmd{g:Ddots(dots \fac{, mark\_options})}.

\begin{itemize}
    \item The argument \argu{dots} can be either a single point (i.e., a complex number), a list (a table) of complex numbers, or a list of lists of complex numbers. The points are drawn in the current color of the line plot.
     \item The \argu{mark\_options} argument is an string (empty by default) that will be passed as is to the \drawcmd instruction (local modifications), for example:
\begin{TeXcode}
mark_options = "color=green, line width=1.2, scale=0.25"
\end{TeXcode}

    \item Two methods to globally modify the appearance of points:
    \begin{itemize}
        \item The \cmd{g:Dotstyle(style)} method defines the point style. The \argu{style} argument is a  string that defaults to \val{"*"}. The possible styles are those of the \emph{plotmarks} library.         
        \item The \cmd{g:Dotscale(scale)} method allows you to adjust the dot size. The \argu{scale} argument is a positive integer that defaults to $1$. It is used to multiply the default dot size. The current line width also affects the dot size. For "solid" dot styles (e.g., the \val{"triangle*"} style), the current fill style and color are used by the library.
    \end{itemize}
\end{itemize}

    \item The method for placing a label is:
\cmdln{g:Dlabel(text1, anchor1, options1, text2, anchor2, options2, ...)}

\begin{itemize}
    \item The arguments \argu{text1}, \argu{text2}, \ldots, are strings; they are the labels.
    \item The arguments \argu{anchor1}, \argu{anchor2}, \ldots, are complex numbers representing the anchor points of the labels.
    \item The arguments \argu{options1}, \argu{options2}, \ldots, allow you to locally define the label parameters; they are tables whose fields are the possible options. These are, with their default values:

    \begin{itemize}
        \item \opt{pos="center"}, indicates the position of the label relative to the anchor point. \val{"center"} (centered), \val{"N"} (north), \val{"NE"} (northeast), \val{"E"} (east), \val{"SE"} (southeast), \val{"S"} (south), \val{"SW"} (southwest), \val{"W"} (west), \val{"NW"} (northwest). By default, it is set to \val{"center"}, and in this case the label is centered on the anchor point,
        \item \opt{dist=0}, distance in cm between the label and its anchor point when \opt{pos} is not equal to \val{"center"},
        \item \opt{dir= \{\}}, this is a table of the form \code{\{dirX \fac{,dirY,dep}\}} which indicates the writing direction. The three values ​​\code{dirX}, \code{dirY}, and \code{dep} are three complex numbers representing three vectors. The first two indicate the writing direction, and the third indicates a translation of the label relative to the anchor point. The vector \code{dep} is zero by default, and the vector \code{dirY}, if absent, is equal to the vector \code{dirX} rotated 90 degrees in the clockwise direction. By default, the \opt{dir} option is equal to the current value of the writing direction,
        \item \opt{node\_options=""}, string (empty by default) intended to receive options that will be passed directly to TikZ in the \emph{node[]} instruction.
        \item The labels are drawn in the current color of the document text, but the color can be changed with the \opt{node\_options} argument, for example, by setting: \opt{node\_options="color=blue"}.

    \textbf{Warning}: The options chosen for a label also apply to subsequent labels if they are unchanged.
    \end{itemize}
\end{itemize}

Global options for labels:

\begin{itemize}
    \item The \cmd{g:Labelstyle(position)} method allows you to specify the position of the labels relative to the anchor points. The \argu{position} argument is a string that can be: \val{"N"} for north, \val{"NE"} for northeast, \val{"NW"} for northwest, or \val{"S"}, \val{"SE"}, \val{"SW"}. By default, it is set to \val{"center"}, and in this case the label is centered on the anchor point.
    
    \item The \cmd{g:Labelcolor(color)} method allows you to set the color of the labels. The \argu{color} argument is a string representing a color for TikZ. By default, the argument is an empty string, which represents the current color of the document.
    
    \item The \cmd{g:Labelangle(angle)} method allows you to specify an \argu{angle} (in degrees) for rotating the labels around the anchor point. This angle is zero by default.
    
    \item The \cmd{g:Labelsize(size)} method allows you to manage the size of the labels. The \argu{size} argument is a string that can be: \val{"tiny"}, or \val{"scriptsize"}, or \val{"footnotesize"}, etc. By default, the argument is an empty string, which represents the \val{"normalsize"} size.
    
    \item The \cmd{g:Labeldir(dir)} method allows you to manage the writing direction. The argument \argu{dir} is an table of the form \code{\{dirX \fac{,dirY,dep}\}} which indicates the writing direction. The three values ​​\code{dirX}, \code{dirY}, and \code{dep} are three complex numbers representing three vectors. The first two indicate the writing direction, and the third indicates a translation of the label relative to the anchor point. The vector \code{dep} is zero by default, and the vector \code{dirY}, if absent, is equal to the vector \code{dirX} rotated 90 degrees in the clockwise direction. When \argu{dir} is an empty list, this represents the usual writing direction.    
\end{itemize}

    \item The \cmd{g:Dlabeldot(text, anchor, options)} method allows you to place a label and draw the anchor point at the same time.
\begin{itemize}
    \item The \argu{text} argument is a string; it is the label.
    \item The \argu{anchor} argument is a complex representing the label's anchor point.
    \item The argument \argu{options} is a table whose fields are the possible options. These are identical to those of the \cmd{Dlabel} method, plus the \opt{mark\_options=""} option, which is a string that will be passed as is to the \drawcmd instruction when drawing the anchor point.
\end{itemize}
\end{itemize}

\subsection{Paths: Dpath, Dspline, and Dtcurve}\label{Dpath}

\subsubsection{What is a path}

A path is a table of complex numbers and instructions (in the form of strings). This table represents a succession of different "pieces", each piece being a sequence of data (2D points and sometimes numeric values) and ending with a string of characters that represents an instruction. The path is governed by the following rule: \par
\hfil\textbf{the last point of one piece is the first point of the next piece (it is therefore not repeated) }\hfil\par

\paragraph{Example:}
\begin{Luacode}
local Z = cpx.Z
local L = { Z(-3,2),"m",-3,-2,"l", 0,2,2,-1,"ca", 3,Z(3,3),0.5,"la",1,Z(-1,5),Z(-3,2),"b" }
\end{Luacode}
The path \emph{L} is composed of five pieces, which are:
    \begin{enumerate}
        \item \code{\{Z(-3,2),"m"\}}: there is a data element and the instruction \code{"m"} which means \emph{moveto}. This instruction does not actually draw the path, but it allows a new component to begin, starting at the first point $Z(-3,2)$ (the last point of the previous piece, if there is one, is not taken into account by this instruction; this is the only exception).

        \item \code{\{Z(-3,2),-3,-2,"l"\}}: the first point of the second piece is indeed $Z(-3,2)$ and not $-3$, because $Z(-3,2)$ is the last point of the previous piece. Therefore, there are three data points, and the instruction \code{"l"} which means \emph{lineto}, it is like executing the instruction \code{g:Dpolyline({Z(-3,2),-3,-2})}, so these three points are connected by a segment. The last point of this piece is $-2$.

        \item \code{\{-2,0,2,2,-1,"ca"\}}: the first point of the third piece is indeed $-2$ and not $0$, because $-2$ is the last point of the previous piece. There are five data points, and the instruction "ca" stands for \emph{circle arc}. It's as if we were executing the instruction \code{g:Darc(-2,0,2,2,-1)}, so the center is $0$, the arc goes from $-2$ to $2$ with a radius equal to $2$ and in a clockwise direction (last value $-1$). The last point of this piece is $2$.

        \item \code{\{2,3,Z(3,3),0.5,"la"\}}: the first point of the fourth piece is $2$ (not $3$). There are four data points, and the instruction \code{"la"} stands for \emph{line arc}. This is a polygonal line with rounded corners and a circular arc of radius $0.5$ (the value preceding the instruction). The points of this line are \code{\{2,3,Z(3,3)\}}, so there will be a rounding to $3$. The last point of this piece is $Z(3,3)$.

        \item \code{\{Z(3,3),1,Z(-1,5),Z(-3,2),"b"\}}: the first point of the fifth piece is $Z(3,3)$ (not $1$). The instruction \code{"b"} stands for \emph{bezier}, so we draw a Bézier curve from $Z(3,3)$ to $Z(-3,2)$. The other two points, $1$ and $Z(-1,5)$, are the first and second control points of the curve.
\end{enumerate}

Here is what this path gives:
\begin{demo}{Path example}
\begin{luadraw}{name=path_example}
local ld = luadraw
local Z = ld.cpx.Z
local g = ld.graph:new{window={-4,4,-0.5,3}, size={10,10}}
local L = { Z(-3,2),"m",-3,-2,"l",0,2,2,-1,"ca",3,Z(3,3),0.5,"la",1,Z(-1,5),Z(-3,2),"b" }
g:Dpath(L, "line width=0.8pt, blue")
g:Ddots({Z(-3,2),-3,-2,0,2,3,Z(3,3)})
g:Dlabel("$Z(-3,2)$",Z(-3,2),{pos="W"}, "$-3$",-3,{pos="S"}, "$-2$",-2,{},
"$0$",0,{}, "$2$",2,{}, "$3$",3,{}, "$Z(3,3)$",Z(3,3),{pos="E"})
g:Show()
\end{luadraw}
\end{demo}

\noindent\textbf{Note}: In the example above, you can replace the part: \code{Z(-3,2),"m",-3,-2,"l"}, with: \code{Z(-3,2),-3,-2,"l"} because there is no other part before the \emph{moveto}.

\noindent\textbf{Available commands and their syntax}, the word \emph{last} denotes the last point of the previous piece:
\begin{itemize}
  \item \code{z1,"m"} (moveto) starts a new component of the path at the point with affix $z_1$.
  \item \code{z1,...,zn,"l"} (lineto) draws the polyline \code{\{last,z1,...,zn\}}.
  \item \code{c1,c2,z2,"b"} (bézier) draws the Bézier curve \code{\{last,c1,c2,z2\}}, where $c_1$ and $c_2$ are the two control points.
  \item \code{z1,...,zn,"s"} (spline) draws the natural cubic spline through the points \code{\{last,z1,...,zn\}}.
  \item \code{z1,"c"} (circle) draws the circle with center $z_1$ passing through the point \emph{last}. There is another possible syntax for the circle: \code{z1,z2,"c"}, in which case the circle passing through the points \emph{last}, $z_1$, and $z_2$ is drawn.
  \item \code{z1,z2,r,sens,"ca"} (circular arc) draws a circular arc with center $z_1$ and radius $r$, going from \emph{last} to $z_2$, counterclockwise when \code{sens=1} (and therefore clockwise when \code{sens=-1}).
  \item \code{z1,z2,rx,ry,sens,inclinaison,"ea"} (elliptic arc) draws an elliptic arc with center $z_1$ going from \emph{last} to $z_2$; $rx$ and $ry$ are the two radii along the two axes of the ellipse; \code{inclinaison} is the angle in degrees between the first axis of the ellipse (the one corresponding to $rx$) and the horizontal. The parameter \code{inclinaison} is optional and defaults to $0$.
  \item \code{z1,rx,ry,inclinaison,"e"} (ellipse) draws the ellipse with center $z_1$ passing through \emph{last}; $rx$ and $ry$ are the two radii along the two axes of the ellipse; \code{inclinaison} is the angle in degrees between the first axis of the ellipse (the one corresponding to $rx$) and the horizontal. The parameter \code{inclinaison} is optional and defaults to $0$. When the point \emph{last} is not on this ellipse, a segment is drawn between this point and a point on the ellipse.
  \item \code{z1,...,zn,r,"la"} (line arc) draws the polyline \code{\{last,z1,...,zn\}} while replacing each “corner” by a circular arc of radius $r$.
  \item \code{z1,...,zn,r,"cla"} (closed line arc) same as the previous command, except that the polyline is closed.
  \item \code{"cl"} (closepath) this command is used alone; it closes the current component by drawing a segment joining the last point to the first point of the current component.
\end{itemize}


\subsubsection{Functions on paths}

\begin{itemize}
\item The function \cmd{ld.path(path \fac{, nbdots})} returns a polygonal line containing the points that make up the \argu{path}. The optional argument \argu{nbdots} is the minimum number of points calculated for each Bézier curve within the path; it defaults to the global variable \varglob{ld.bezier\_nbdots}, which is initialized to $12$. 

\item Conversely, the function \cmd{ld.polyline2path(L \fac{, close})}—where \argu{L} is a polygonal line (a list of complex numbers or a list of lists of complex numbers)—returns \argu{L} in the form of a path. The optional argument \argu{close} allows each component of the path to be closed or not (\false by default).

\item The function \cmd{ld.convpath(path)} converts the \argu{path} into another path using only the "m", "l", and "b" instructions (all other instructions are converted into Bézier curves). The function returns the new path; the advantage of this new path is that a transformation matrix can be applied to it (using the \cmd{ld.mtransform()} function).
\end{itemize}


\subsubsection{Draw a Path}

\begin{itemize}
    \item The \cmd{g:Dpath(path \fac{, draw\_options})} method draws the \argu{path} (which was described above) using Bézier curves as much as possible, including arcs, ellipses, etc. The \argu{draw\_options} argument is a string that will be passed directly to the \drawcmd instruction.

    \item The function \cmd{ld.spline(points, v1, v2)} returns a path (to be drawn with \cmd{Dpath}) of the cubic spline passing through the points of the argument \argu{points} (which must be a list of complex numbers). The arguments \argu{v1} and \argu{v2} are tangent vectors imposed at the ends (constraints); when these are equal to \nil (default value), a natural cubic spline (i.e., unconstrained) is calculated.

    \item The method \cmd{g:Dspline(points \fac{, v1, v2, draw\_options})} draws the spline described above. The argument \argu{draw\_options} is a string that will be passed directly to the instruction \drawcmd.

\begin{demo}{Path and Spline}
\begin{luadraw}{name=path_spline}
local ld = luadraw
local g = ld.graph:new{window={-5,5,-5,5},size={10,10},bg="Beige"}
local i = ld.cpx.I
local p = {-3+2*i,"m",-3,-2,"l",0,2,2,1,"ca",3,3+3*i,0.5,"la",1,-1+5*i,-3+2*i,"b",-1,"m",0,"c"}
g:Daxes( {0,1,1} )
g:Filloptions("full","blue!30",1,true); g:Dpath(p,"line width=0.8pt")
g:Filloptions("none")
local A,B,C,D,E = -4-i,-3*i,4,3+4*i,-4+2*i
g:Lineoptions(nil,"ForestGreen",12); g:Dspline({A,B,C,D,E},nil,-5*i) -- constraint in E
g:Ddots({A,B,C,D,E},"fill=white,scale=1.25")
g:Show()
\end{luadraw}
\end{demo}

    \item The function \cmd{ld.tcurve(L)} returns a curve passing through given points as a path, with tangent vectors (left and right) imposed at each point. The argument \argu{L} is a table of the form:
\begin{Luacode}
L = {point1,{t1,a1,t2,a2}, point2,{t1,a1,t2,a2}, ..., pointN,{t1,a1,t2,a2}}
\end{Luacode}
\argu{point1}, ..., \argu{pointN} are the interpolation points of the curve (complex numbers), and each of them is followed by a table of the form \code{\{t1,a1,t2,a2\}} which specifies the tangent vectors to the curve to the left of the point (with \argu{t1} and \argu{a1}) and to the right of the point (with \argu{t2} and \argu{a2}). The left tangent vector is given by the formula $V_g = t_1\times e^{ia_1\pi/180}$, so \argu{t1} represents the modulus and \argu{a1} is an argument \textbf{in degrees} of this vector. The same is true for \argu{t2} and \argu{a2} for the right tangent vector, \textbf{but these are optional}, and if not specified, they take the same values ​​as \argu{t1} and \argu{a1}.

Two consecutive points will be connected by a Bézier curve; the function calculates the control points to obtain the desired tangent vectors.

    \item The method \cmd{g:Dtcurve(L, options)} draws the path obtained by \argu{tcurve} described above. The argument \emph{options} is a table whose fields are the possible options. These are, with their default values:
    \begin{itemize}
        \item \opt{showdots=false}, this option allows you to draw or not the given interpolation points as well as the calculated control points, allowing for visualization of the constraints.
        \item \opt{draw\_options=""}, this is a string that will be passed directly to the \drawcmd instruction.
    \end{itemize}
    
\item A path consisting solely of Bézier curves can be drawn using the method:
\cmdln{g:Dbezier(L \fac{, draw\_options})}
where \argu{L} is a list of the form $\{A_1,c_1,c_2,A_2,c_3,c_4,A_3,\ldots\}$, the complex numbers $A_1$, $A_2$, \ldots, are the points on the curve, and the complex numbers $c_1$, $c_2$, $c_3$, $c_4$, \ldots, are the control points.    
\end{itemize}    

\begin{demo}{Interpolation curve with imposed tangent vectors}
\begin{luadraw}{name=tcurve}
local ld = luadraw
local g = ld.graph:new{window={-0.5,10.5,-0.5,6.5},size={10,10,0}}
local i = ld.cpx.I
local L = {
1+4*i,{2,-20},
2+3*i,{2,-70},
4+i/2,{3,0},
6+3*i,{4,15},
8+6*i,{4,0,4,-90}, -- angular point
10+i,{3,-15}}
g:Dgrid({0,10+6*i},{gridstyle="dashed"})
g:Daxes(nil,{limits={{0,10},{0,6}},originpos={"center","center"}, arrows="->"})
g:Dtcurve(L,{showdots=true,draw_options="line width=0.8pt,red"})
g:Show()
\end{luadraw}
\end{demo}

\subsection{Paths and Clipping: Beginclip() and Endclip()}

A path can be used for clipping via two methods: \par
\hfil \cmd{g:Beginclip(2D or 3D path \fac{, inverse})} and \cmd{g:Endclip(\fac{draw\_path, draw\_options})}.\hfil

The first opens a \emph{scope} group and passes the \argu{path} as an argument to the TikZ \emph{\textbackslash clip} function. The \argu{inverse} argument is a boolean that defaults to \false; when set to \true, the clipping is inverted—meaning only the area outside the \argu{path} is drawn—though this requires the path to be defined in a counter-clockwise direction.

The second method closes the \emph{scope} group; it is essential (otherwise, a compilation error will occur). The optional argument \argu{draw\_path} is a boolean (defaulting to \false); when set to \true, the \argu{path} is drawn after the group closes, and in this case, the optional argument \argu{draw\_options}, which must be a character string (empty by default), is passed to the \drawcmd command.

\begin{demo}{Clipping Example}
\begin{luadraw}{name=polygon_with_different_line_color_and_rounded_corners}
local ld = luadraw
local g = ld.graph:new{window={-5,5,-5,5},size={10,10}}
local i = ld.cpx.I
local Dcolored_polyreg = function(c,a,nb,r,wd,colors)
-- c=center, a=vertex, nb=number of sides, r=radius, wd=width in point, colors=list of colors
    local L = ld.polyreg(c,a,nb)
    table.append(L,{r,"cla"}) --polygon with rounded corners (radius=r)
    local angle = 360/nb
    local b = a
    for k = 1, nb do
        a = b; b = ld.rotate(a,angle,c)
        g:Beginclip({2*a-c,c,2*b-c,"l"}) -- definition of an angular sector for clipping
        g:Dpath(L,"line width="..wd.."pt,color="..colors[k])
        g:Endclip()
    end
end
Dcolored_polyreg(3+2*i,5+2*i,5,0.8,12,{"red","blue","orange","green","yellow"}) -- pentagon
Dcolored_polyreg(-2.5-2*i,-5-2*i,7,1,24,ld.getpalette(ld.palGasFlame,7)) -- heptagon
g:Show()
\end{luadraw}
\end{demo}


\subsection{Axes and Grids}

Global variables used for axes and grids:
\begin{itemize}
    \item \varglob{ld.maxGrad = 100}: Maximum number of tick marks on an axis.
    \item \varglob{ld.defaultlabelshift = 0.1875}: When a grid is drawn with the axes (option \opt{grid=true}), the labels are automatically shifted along the axis using this variable.
    \item \varglob{ld.defaultxylabelsep = 0}: Sets the default distance between labels and tick marks.
    \item \varglob{ld.defaultlegendsep = 0.2}: Sets the default distance between the legend and the axis.
    \item \varglob{ld.digits = 4}: Default number of decimal places in string conversions; terminal $0$s are removed.     
    \item \varglob{ld.dollar = true}: to add dollars around the tick labels.
    \item \varglob{ld.siunitx = false}: with the value \true, the labels are formatted with the macro \verb|\num{..}| of the \emph{siunitx} package, which allows you to use certain options of this package, such as replacing the decimal point with a comma by doing: \par
\begin{TeXcode}
\usepackage[local=FR]{siunitx}
\end{TeXcode}
or by doing:
\begin{TeXcode}
\usepackage{siunitx}
\sisetup{output-decimal-marker={,}}
\end{TeXcode}
\end{itemize}
For axes, in both 2D and 3D, all labels are formatted as strings with the \cmd{ld.num(x)} function. This transforms the number $x$ into a string \emph{str} with the number of decimal places set by the global variable \varglob{ld.digits}. When the \varglob{ld.siunitx} variable has the value \true, the function returns \val{"\textbackslash num\{str\}"}, otherwise it simply returns \emph{str}. This also applies to 3D axes. Here is the code for this function:
\begin{Luacode}
function ld.num(x) -- x is a real, returns a string
    local rep = ld.strReal(x) -- conversion to string with digits decimals max
    if ld.siunitx then rep = "\\num{"..rep.."}" end --needs \usepackage{siunitx}
    return rep
end
\end{Luacode}

\subsubsection{Daxes}
%\def\opt#1{\textcolor{blue}{\texttt{#1}}}%
The axes are plotted using the method \cmd{g:Daxes(\fac{\{A, xstep, ystep\}, options})}.
\begin{itemize}
    \item The first argument specifies the intersection point of the two axes (this is the complex \argu{A}), the graduation spacing on the $x$-axis (this is \argu{xstep}), and the graduation spacing on $y$-axis (this is \argu{ystep}). By default, the point \argu{A} is the origin $Z(0,0)$, and both steps are equal to $1$.
    \item The argument \argu{options} is a table whose fields are the possible options. Here are these options with their default values:

    \begin{itemize}
        \item \opt{showaxe={1,1}}. This option specifies whether or not the axes should be plotted ($1$ or $0$). The first value is for the $x$-axis and the second for the $y$-axis.
    
        \item \opt{arrows="-"}. This option allows you to add an arrow to the axes (no arrow by default; enter \val{"->"}for example to add an arrow).
    
        \item \opt{limits={"auto","auto"}}. This option specifies the extent of the two axes (first value for $x$-axis, second value for $y$-axis). The value \val{"auto"} means that it is the entire line, but you can specify the extreme abscissas, for example: \opt{limits={{-4,4},"auto"}}.
        \item \opt{gradlimits=\{"auto","auto"\}}. This option allows you to specify the range of the graduations on both axes (first value for $x$-axis, second value for $y$-axis). The value \val{"auto"} means that it is the entire line, but you can specify the extreme graduations, for example: \opt{gradlimits=\{\{-4.4\},\{-2.3\}\}}.
        \item \opt{unit=\{"",""\}}. This option allows you to specify the range of the graduations on the axes. The default value means that the step value should be taken (\argu{xstep} on $x$-axis, or \argu{ystep} on $y$-axis), EXCEPT when the option \opt{labeltext} is not the empty string, in which case \opt{unit} takes the value $1$.
        \item \opt{nbsubdiv=\{0,0\}}. This option specifies the number of subdivisions between two main ticks on the axis.
        \item \opt{tickpos=\{0.5,0.5\}}. This option specifies the position of the ticks relative to each axis. These are two numbers between $0$ and $1$. The default value of $0.5$ means they are centered on the axis. ($0$ and $1$ represent the ends).
        \item \opt{tickdir=\{"auto","auto"\}}. This option specifies the direction of the ticks on the axis. This direction is a non-zero vector (complex number). The default value \val{"auto"} means the ticks are orthogonal to the axis.
        \item \opt{xyticks=\{0.2,0.2\}}. This option specifies the length of the ticks on the axis.
        \item \opt{xylabelsep=\{0,0\}}. This option specifies the distance between the labels and the tick marks on the axis.
        \item \opt{originpos=\{"right","top"\}}. This option specifies the position of the label at the origin on the axis. Possible values ​​are:  \val{"none"}, \val{"center"}, \val{"left"}, \val{"right"} for $x$-axis, and \val{"none"}, \val{"center"}, \val{"bottom"}, \val{"top"} for $y$-axis.
        
        \item \opt{originnum=\{A.re,A.im\}}. This option specifies the value at the origin point of the graduations (graduation number $0$).
    
    The formula that defines the label at tick mark number $n$ is: 
\codeln{(originnum + unit*n)"labeltext"/labelden.}

        \item \opt{originloc=A}. This option specifies the origin point of the graduations.
        
        \item \opt{legend=\{"",""\}}. This option allows you to specify a legend for the axis.
        \item \opt{legendpos=\{0.975,0.975\}}. This option specifies the position (between $0$ and $1$) of the legend relative to each axis.
        \item \opt{legendsep=\{ld.defaultlegendsep,ld.defaultlegendsep\}}. This option specifies the distance between the legend and the axis. The legend is on the other side of the axis from the graduations.
        \item \opt{legendangle=\{"auto","auto"\}}. This option specifies the angle (in degrees) that the legend should make for the axis. The default value \val{"auto"} means that the legend must be parallel to the axis if the \opt{labelstyle} option is also set to \val{"auto"}, otherwise the legend is horizontal.
        \item \opt{legendstyle=\{"auto","auto"\}}. Specifies the label style for the legends; with the value \val{"auto"}, it is determined automatically; otherwise, the following values ​​can be used:  \val{"N"}, \val{"NW"}, \val{"W"}, \val{"SW"}, \val{"S"}, \val{"SE"}, \val{"E"}, \val{"NE"}.
        \item \opt{labelpos=\{"bottom","left"\}}. This option specifies the position of the labels relative to the axis. For the $x$-axis, the possible values ​​are: \val{"none"}, \val{"bottom"} or \val{"top"}, for the $y$-axis it is:  \val{"none"}, \val{"right"} or \val{"left"}.

        \item \opt{labelden=\{1,1\}}. ​​This option specifies the denominator of the labels (integer) for the axis. Remember that the formula that defines the label at graduation number $n$ is: \codeln{(originnum + unit*n)"labeltext"/labelden.}
        \item \opt{labeltext=\{"",""\}}. This option defines the text that will be added to the numerator of the labels for the axis.
        \item \opt{labelstyle=\{"S","W"\}}. This option sets the label style for each axis. Possible values ​​are \val{"auto"}, \val{"N"}, \val{"NW"}, \val{"W"}, \val{"SW"}, \val{"S"}, \val{"SE"}, \val{"E"}, \val{"NE"}.
        \item \opt{labelangle=\{0,0\}}. This option sets the angle of the labels in degrees from the horizontal for each axis.
        \item \opt{labelcolor=\{"",""\}}. This option allows you to choose a color for the labels on each axis. The empty string represents the default color.
        
        \item \opt{labelshift=\{0,0\}}. This option allows you to define a systematic offset for the labels on the axis (offset along the axis). With the option \opt{grid=false} the default values ​​are $0$ and $0$, but with the option \opt{grid=true} the default is \opt{labelshift=\{ld.defaultlabelshift,ld.defaultlabelshift\}} where \varglob{ld.defaultlabelshift} is a global variable initialized to $0.1875$.
        
        \item \opt{xynode\_options=""}. String of characters that will be passed as is to the instruction \verb|\node{}| for all labels on both axes (but not for legends).
        
        \item \opt{xnode\_options=xynode\_options}. String of characters that will be passed as is to the instruction \verb|\node{}| for all labels on the $x$ axis (but not for the legend).
        
        \item \opt{ynode\_options=xynode\_options}. String of characters that will be passed as is to the instruction \verb|\node{}| for all labels on the $y$ axis (but not for the legend).
        
        \item \opt{nbdeci=\{2,2\}}. This option specifies the number of decimal places for numeric values ​​on the axis.
        
         \item \opt{use\_siunitx=\{ld.siunitx,ld.siunitx\}}. This option specifies whether numeric values ​​should be formatted using the \emph{siunitx} package; the default value is that of the global variable \varglob{ld.siunitx}, which is \false by default.
         
        \item \opt{myxlabels=""}. This option allows you to impose custom labels on the $x$-axis. When any are present, the value passed to the option must be a list of the type: \code{\{pos1,"text1",pos2,"text2",\ldots\}}. The number \argu{pos1} represents an abscissa in the \code{(A,xstep)} coordinate system, which corresponds to the affix point \code{A+pos1*xstep}.

        \item \opt{myylabels=""}. This option allows you to impose custom labels on the $y$-axis. When any are present, the value passed to the option must be a list of the type: \code{\{pos1,"text1",pos2,"text2",\ldots\}}. The number \argu{pos1} represents an abscissa in the coordinate system \code{(A,i*ystep)}, which corresponds to the affix point \code{A+pos1*ystep*i}.
        
        \item \opt{grid=false}. This option allows you to add a grid or not.

        \item \opt{showlines=\{\true,\true\}}. When \opt{grid=\true}, this option allows you to display or hide the vertical lines of the grid (corresponding to the $x$ axis) as well as the horizontal lines of the grid (corresponding to the $y$ axis).

        \item \opt{drawbox=false}. This option draws the axes as a box; in this case, the graduations are on the left and bottom sides.

        \item \opt{gridstyle="solid"}. This option sets the line style for the primary grid.
        \item \opt{subgridstyle="solid"}. This option sets the line style for the secondary grid. A secondary grid appears when there are subdivisions on one of the axes.
        \item \opt{gridcolor="gray"}. This sets the color of the primary grid.
        \item \opt{subgridcolor="lightgray"}. This sets the color of the secondary grid.
        \item \opt{gridwidth=4}. Line thickness of the primary grid (which is 0.4pt).
        \item \opt{subgridwidth=2}. Line thickness of the secondary grid (which is 0.2pt).
    \end{itemize}
\end{itemize}


\begin{demo}{Example with axes with grid}
\begin{luadraw}{name=axes_grid}
local ld = luadraw
local g = ld.graph:new{window={-6.5,6.5,-3.5,3.5}, size={10,10,0}}
local i, pi, a = ld.cpx.I, math.pi, math.sqrt(2)
local f = function(x) return 2*a*math.sin(x) end
g:Labelsize("footnotesize"); g:Linewidth(8)
g:Daxes({0,pi/2,a},{labeltext={"\\pi","\\sqrt{2}"}, labelden={2,1},nbsubdiv={1,1},grid=true,arrows="->"})
g:Lineoptions("solid","Crimson",12); g:Dcartesian(f, {x={-2*pi,2*pi}})
g:Show()
\end{luadraw}
\end{demo}

\subsubsection{DaxeX and DaxeY}

The methods \cmd{g:DaxeX(\fac{\{A, xstep\}, options})} and \cmd{g:DaxeY(\fac{\{A, ystep\}, options})} allow you to plot the axes separately.
\begin{itemize}
    \item The first argument specifies the point serving as the origin (the complex number \argu{A}) and the step size of the tick marks on the axis. By default, the point \argu{A} is the origin $Z(0,0)$, and the step size is equal to $1$.
    \item The argument \argu{options} is a table specifying the possible options. Here are these options with their default values:
    \begin{itemize}
        \item \opt{showaxe=1}. This option specifies whether or not the axis should be plotted ($1$ or $0$).
        \item \opt{arrows="-"}. This option allows you to add an arrow to the axis (no arrow by default; enter \val{"->"} to add an arrow).

        \item \opt{limits="auto"}. This option allows you to specify the range of the two axes. The value "auto" means that it is the entire line, but you can specify the extreme abscissas, for example: \opt{limits=\{-4.4\}}.
        \item \opt{gradlimits="auto"}. This option allows you to specify the range of the graduations on both axes. The value \val{"auto"} means that it is the entire line, but you can specify the extreme graduations, for example: \opt{gradlimits=\{-2.3\}}.
        \item \opt{unit=""}. This option allows you to specify the range of the graduations on the axis. The default value ("") means to take the step value, EXCEPT when the \opt{labeltext} option is not the empty string, in which case \opt{unit} takes the value $1$.
        \item \opt{nbsubdiv=0}. This option specifies the number of subdivisions between two main tick marks.
        \item \opt{tickpos=0.5}. This option specifies the position of the tick marks relative to the axis. These are two numbers between $0$ and $1$. The default value of $0.5$ means they are centered on the axis. ($0$ and $1$ represent the ends).
        \item \opt{tickdir="auto"}. This option indicates the direction of the tick marks on the axis. This direction is a non-zero (complex) vector. The default value "auto" means the tick marks are orthogonal to the axis.
        \item \opt{xyticks=0.2}. This option specifies the length of the tick marks.
        \item \opt{xylabelsep=0}. This option specifies the distance between the labels and the tick marks.
        \item \opt{originpos="center"}. This option specifies the position of the label at the origin on the axis. Possible values ​​are:  \val{"none"}, \val{"center"}, \val{"left"}, \val{"right"} for $x$-axis, and \val{"none"}, \val{"center"}, \val{"bottom"}, \val{"top"} for $y$-axis.
        \item \opt{originnum=A.re} for $x$-axis and \opt{originnum=A.im} for $y$-axis. This option specifies the value of the tick mark at the origin (tick mark number $0$).
    
    The formula that defines the label at tick mark number $n$ is: 
\codeln{(originnum + unit*n)"labeltext"/labelden.}
    
        \item \opt{legend=""}. This option allows you to specify a legend for the axis.

        \item \opt{legendpos=0.975}. This option specifies the position (between $0$ and $1$) of the legend relative to the axis.

        \item \opt{legendsep=ld.defaultlegendsep}. This option specifies the distance between the legend and the axis. The legend is on the other side of the axis from the graduations.

        \item \opt{legendangle="auto"}. This option specifies the angle (in degrees) that the legend should make for the axis. The default value \val{"auto"} means that the legend must be parallel to the axis if the \opt{labelstyle} option is also set to \val{"auto"}, otherwise the legend is horizontal.

        \item \opt{legendstyle="auto"}. Specifies the label style for the legend. With the value \emph{"auto"}, the style is determined automatically; otherwise, the following values ​​can be used:  \val{"N"}, \val{"NW"}, \val{"W"}, \val{"SW"}, \val{"S"}, \val{"SE"}, \val{"E"}, \val{"NE"}.

        \item \opt{labelpos="bottom"} for $x$-axis and \opt{labelpos="left"} for $y$-axis. This option specifies the position of the labels relative to the axis. For the $x$-axis, the possible values ​​are: \val{"none"}, \val{"bottom"} or \val{"top"}, for the $y$-axis it is:  \val{"none"}, \val{"right"} ou \val{"left"}.

        \item \opt{labelden=1}. This option specifies the denominator of the labels (integer) for the axis. The formula that defines the label at graduation number $n$ is: 
\codeln{(originnum + unit*n)"labeltext"/labelden.}

        \item \opt{labeltext=""}. This option defines the text that will be added to the numerator of the labels.

        \item \opt{labelstyle="S"} for $x$-axis and \opt{labelstyle="W"} for $y$-axis. This option defines the style of the labels. The possible values ​​are: \val{"auto"}, \val{"N"}, \val{"NW"}, \val{"W"}, \val{"SW"}, \val{"S"}, \val{"SE"}, \val{"E"}, \val{"NE"}.

        \item \opt{labelangle=0}. This option sets the angle of the labels in degrees from the horizontal.

        \item \opt{labelcolor=""}. This option allows you to choose a color for the labels. The empty string represents the current text color.

        \item \opt{labelshift=0}. This option allows you to set a systematic offset for labels on the axis (along-axis offset).

        \item \opt{nbdeci=2}. This option specifies the number of decimal places for numeric labels.

        \item \opt{use\_siunitx=ld.siunitx}. This option specifies whether numeric values ​​should be formatted using the \emph{siunitx} package; the default value is that of the global variable \varglob{ld.siunitx}, which is \false by default.

        \item \opt{mylabels=""}. This option allows you to impose custom labels. When there are any, the value passed to the option must be a list of the type:  \code{\{pos1,"text1",pos2,"text2",\ldots\}}. The number \argu{pos1} represents an abscissa in the coordinate system \code{(A,xstep)} for $x$-axis, or \code{(A,ystep*i)} for $y$-axis, which corresponds to the affix point \code{A+pos1*xstep} for $x$-axis, and \code{A+pos1*ystep*i} for $y$-axis.
    \end{itemize}
\end{itemize}

\subsubsection{Dgradline}

The axis plotting methods are based on the method \cmd{g:Dgradline(\{A, u\}, options)}, where \argu{\{A,u\}} represents the line passing through \argu{A} (a complex number) and directed by the vector \argu{u} (a non-zero complex number). The pair $(A,u)$ serves as a reference point on this line (and orients this line), so each point $M$ on this line has an abscissa $x$ such that $M=A+xu$. This method allows you to draw this graduated line. The argument \argu{options} is a table specifying the possible options, which are (with their default values):
\begin{itemize}
    \item \opt{showaxe=1}. This option specifies whether or not the axis should be plotted ($1$ or $0$).

     \item \opt{arrows="-"}. This option allows you to add an arrow to the axis (no arrow by default; enter \val{"->"} to add an arrow).

    \item \opt{limits="auto"}. This option allows you to specify the range of the two axes. The value \val{"auto"} means that it is the entire line, but you can specify the extreme abscissas, for example: \opt{limits=\{-4.4\}}.

    \item \opt{gradlimits="auto"}. This option allows you to specify the range of the graduations on both axes. The value \val{"auto"} means that it is the entire line, but you can specify the extreme graduations, for example: \opt{gradlimits=\{-2.3\}}.

    \item \opt{unit=1}. This option allows you to specify the number of graduations on the axis.

    \item \opt{nbsubdiv=0}. This option specifies the number of subdivisions between two main ticks.

    \item \opt{tickpos=0.5}. This option specifies the position of the ticks relative to the axis. These are two numbers between $0$ and $1$. The default value of $0.5$ means they are centered on the axis. ($0$ and $1$ represent the ends).

    \item \opt{tickdir="auto"}. This option specifies the direction of the ticks on the axis. This direction is a non-zero vector (complex number). The default value \val{"auto"} means the ticks are orthogonal to the axis.

    \item \opt{xyticks=0.2}. This option specifies the length of the ticks.

    \item \opt{xylabelsep=defaultxylabelsep}. This option specifies the distance between the labels and the tick marks. \varglob{defaultxylabelsep} is a global variable with a default value of $0$.

    \item \opt{originpos="center"}. This option specifies the position of the label at the origin on the axis. Possible values ​​are: \val{"none"}, \val{"center"}, \val{"left"}, \val{"right"}.

    \item \opt{originnum=0}. This option specifies the value of the tick mark at the origin $A$ (tick mark number $0$).
    
The formula that defines the label at graduation number $n$ (at point $A+nu$) is: 
\codeln{(originnum + unit*n)"labeltext"/labelden.}

    \item \opt{legend=""}. This option allows you to specify a legend for the axis.

    \item \opt{legendpos=0.975}. This option specifies the position (between $0$ and $1$) of the legend relative to the axis.

    \item \opt{legendsep=ld.defaultlegendsep}. This option specifies the distance between the legend and the axis. The legend is on the other side of the axis from the graduations; \varglob{ld.defaultlegendsep} is a global variable that defaults to $0.2$.

    \item \opt{legendangle="auto"}. This option specifies the angle (in degrees) that the legend should form for the axis. The default value \val{"auto"} means that the legend must be parallel to the axis if the \opt{labelstyle} option is also set to \val{"auto"}, otherwise the legend is horizontal.

    \item \opt{legendstyle="auto"}. Specifies the label style for the legend. With the value \emph{"auto"}, the style is determined automatically; otherwise, the following values ​​can be used: \val{"N"}, \val{"NW"}, \val{"W"}, \val{"SW"}, \val{"S"}, \val{"SE"}, \val{"E"}, \val{"NE"}.

    \item \opt{labelpos="bottom"}. This option specifies the position of the labels relative to the axis. The possible values ​​are: \val{"none"}, \val{"bottom"}, or \val{"top"}. This position also determines the position of the legend: on the opposite side of the axis.

    \item \opt{labelden=1}. This option specifies the denominator of the labels (integer) for the axis. The formula that defines the label at graduation number $n$ is: 
\codeln{(originnum + unit*n)"labeltext"/labelden.}

    \item \opt{labeltext=""}. This option defines the text that will be added to the numerator of the labels.

     \item \opt{labelstyle="auto"}. This option defines the label style. Possible values ​​are:  \val{"auto"}, \val{"N"}, \val{"NW"}, \val{"W"}, \val{"SW"}, \val{"S"}, \val{"SE"}, \val{"E"}, \val{"NE"}.

    \item \opt{labelangle=0}. This option defines the angle of the labels in degrees from the horizontal.

    \item \opt{labelcolor=""}. This option allows you to choose a color for the labels. The empty string represents the current text color.

    \item \opt{labelshift=0}. This option allows you to define a systematic offset for the labels on the axis (along axis offset).

    \item \opt{nbdeci=2}. This option specifies the number of decimal places for numeric labels.

    \item \opt{use\_siunitx=ld.siunitx}. This option specifies whether numeric values ​​should be formatted using the \emph{siunitx} package; the default value is that of the global variable \varglob{ld.siunitx}, which is \false by default.

    \item \opt{mylabels=""}. This option allows you to impose custom labels. When any are present, the value passed to the option must be a list of the type: \code{\{x1,"text1",x2,"text2",\ldots\}}. The numbers \argu{x1}, \argu{x2}, \ldots, represent abscissas in the $(A,u)$ coordinate system.
\end{itemize}

\begin{demo}{Examples of numbered lines}
\begin{luadraw}{name=gradline}
local ld = luadraw
local g = ld.graph:new{window={-5,5,-5,5},size={10,10}}
g:Labelsize("footnotesize")
local i = ld.cpx.I
g:Dgradline({3.25*i,1+i/2}, {limits={-4,4}, legend="Axe", legendpos=0.5, arrows="-stealth"})
g:Dgradline({-3,1}, {legend="demo", labeltext="\\pi", labelden=3, unit=2, nbsubdiv=1, arrows="-latex"})
g:Dgradline({3-4*i,-1.25+i/5}, {legend="A", labelstyle="N", gradlimits={-1,5},
nbsubdiv=3, unit=1.411, nbdeci=3, arrows="-Latex"})
g:Show()
\end{luadraw}
\end{demo}

\subsubsection{Dgrid}

The \cmd{g:Dgrid(\{A, B\}, options)} method allows you to draw a grid.
\begin{itemize}
    \item The first argument is mandatory; it specifies the lower-left corner (this is the \argu{A} complex number) and the upper-right corner (this is the \argu{B} complex number) of the grid.
    \item The \argu{options} argument is a table specifying the possible options. Here are these options with their default values:
\begin{itemize}
    \item \opt{unit=\{1,1\}}. ​​This option defines the units on the axes for the main grid.
    \item \opt{showlines=\{\true,\true\}}. This option allows you to display or hide the vertical lines of the grid (corresponding to the $x$ axis) as well as the horizontal lines of the grid (corresponding to the $y$ axis).
    \item \opt{gridwidth=4}. This option defines the line thickness of the main grid (0.4pt by default).
    \item \opt{gridcolor="gray"}. Grid color of the main grid.
    \item \opt{gridstyle="solid"}. Line style for the primary grid.
    \item \opt{nbsubdiv={0,0}}. Number of subdivisions (for each axis) between two lines of the primary grid. These subdivisions determine the secondary grid.
    \item \opt{subgridcolor ="lightgray"}. Color of the secondary grid.
    \item \opt{subgridwidth=2}. Line thickness of the secondary grid (0.2pt by default).
    \item \opt{subgridstyle="solid"}. Line style for the secondary grid.
    \item \opt{originloc=A}. Location of the grid origin.
\end{itemize}
\end{itemize}

\paragraph{Example:} It is possible to work in a non-orthogonal coordinate system. Here is an example where the $x$-axis is retained, but the first bisector becomes the new $y$-axis. We modify the graph's transformation matrix. Based on this modification, the affixes represent the coordinates in the new coordinate system.

\begin{demo}{Example of a non-orthogonal coordinate system}
\begin{luadraw}{name=axes_non_ortho}
local ld = luadraw
local g = ld.graph:new{window={-5.25,5.25,-4,4},size={10,10}}
local i, pi, Z = ld.cpx.I, math.pi, ld.cpx.Z
local f = function(x) return 2*math.sin(x) end
g:Setmatrix({0,1,1+i}); g:Labelsize("small")
g:Dgrid({-5-4*i,5+4*i},{gridstyle="dashed"})
g:Daxes({0,1,1}, {arrows="-Stealth"})
g:Lineoptions("solid","ForestGreen",12); g:Dcartesian(f,{x={-5,5}})
g:Dcircle(0,3,"Crimson")
g:DlineEq(1,0,3,"Navy") -- line with equation x=-3
g:Lineoptions("solid","black",8); g:DtangentC(f,pi/2,1.5,"<->")
g:Dpolyline({pi/2,pi/2+2*i,2*i},"dotted")
g:Ddots(Z(pi/2,2))
g:Dlabeldot("$\\frac{\\pi}2$",pi/2,{pos="SW"})
g:Show()
\end{luadraw}
\end{demo}

\subsubsection{Dgradbox}

The \cmd{g:Dgradbox(\{A, B, xstep, ystep\}, options)} method allows you to draw a graduated box.
\begin{itemize}
    \item The first argument is mandatory; it specifies the lower-left corner (this is the \argu{A} complex number) and the upper-right corner (this is the \argu{B} complex number) of the box, as well as the step on each axis.
    \item The \argu{options} argument is a table specifying the possible options. These are the same as for the axes, except for some default values. In addition, the following option is added: \opt{title=""}, which allows you to add a title at the top of the box; however, be careful to leave enough space for this.
\end{itemize}

\begin{demo}{Using Dgradbox}
\begin{luadraw}{name=gradbox}
local ld = luadraw
local g = ld.graph:new{window={-5,4,-5.5,5},size={10,10}}
local i, pi = ld.cpx.I, math.pi
local h = function(x) return x^2/2-2 end
local f = function(x) return math.sin(3*x)+h(x) end
g:Dgradbox({-pi-4*i,pi+4*i,pi/3,1},{grid=true,originloc=0, originnum={0,0}, labeltext={"\\pi",""},
labelden={3,1}, title="\\textbf{Title}", legend={"Legend $x$","Legend $y$"}})
g:Saveattr(); g:Viewport(-pi,pi,-4,4) -- The view is limited (clip).
g:Filloptions("full","blue",0.6); g:Linestyle("noline"); g:Ddomain2(f,h,{x={-pi/2,2*pi/3}})
g:Filloptions("none",nil,1); g:Lineoptions("solid",nil,8); g:Dcartesian(h,{x={-pi,pi}, draw_options="DarkBlue"})
g:Dcartesian(f,{x={-pi,pi},draw_options="Crimson"})
g:Restoreattr()
g:Show()
\end{luadraw}
\end{demo}

\subsection{Set Drawings (Venn Diagrams)}

\subsubsection{Drawing a Set}

The function \cmd{ld.set(center \fac{, angle, scale})} returns a path representing a set (egg-shaped), with the center being \argu{center} (complex number), the argument \argu{angle} representing the inclination (in degrees) of the set's vertical axis ($0$ by default), and the argument \argu{scale} being a scale factor to modify the size of the set ($1$ by default). Such a path can be drawn with the method \cmd{g:Dpath()}.

\begin{demo}{Drawing a Set}
\begin{luadraw}{name=set}
local ld = luadraw
local g = ld.graph:new{window={-5.25,5.25,-5,5},size={10,10}}
local i = ld.cpx.I
local A, B, C = ld.set(i,0), ld.set(-2-i,25), ld.set(2-i,-25)
g:Fillopacity(0.3)
g:Dpath(A,"fill=orange"); g:Dpath(B,"fill=blue")
g:Dpath(C,"fill=green")
g:Fillopacity(1)
g:Dlabel("$A$",5*i,{pos="N"},"$B$",-4+3*i,{pos="W"},"$C$",4+3*i,{pos="E"})
g:Show()
\end{luadraw}
\end{demo}

\subsubsection{Operations on Sets}

Let $C_1$ and $C_2$ be two lists of complex numbers representing the contour of two sets (simple closed curves, all in one piece). There are three possible operations:
\begin{itemize}
    \item The function \cmd{ld.cap(C1, C2)} returns a list of complex numbers representing the contour of the intersection of the sets corresponding to $C_1$ and $C_2$.
    \item The function \cmd{ld.cup(C1, C2)} returns a list of complex numbers representing the contour of the union of the sets corresponding to $C_1$ and $C_2$.
    \item The function \cmd{ld.setminus(C1, C2)} returns a list of complex numbers representing the contour of the difference of the sets corresponding to $C_1$ and $C_2$ ($C_1\setminus C_2$).
\end{itemize}
The result of these operations, being a list of complex numbers, can be drawn with the \cmd{g:Dpolyline()} method.

\begin{demo}{Set Operations}
\begin{luadraw}{name=cap_and_cup}
local ld = luadraw
local g = ld.graph:new{window={-5.5,5.5,-5,5},size={10,10}}
local i = ld.cpx.I
local A, B, C = ld.set(i,0), ld.set(-2-i,25), ld.set(2-i,-25)
g:Fillopacity(0.3)
g:Dpath(A,"fill=orange"); g:Dpath(B,"fill=blue"); g:Dpath(C,"fill=green")
g:Fillopacity(1)
local C1, C2, C3 = ld.path(A), ld.path(B), ld.path(C) -- conversion: path -> list of complex numbers
local I = ld.cap(ld.cup(C1,C2),C3)
g:Linecolor("red"); g:Filloptions("full","white")
g:Dpolyline(I,true,"line width=0.8pt,fill opacity=0.8")
g:Dlabel("$A$",5*i,{pos="N"},"$B$",-4+3*i,{pos="W"},"$C$",4+3*i,{pos="E"},
"$(A\\cup B) \\cap C$",-i,{pos="NE",node_options="red,draw"})
g:Show()
\end{luadraw}
\end{demo}

\paragraph{NB}: The result is not always satisfactory when the contours become too complex, or when the contours share common sections.


\subsection{Importing an Image}

\subsubsection{Placing an Image in the Graphic}

To import an image into the current graphic (and possibly draw on top of it), use the method:\cmdln{g:Dimage( filename, anchor, options)}
where:
\begin{itemize}
    \item \argu{filename} is the full name of the image file. The image will be displayed using the \verb|\includegraphics[]{}| command within a node.
    \item \argu{anchor} is a complex number representing the anchor point of the image in the graphic.
    \item \argu{options} is a table whose fields define the display parameters (with their default values):
    \begin{itemize}
        \item \opt{pos="center"} specifies the position of the image relative to the anchor point, following the same principle as for labels. Possible values are:  \val{"center"}, \val{"N"}, \val{"NE"}, \val{"E"}, \val{"SE"}, \val{"S"}, \val{"SW"}, \val{"W"}, \val{"NW"}.
        
        \item \opt{name=""} optionally assigns a name to the node that will be created. This name can later be referenced in TikZ.
        
        \item \opt{graphics\_options=""} which is a string containing options for the \verb|\includegraphics| command (no default options).
        
        \item \opt{matrix=nil} is a table representing an affine transformation matrix. This matrix is applied locally to the created node (for which the origin is the anchor point). Note that the current global 2D transformation matrix of the graphic is also taken into account by this method.
        
        \item \opt{node\_options=""} which is a string containing options for the \verb|\node| command, these will be added after the default options like this: \par
\hfil \verb|\node[line width=0.3pt,inner sep=-0.15pt,cm=<matrix>,<position>,<node_options>]|.\hfil
    \end{itemize}
\end{itemize}

\begin{demo}{Importing an Image}
\begin{luadraw}{name=Dimage}
local ld = luadraw
local Z, i = ld.cpx.Z, ld.cpx.I
local g = ld.graph:new{margin={0,0,0,0}, size={10,10},bg="green!30"}
local filename = ld.cachedir.."flower.jpg"
local g_options = "width=4.5cm,height=4.5cm"
g:Dimage(filename,Z(-5,5),{pos="SE", graphics_options=g_options})
g:Dimage(filename,Z(5,5),{pos="SE", matrix={0,-1,i}, graphics_options=g_options})
g:Rotate(180)
g:Dimage(filename,Z(-5,5),{pos="SE", graphics_options=g_options})
g:IDmatrix()
g:Dimage(filename,Z(-5,-5),{pos="SE", matrix={0,1,-i}, graphics_options=g_options})
g:Dlabel( "Original", Z(-5,5),{pos="SE"},
"Vertical symmetry", Z(5,5), {pos="SW"},
"Rotation (global)", Z(5,-5), {pos="NW"},
"Horizontal symmetry", Z(-5,-5), {pos="NE"} )
g:Show()
\end{luadraw}
\end{demo}

\subsubsection{Mapping an Image onto a Parallelogram}

This can be done using the method: \cmd{g:Dmapimage( filename, parallelo, options)} (which uses the \cmd{g:Dimage()} method), where:
\begin{itemize}
    \item \argu{filename} is the full name of the image file.
    
    \item \argu{parallelo} is a list of the form $\{a,u,v\}$, consisting of three complex numbers. This list defines the parallelogram with vertices $\{a,a+u,a+u+v,a+v\}$.
    
    \item \argu{options} is a table whose fields specify the parameters (with their default values):
    \begin{itemize}
        \item \opt{name=""} optionally assigns a name to the node that is created. This name can later be referenced in TikZ.
        
        \item \opt{clip=false} is a Boolean flag indicating whether the image should be clipped to the parallelogram. In most cases, clipping is not required.
        
        \item \opt{border\_options=nil}, when set to a value other than \nil, must be a string containing the drawing options for the boundary of the parallelogram. The value of \opt{border\_options} is passed directly to the \drawcmd command to render the outline.
        
        \item \opt{graphics\_options=""} which is a string of options for the \verb|\includegraphics| command, these will be added after the default options like this:\par
\hfil\verb|\includegraphics[width=1cm,height=1cm, <graphics_options>]|.\hfil

        \item \opt{node\_options=""} which is a string containing options for the \verb|\node| command, these will be added to the end of the default options as for \cmd{g:Dimage()}.
    \end{itemize}
\end{itemize}

\begin{demo}{Mapping an Image}
\begin{luadraw}{name=Dmapimage}
local ld = luadraw
local g = ld.graph:new{window={-2,3.5,-0.5,7}, margin={0,0,0,0}, size={10,10},bg="green!30"}
local filename = ld.cachedir.."flower.jpg"
local i = ld.cpx.I
local a, u, v, w = 0, 3, -1.5+i, 4*i
local facets = {{a,u,w}, {a+v,-v,w}, {a+v+w,-v,u} }
local portrait = {a+w+(2*v+u)/3+0.2, (u-v)/4, 1.5*i+0.2}
for _,f in ipairs(facets) do
    g:Dmapimage(filename,f,{border_options="Navy"})
end
g:Dmapimage(ld.cachedir.."russell.jpg", portrait, {border_options="lightgray,line width=1.5mm"})
g:Show()
\end{luadraw}
\end{demo}


\subsection{Colors}

In the \luadrawenv environment, colors are character strings that must correspond to colors known to TikZ. The \emph{xcolor} package is strongly recommended so as not to be limited to basic colors.

\subsubsection{Color Calculations}

To be able to manipulate colors, they have been defined (in the \emph{luadraw\_colors.lua} module) as tables of three components: red, green, blue, each component being a number between $0$ and $1$, and with their names in the \emph{svgnames} format of the \emph{xcolor} package. For example, we find (among others) the declarations:

\begin{Luacode}
local ld = luadraw
ld.AliceBlue = {0.9412, 0.9725, 1}
ld.AntiqueWhite = {0.9804, 0.9216, 0.8431}
ld.Aqua = {0.0, 1.0, 1.0}
ld.Aquamarine = {0.498, 1.0, 0.8314}
\end{Luacode}
You can refer to the \emph{xcolor} documentation for a list of these colors.

To use these in the \luadrawenv environment, you can:
\begin{itemize}
    \item either use them by name if you have declared them in the preamble: \verb|\usepackage[svgnames]{xcolor}|, for example: \code{g:Linecolor("AliceBlue")},

    \item or use them with the \luadrawenv \textbf{ld.rgb()} function, for example: \code{g:Linecolor(ld.rgb(AliceBlue))}. However, with this \cmd{ld.rgb()} function, to change the color locally, you must do the following (example): \par
\code{g:Dpolyline(L,"color="..ld.rgb(AliceBlue))}, or \code{g:Dpolyline(L,"fill="..ld.rgb(AliceBlue))}. Because the \code{rgb()} function does not return a color name, but a color definition.
\end{itemize}

\paragraph{Functions for color management:}
\begin{itemize}
    \item The \cmd{ld.rgb(r, g, b)} or \cmd{ld.rgb(\{r, g, b\})} function returns the color as a string understandable by TikZ in the \code{color=\ldots} and \code{fill=\ldots} options. The values ​​of $r$, $g$, and $b$ must be between $0$ and $1$.

    \item The \cmd{ld.hsb(h, s, b \fac{, table})} function returns the color as a string understandable by TikZ. The \argu{h} (hue) argument must be a number between $0$ and $360$, the \argu{s} (saturation) argument must be between $0$ and $1$, and the \argu{b} (brightness) argument must also be between $0$ and $1$. The (optional) argument \argu{table} is a boolean (false by default) that indicates whether the result should be returned as a table $\{r,g,b\}$ or not (by default it is as a string).

    \item The function \cmd{ld.mixcolor(color1, proportion1, color2, proportion2, ..., colorN, proportionN)} mixes the colors \argu{color1}, ...,\argu{colorN} in the requested proportions and returns the resulting color as a string understandable by TikZ, followed by the same color as a table $\{r,g,b\}$. Each color must be a table of three components $\{r,g,b\}$.
    
    \item The function \cmd{ld.mixpalette(pal, percent, color)} returns a new palette after mixing each color from the palette \argu{pal} with \argu{color}. The argument \argu{pal} is a list of colors, each of which is a table of three components $\{r,g,b\}$, the argument \argu{percent} is a number between $0$ and $100$, and the argument \argu{color} is a color in the form of a table $\{r,g,b\}$.

    \item The function \cmd{ld.palette(colors, pos \fac{, table})}: the argument \argu{colors} is a list (table) of colors in the format $\{r,g,b\}$, the argument \argu{pos} is a number between $0$ and $1$, the value $0$ corresponds to the first color in the list and the value $1$ to the last. The function calculates and returns the color corresponding to the position \argu{pos} in the list by linear interpolation. The (optional) argument \argu{table} is a boolean (false by default) that indicates whether the result should be returned as a table $\{r,g,b\}$ or not (by default it is as a string).

    \item The \cmd{ld.getpalette(colors, nb \fac{, table})} function: the \argu{colors} argument is a list (table) of colors in $\{r,g,b\}$ format, the \argu{nb} argument indicates the desired number of colors. The function returns a list of \argu{nb} colors evenly distributed in \argu{colors}. The (optional) \argu{table} argument is a boolean (\false by default) that indicates whether the colors are returned as $\{r,g,b\}$ tables or not (by default, they are as strings).

    \item The method \cmd{g:Newcolor(name, color)} allows you to define a new color in the TikZ export to rgb format. The name of this color will be \argu{name} (string). The argument \argu{color} can be either a table of three components: red, green, blue (between 0 and 1) defining this color, or a string representing a color. In the first case, the method uses a \verb|\definecolor|, and in the second case, it uses a \verb|\colorlet|.

\end{itemize}
You can also use all of TikZ's usual color management features.

By default, there are five color palettes.\footnote{A palette is a table of colors; these are themselves tables of numbers between $0$ and $1$ representing the red, green, and blue components.}.

\begin{demo}{The five default palettes}
\begin{luadraw}{name=palettes}
local ld = luadraw
local Z = ld.cpx.Z
local g = ld.graph:new{window={-5,5,-5,5},bbox=false, border=true}
g:Linewidth(1)
local Dpalette = function(pal,A,h,L,N,name)
    local dl = L/N
    for k = 1, N do
        local color = ld.palette(pal,(k-1)/(N-1))
        g:Drectangle(A,A+h,A+h+dl,"color="..color..",fill="..color)
        A = A+dl
    end
    g:Drectangle(A,A+h,A+h-L); g:Dlabel(name,A+h/2,{pos="E"})
end
local A, h, dh, L, N = Z(-5,4), Z(0,-1), Z(0,-1.1), 5, 100
Dpalette(ld.rainbow,A,h,L,N,"ld.rainbow"); A = A+dh
Dpalette(ld.palAutumn,A,h,L,N,"ld.palAutumn"); A = A+dh
Dpalette(ld.palGistGray,A,h,L,N,"ld.palGistGray"); A = A+dh
Dpalette(ld.palGasFlame,A,h,L,N,"ld.palGasFlame"); A = A+dh
Dpalette(ld.palRainbow,A,h,L,N,"ld.palRainbow")
g:Show()
\end{luadraw}
\end{demo}
