[Tex/LaTex] Drawing rectilinear curves in Tikz, aka an Etch-a-Sketch drawing

diagramstikz-pgf

I'd like to draw a rectilinear path by starting at a particular location, and then prescribing increments in the horizontal and vertical directions.

I am currently doing something like this

\documentclass{standalone}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
    \draw [ultra thick] (20,12) -- (18,12);
    \draw [ultra thick] (18,12) -- (18,10);
    \draw [ultra thick] (18,10) -- (16,10);
    \draw [ultra thick] (16,10) -- (16,8);
    \draw [ultra thick] (16,8) -- (12,8);
    \draw [ultra thick] (12,8) -- (12,10);
    \draw [ultra thick] (12,10) -- (8,10);
    \draw [ultra thick] (8,10) -- (8,12);
    \draw [ultra thick] (8,12) -- (6,12);
    \draw [ultra thick] (6,12) -- (6,14);
    \draw [ultra thick] (6,14) -- (4,14);
    % etc ...

\end{tikzpicture}
\end{document}

enter image description here

But this requires that I locate each point, when what is much easier (for my particular case) is to just indicate the horizontal or vertical distance from one point to the next.

I have tried something like this

\documentclass{standalone}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
   \node (A) at (20,12) {};
   \node (B) at (18,12) {};
   \node (C) at (18,10) {};
   \node (D) at (16,10) {};

   \draw (A) |- ++ (-2,0)  -| (B);
   \draw (B) |- ++ (0, -2) -| (C);
   \draw (C) |- ++ (-2, 0) -| (D);
   % etc
\end{tikzpicture}
\end{document}

It looks like the ++ (-2,0) bit seems to be on the right track, but naming the nodes is tedious, and this doesn't produce what I have in mind.

Is there some way to do what I have in mind with simple notation? Something like

Start at (20,12)
Go left 2 units
Go down 2 units
Go left 2 units
% etc

Aka, Etch-a-Sketch style?

Best Answer

\draw (20,12) -- ++(2,0) -- ++(0,2) -- ++(-3,0) -- ++(45:3);

Use ++ before each new incremental coordinate to make it relative to the last one and put the pencil there.

Here's a complete example:

\documentclass{article}
\usepackage{tikz}
\begin{document}
\tikz\draw (20,12) -- ++(2,0) -- ++(0,2) -- ++(-3,0) -- ++(30:3) {[rounded corners=10pt]-- ++(5,0) -- ++(0,-6)} -- ++(-7,0) -- cycle;
\end{document}

enter image description here

Of course, combining this with the -| or |- path operators can simplify the code even further; the following two pieces of code produce the same result:

\tikz\draw (20,12) -- ++(2,0) -- ++(0,2) -- ++(3,0) -- ++(0,1) -- ++(1,0) -- ++(0,-3) -- ++(2,0);\par\bigskip

and

\tikz\draw (20,12) -| ++(2,2) -| ++(3,1) -- ++(1,0) |- ++(2,-3);

I don't think that defining commands in this case adds anything; in fact, I think it reduces the functionality of the existing syntax (which is already simple). The example demonstrates that you can use, for example, polar coordinates and modify (up to TikZ limitations) the path attributes midways; even if the current question doesn't require this, it's a good thing to have the possibility to do those kind of modification if they are required.