\section{Implicit curves}

An implicit curve is the set of the points $(x, y)$ such that $F(x,y)=0$: circle, ellipse, hyperbola, lemniscate, folium of Descartes, etc. The equation does not need to be solved: Lua computes $F$ at the nodes of a grid that covers the window and finds the cells where $F$ changes sign (\emph{marching squares}). Each crossing point is then computed accurately by bisection, and the pieces are linked into continuous paths.

\begin{NewMacroBox}{tkzFnImplicit}{\oarg{options}\marg{$F(x,y)$}}
\emph{$F(x,y)$ is a Lua expression of the variables \texttt{x} and \texttt{y}. An equation can also be written: \texttt{\{x\string^2 + y\string^2 = 4\}} is the same as \texttt{\{(x\string^2 + y\string^2)-(4)\}}. The curve is drawn in the window defined by \tkzcname{tkzFnInit}.}

\begin{tabular}{lll}
\toprule
options             & default & definition                         \\
\midrule
\TOline{level}{0}{value(s) $c$: curves $F(x,y)=c$}
\TOline{samples}{100}{number of cells on the longer side}
\TOline{domain}{xmin:xmax}{interval of the values of $x$}
\TOline{ydomain}{ymin:ymax}{interval of the values of $y$}
\TOline{refine}{true}{bisection on each edge of the grid}
\TOline{clip}{true}{the curve is clipped to the window}
\bottomrule
\end{tabular}

\emph{Other options are \TIKZ\ options. \texttt{level} accepts a list as \tkzcname{foreach} does: \texttt{level=\{-2,-1,...,2\}}.}
\end{NewMacroBox}

\subsection{Conics}

$\dfrac{x^2}{9}+\dfrac{y^2}{4}=1$ \quad and \quad $x^2-y^2=1$

\begin{tkzexample}[latex=8cm]
\begin{tikzpicture}[scale=.8]
  \tkzFnInit[xmin=-4,xmax=4,ymin=-3,ymax=3]
  \tkzFnGrid
  \tkzFnAxes
  \tkzFnImplicit[thick,red]{x^2/9 + y^2/4 = 1}
  \tkzFnImplicit[thick,blue]{x^2 - y^2 = 1}
\end{tikzpicture}
\end{tkzexample}

\subsection{Lemniscate of Bernoulli and folium of Descartes}

$(x^2+y^2)^2=4(x^2-y^2)$ \quad and \quad $x^3+y^3=3xy$

The double points at $O$ are handled correctly.

\begin{tkzexample}[latex=8cm]
\begin{tikzpicture}[scale=.8]
  \tkzFnInit[xmin=-4,xmax=4,ymin=-3,ymax=3]
  \tkzFnGrid
  \tkzFnAxes
  \tkzFnImplicit[thick,blue]
    {(x^2+y^2)^2 = 4*(x^2-y^2)}
  \tkzFnImplicit[thick,red]{x^3 + y^3 = 3*x*y}
\end{tikzpicture}
\end{tkzexample}

\subsection{An elliptic curve}

$y^2=x^3-2x+2$: the curve has one component; with $y^2=x^3-2x$, it has two.

\begin{tkzexample}[latex=8cm]
\begin{tikzpicture}[scale=.8]
  \tkzFnInit[xmin=-3,xmax=3,ymin=-4,ymax=4]
  \tkzFnGrid
  \tkzFnAxes
  \tkzFnImplicit[thick,red]{y^2 = x^3 - 2*x + 2}
  \tkzFnImplicit[thick,blue]{y^2 = x^3 - 2*x}
\end{tikzpicture}
\end{tkzexample}

\newpage
\subsection{Level curves}

The option \tkzname{level} draws the curves $F(x,y)=c$ for several values of $c$. Here are the level curves of $F(x,y)=x^2-y^2$: the level curve $0$ is made of the two lines $y=x$ and $y=-x$.

\begin{tkzexample}[latex=8cm]
\begin{tikzpicture}[scale=.8]
  \tkzFnInit[xmin=-3,xmax=3,ymin=-3,ymax=3]
  \tkzFnGrid
  \tkzFnAxes
  \tkzFnImplicit[level={-4,-2,...,4},blue]
    {x^2 - y^2}
  \tkzFnImplicit[thick,red]{x^2 - y^2}
\end{tikzpicture}
\end{tkzexample}

With a \tkzcname{foreach} loop, each level can have its own color.

\begin{tkzexample}[latex=8cm]
\begin{tikzpicture}[scale=.8]
  \tkzFnInit[xmin=-3,xmax=3,ymin=-3,ymax=3]
  \tkzFnAxes
  \foreach \c/\col in {0.5/blue,1/teal,
                       2/orange,3/red}
    {\tkzFnImplicit[level=\c,thick,\col]
       {x^2 + x*y + y^2}}
\end{tikzpicture}
\end{tkzexample}

\subsection{Discontinuous function}

For $y=\tan(x)$, $F(x,y)=y-\tan(x)$ changes sign across the asymptotes without vanishing: these false crossings are detected and removed.

\begin{tkzexample}[latex=8cm]
\begin{tikzpicture}[scale=.8]
  \tkzFnInit[xmin=-4,xmax=4,ymin=-3,ymax=3]
  \tkzFnGrid
  \tkzFnAxes
  \tkzFnImplicit[thick,red]{y = tan(x)}
\end{tikzpicture}
\end{tkzexample}

\newpage
\subsection{Window, units and smoothing}

The cells of the grid are squares on the page, even if \tkzname{xstep} and \tkzname{ystep} are different. The options \tkzname{domain} and \tkzname{ydomain} restrict the drawing to a part of the window. With \tkzname{smooth}, closed curves are drawn with \texttt{smooth cycle}.

\begin{tkzexample}[vbox]
\begin{tikzpicture}[scale=.8]
  \tkzFnInit[xmin=-6,xmax=6,xstep=2,ymin=-2,ymax=2,ystep=.5]
  \tkzFnGrid
  \tkzFnAxes
  \tkzFnImplicit[thick,red,smooth,samples=60]{x^2/16 + y^2 = 1}
  \tkzFnImplicit[thick,blue,domain=0:6,ydomain=0:2]{x^2/16 + y^2 = 1.5^2}
\end{tikzpicture}
\end{tkzexample}

\subsection{Limits of the method}

\begin{itemize}
\item A curve along which $F$ does not change sign is not found: $(x-y)^2=0$ gives nothing, write $x-y=0$ instead.
\item A detail smaller than a cell of the grid may be missed: increase \tkzname{samples} (for example \texttt{samples=200}).
\item Near a singular point, the drawing is accurate up to the size of a cell.
\end{itemize}

\endinput
