\subsection{The \emph{luadraw\_coils\_chains} module}

The module \emph{luadraw\_coils\_chains} allows you to draw springs (two types) and chains (two types).
It adds new graphing methods to the \emph{ld.graph} class but returns nothing.

\subsubsection{Springs}

\begin{itemize}
    \item The method \cmd{g:Dcoil(list, radius, options)} allows you to draw a spring. The argument \argu{radius} is the radius of the spring. The argument \argu{list} has two possible forms:
    \begin{itemize}
        \item Form $1$: \argu{list} = \{a1, ​​n1, a2, n2,\ldots, aN\}, where $a_1$, \ldots, $a_N$ are points (complex numbers), $n_1$ is an integer, representing the number of turns between $a_1$ and $a_2$, $n_2$ is the number of turns between $a_2$ and $a_3$, and so on.

        \item Form $2$: \argu{list} = \{C, n\}, where $C$ is a polygonal line (for example, a curve) and $n$ is an integer representing the total number of turns. The spring will be drawn along the curve $C$.
    \end{itemize}

    The \argu{options} argument is a table whose fields are the options that will modify the aesthetic appearance of the spring. These options are (with their default values):
    
    \begin{itemize}
        \item \opt{direction=1}: Direction of rotation of the coils ($1$ for clockwise, $-1$ for counterclockwise).
        \item \opt{wire\_dia=<2*radius/15>}: Wire diameter.
        \item \opt{color="gray"}: Fill color of the coils.
        \item \opt{colorB=color}: Fill color of the "back" coils; by default, this is the same value as the \opt{color} option.
        \item \opt{border=<current color>}: Color of the coil outline.
        \item \opt{border\_width=<current thickness>}: Thickness of the coil outline.
        \item \opt{tension=1}: The coils are slightly curved to give a 3D effect; this option allows you to increase or decrease the curvature. With a value of $0$, there is no curvature.
        \item \opt{start\_angle=\nil}, \opt{end\_angle=\nil}: angles (in degrees) for the first and last half-turns. These angles are determined automatically, but they can be modified with these options.
        \item \opt{wireframe=\false}: with the value \true, the turns are filled with a solid color, but with the value \false (default), they are filled with a gradient, specifically with the following formula:
        \codeln{left color=<color>!<leftC>, right color=<color>!<rightC>, middle color=<color>!<midC>}
        with the three coefficients being options: \opt{leftC=100}, \opt{rightC=50}, \opt{midC=10}.
        \item \opt{holes=\false}: With the value \true, a dot is drawn at the ends of the spring.
        \item \opt{reverse=\false}: Reverses the display order of the spring when it is made up of multiple pieces.
    \end{itemize}

\begin{demo}{The \emph{g:Dcoil()} Method}
\begin{luadraw}{name=Dcoil_examples}
local ld = luadraw
local cpx = ld.cpx
local Z = cpx.Z

local g = ld.graph:new{window={-5.6,5,-5,5}, size={10,10}}
require 'luadraw_coils_chains'
local a, b, c, d = Z(-5,4), Z(-1,4), Z(1,4), Z(3,4)
g:Dcoil({a,10,b}, 0.75, {}); g:Dlabel("default options", b+0.5, {pos="E"})
g:Shift(Z(0,-2))
g:Dcoil({a,10,b,10,c,5,d}, 0.75, {color="SteelBlue", direction=-1, wire_dia=0.075, 
    border="gray", colorB="Brown"})
g:Dlabel("3 springs", d+0.5, {pos="E"})
g:Shift(Z(0,-2))
g:Dcoil({Z(-4,4),8, Z(-4,0)}, 1, {wireframe=true}); g:Dlabel("wireframe=true",Z(-4,-0.5),{pos="S"})
local C = ld.circle(Z(2,2),2)
g:Dcoil( {C,30}, 0.5, {color="Crimson", wire_dia=0.1, border="lightgray", colorB="DarkRed"})
g:Dlabel("Along a circle",Z(2,-0.5),{pos="S"})
g:Show()
\end{luadraw}
\end{demo}
    
    \item The method \cmd{g:Dcoil2(list, radius, options)} allows you to draw a wire spring. The argument \argu{radius} is the radius of the spring. The argument \argu{list} has two possible forms:
    \begin{itemize}
        \item Form $1$: \argu{list} = \{a1, ​​n1, a2, n2,\ldots, aN\}, where $a_1$, \ldots, $a_N$ are points (complex numbers), $n_1$ is an integer, representing the number of turns between $a_1$ and $a_2$, $n_2$ is the number of turns between $a_2$ and $a_3$, and so on.

        \item Form $2$: \argu{list} = \{C, n\}, where $C$ is a polygonal line (for example, a curve) and $n$ is an integer representing the total number of turns. The spring will be drawn along the curve $C$.
    \end{itemize}
    The argument \argu{options} is a table whose fields are the options that will modify the aesthetic appearance of the spring. These options are (with their default values):
    \begin{itemize}
        \item \opt{direction=1}: direction of rotation of the coils ($1$ for clockwise, $-1$ for counterclockwise).

        \item \opt{color=<current color>}: color of the coils.

        \item \opt{width=<current thickness>}: thickness of the coils (in tenths of a point).

        \item \opt{border="white"}: this option is either an empty string (\val{""}) or a string representing a color. In the latter case, the drawing uses the \emph{double} option of TikZ with this color as the border, allowing the "front" coils to be seen passing in front of the "back" coils, like a 3D spring. 

        \item \opt{border\_width=<current thickness>}: border thickness (used when the \opt{border} option is not equal to \nil).
        \end{itemize}

\begin{demo}{The \emph{g:Dcoil2()} Method}
\begin{luadraw}{name=Dcoil2_examples}
local ld = luadraw
local cpx = ld.cpx
local Z = cpx.Z

local g = ld.graph:new{window={-5.6,5,-5,5}, size={10,10}}
require 'luadraw_coils_chains'
local a, b, c, d = Z(-5,4), Z(-1,4), Z(1,4), Z(3,4)
g:Dcoil2({a,10,b}, 0.75, {}); g:Dlabel("default options", b+0.5, {pos="E"})
g:Shift(Z(0,-2))
g:Dcoil2({a,10,b,10,c,4,d}, 0.75, {color="SteelBlue", direction=-1, width=12})
g:Dlabel("3 springs", d+0.5, {pos="E"})
g:Shift(Z(0,-2))
g:Dcoil2({Z(-4,4),8, Z(-4,0)}, 1, {border="", width=12}); g:Dlabel('border=""',Z(-4,-0.5),{pos="S"})
local C = ld.circle(Z(2,2),2)
g:Dcoil2( {C,60}, 0.5, {color="Crimson", width=8, border="white"})
g:Dlabel("Along a circle",Z(2,-0.5),{pos="S"})
g:Show()
\end{luadraw}  
\end{demo}        
\end{itemize}

\subsubsection{Chains}

\begin{itemize}
    \item The \cmd{g:Dchain(list, link\_length, options)} method allows you to draw a chain. The \argu{list} argument is a list of at least two complex numbers; these are the points through which the chain will pass. The \argu{link\_length} argument is the length of a link in the chain, but this will be adjusted to have a whole number of links.
    The \argu{options} argument is an array whose fields are the options that will modify the aesthetic appearance of the chain. These options are (with their default values):
    \begin{itemize}
        \item \opt{width=<(calculated length of a link)/1.618>} : width of a link, with the value \opt{width="circle"}, the link will be circular.
        \item \opt{wire\_dia=<width/4>}: wire diameter.
        \item \opt{color="gray"}: color to fill the links.
        \item \opt{colorB=color}: Second fill color for the links; by default, it's the same value as the \opt{color} option.
        \item \opt{border=<current color>}: Color of the link outline.
        \item \opt{border\_width=<current thickness>}: Thickness of the link outline. \item \opt{wireframe=\false}: With the value \true, the links are filled with a solid color, but with the value \false (the default value) they are filled with a gradient, specifically with the following formula:
        \codeln{left color=<color>!<leftC>, right color=<colorB>!<rightC>, middle color=<color>!<midC>}
        with the three coefficients being options: \opt{leftC=100}, \opt{rightC=50}, \opt{midC=10}.
    \end{itemize}

\begin{demo}{The \emph{g:Dchain()} Method}
\begin{luadraw}{name=Dchain_examples}
local ld = luadraw
local cpx = ld.cpx
local Z = cpx.Z

local g = ld.graph:new{window={-5.25,6,-5,5.25}, size={10,10}}
require 'luadraw_coils_chains'
g:Labelsize("small")
local a, b = Z(-5,4.5), Z(3,4.5)
g:Dchain({a,b}, 1, {}); g:Dlabel(" default options", b, {pos="E", dist=0.1})
g:Shift(Z(0,-1))
g:Dchain({a,b}, 0.25, {wireframe=true}); g:Dlabel("wireframe=true", b, {pos="E"})
g:Shift(Z(0,1))
a, b = Z(-4,3), Z(-4,-4)
g:Dchain({a,b}, 0.35, {color="SteelBlue", colorB="DarkBlue", width="circle", wire_dia=0.1, border="lightgray"})
g:Dlabel('width="circle"', b, {pos="S",dist=0.1})
a, b = Z(-2,1), Z(4,1)
local C = ld.ellipticarc(a,Z(1,1),b,3,4,1)
g:Dchain(C, 0.75, {color="Brown", border="lightgray"}); g:Ddots({a,b})
g:Dlabel("Elliptic Arc",Z(1,-3),{pos="S",dist=0.5})
g:Show()
\end{luadraw}
\end{demo}

    \item The \cmd{g:Dchain2(list, R, options)} method draws a chain as two intersecting wavy wires. The \argu{list} argument is a list of at least two complex numbers, representing the points through which the chain will pass. The \argu{R} argument is the radius of the chain (the height of a wave). The \argu{options} argument is an array whose fields are the options that will modify the aesthetic appearance of the chain.
    \begin{itemize}
        \item \opt{direction=1}: direction of rotation of the wires ($1$ for clockwise, $-1$ for counterclockwise).
        \item \opt{color=<current color>}: color of the chain.
        \item \opt{width=<current thickness>}: thickness of the wires (in tenths of a point).
        \item \opt{border="white"}: This option is either an empty string (\val{""}) or a string representing a color. In the latter case, the drawing uses TikZ's \emph{double} option with that color as the border, thus distinguishing the wire in front from the one behind.
        \item \opt{border\_width=<current thickness>}: Border thickness (used when the \opt{border} option is not equal to \nil).
        \end{itemize}

\begin{demo}{The \emph{g:Dchain2()} Method}
\begin{luadraw}{name=Dchain2_examples}
local ld = luadraw
local cpx = ld.cpx
local Z = cpx.Z

local g = ld.graph:new{window={-5,5,-4.5,5}, size={10,10}}
require 'luadraw_coils_chains'
g:Labelsize("small")
local a, b = Z(-4,4.5), Z(4,4.5)
g:Dchain2({a,b}, 0.1, {width=8})
g:Shift(Z(0,1))
a, b = Z(-4,3), Z(-4,-4)
g:Dchain2({a,b}, 0.35, {color="SteelBlue", width=24, border=""})
g:Dlabel('border=""', b, {pos="S",dist=0.1})
a, b = Z(-2,1), Z(4,1)
local C = ld.circle(Z(1,-1), 2.5)
g:Dchain2(C, 0.125, {color="Brown", width=12})
g:Dlabel("Along a circle",Z(1,-4),{pos="S",dist=0.1})
g:Show()
\end{luadraw} 
\end{demo}
\end{itemize}

