# Nodes defined recursively

The following code is straightforward (I have left blank lines to distinguish 5 sections):

\begin{tikzpicture}
\coordinate (B) at (0,0);
\coordinate (A) at (12,0);
\coordinate (C) at (3,5);
\coordinate (f0) at ($(B)!(C)!(A)$);

\coordinate (f1) at ($(C)!(f0)!(A)$);
\coordinate (f2) at ($(B)!(f1)!(A)$);
\coordinate (f3) at ($(C)!(f2)!(A)$);
\coordinate (f4) at ($(B)!(f3)!(A)$);
\coordinate (f5) at ($(C)!(f4)!(A)$);
\coordinate (f6) at ($(B)!(f5)!(A)$);

\draw (B) -- (A) -- (C)
node [midway,above] {$\mathbf b$} --
node [midway, left] {$\mathbf a$}    cycle;

\filldraw[fill=blue!5!white, draw=black]
(C) -- (f0) node [midway,left] {$h_0$} --
(f1) node [midway,above,left] {$a_0$} --
(f2) node [midway,left] {$h_1$} --
(f3) node [midway,above,left] {$a_1$} --
(f4) node [midway,left] {$h_2$} --
(f5) node [midway,above,left] {$a_2$};

\draw
(B) --
(f0) node [midway,below] {$c_0$} --
(f2) node [midway,below] {$c_1$} --
(f4) node [midway,below] {$c_2$} --
(f6) node [midway,below] {$c_3$};
\end{tikzpicture}

1. Defines the coordinates of a right triangle, and the base point of the construction.

2. Defines each new coordinate as the foot of a height to either segment CA or BA from a previously computed coordinate, starting from the base point.

3. Draw the triangle.

4. Draw the heights toward BA and label them.

5. Draw the heights toward CA.

My question is what is the syntax to automate this process, so that I can do the same, not just 3 steps in, but 5, 10, or variably more steps?

• you can define a new command (\newcommand or \def) cho that task Jul 15, 2020 at 1:58

With \foreach, only two paths to which the points are projected and a start point are needed.

In your case, path 1 is BA, path 2 is CA, start point is C. From start, C is projected to path 1, and then the perpendicular is projected to path 2, and new perpendicular is projected to path 1 and so on...

\documentclass[tikz, border=1cm]{standalone}
\usetikzlibrary{calc}
\def\perpset#1{\pgfqkeys{/perpendicular}{#1}}
\makeatletter
\perpset{
/perpendicular/.search also=/tikz,
path 1/.code args={from (#1) to (#2)}{
\def\perp@from@a{#1}
\def\perp@to@a{#2}
},
path 2/.code args={from (#1) to (#2)}{
\def\perp@from@b{#1}
\def\perp@to@b{#2}
},
start/.code args={(#1)}{\def\perp@start{#1}}
}
\newcommand{\perpdraw}[2][1]{
\begin{scope}[/perpendicular/.cd,#2]
\coordinate (perp@start-0) at (\perp@start);
\foreach \x [count=\i from 0] in {1, ..., #1} {
\draw[fill=blue!5] (perp@start-\i)
-- node[left, teal] {$h_\i$} ($(\perp@from@a)!(perp@start-\i)!(\perp@to@a)$) coordinate (perp@end-\i)
-- node[left, red] {$a_\i$} ($(\perp@from@b)!(perp@end-\i)!(\perp@to@b)$) coordinate (perp@start-\x);
}
\end{scope}
}
\makeatother

\begin{document}
\begin{tikzpicture}
\coordinate (B) at (0,0);
\coordinate (A) at (12,0);
\coordinate (C) at (3,5);
\draw (B) -- (A) -- (C)
node [midway,above] {$\mathbf b$} --
node [midway, left] {$\mathbf a$}    cycle;
\perpdraw[4]{
path 1=from (B) to (A),
path 2=from (C) to (A),
start=(C),
}
\end{tikzpicture}
\end{document}


One \foreach loop is enough to draw. The coordinates (Ai), (Bi), (Ci)are recusively defined.

\documentclass[tikz,border=5mm]{standalone}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}[join=round]
\path
(0,0) coordinate (B0)
(12,0) coordinate (A0)
(2,6) coordinate (C0)
;
\draw (B0)--(A0)--(C0)
node[midway,above]{$\mathbf b$} --
node[midway,left]{$\mathbf a$}  cycle;

\foreach \i in {1,...,5}{
\pgfmathsetmacro{\j}{int(\i-1)}
\path
($(A\j)!(C\j)!(B\j)$) coordinate (B\i)
($(A\j)!(B\i)!(C\j)$) coordinate (C\i)
(A\j) coordinate (A\i);
\fill[brown!30] (C\j)--(B\i)--(C\i)--cycle;
\draw[blue] (B\i)--(B\j) node[midway,below] {$c_{\j}$};
\draw[red] (C\j)--(B\i) node[pos=.7,left]{$h_{\j}$};
\draw[violet] (B\i)--(C\i) node[pos=.7,above left]{$a_{\j}$};
}
\draw (A0)--(C0);
\end{tikzpicture}
\end{document}