绘制递归阶梯函数

绘制递归阶梯函数

我正在尝试绘制以下图形

在此处输入图片描述

但是我真的不知道该怎么做。很抱歉没有像往常一样包含 MWE。但我不知道从哪里开始。

答案1

像这样?

\documentclass[tikz,border=3.14mm]{standalone}
\begin{document}
\begin{tikzpicture}[pics/staircase/.style={code={
  \draw[mydash] (-0.5*#1,-0.5*#1) -| (0.5*#1,0.5*#1);
  \foreach \XX in {1,...,#1}
   {\draw[mydash] (-0.5*#1+\XX-1,-0.5*#1+\XX-1) |- ++ (1,1);
   \ifnum\XX<#1
   \draw[mydash,red] (-0.5*#1+\XX,-0.5*#1) -- ++ (0,\XX);
   \draw[mydash,blue] (0.5*#1,0.5*#1-\XX) -- ++ (-\XX,0);
   \fi}}},mydash/.style={thick,shorten <=1mm,dash pattern=on 8mm off 2mm}]

 \path (0,-1.5) pic{staircase=1} (2,-1) pic{staircase=2}
 (5,-0.5) pic{staircase=3} (9,0) pic{staircase=4};
\end{tikzpicture}
\end{document}

在此处输入图片描述

此版本的原点位于其“中心”,因此更容易旋转。但是,为了重现您的图形,您可能对原点位于基线中间的版本感兴趣。

\documentclass[tikz,border=3.14mm]{standalone}
\begin{document}
\begin{tikzpicture}[pics/staircase/.style={code={
  \draw[mydash] (-0.5*#1,0) -| (0.5*#1,#1);
  \foreach \XX in {1,...,#1}
   {\draw[mydash] (-0.5*#1+\XX-1,\XX-1) |- ++ (1,1);
   \ifnum\XX<#1
   \draw[mydash,red] (-0.5*#1+\XX,0) -- ++ (0,\XX);
   \draw[mydash,blue] (0.5*#1,#1-\XX) -- ++ (-\XX,0);
   \fi}}},mydash/.style={thick,shorten <=1mm,dash pattern=on 8mm off 2mm}]

 \path \foreach \X in {1,...,5}
 {({\X*(\X+1)/2-1},0) pic{staircase=\X} node[below=0.5ex] {$F_\X$}};
\end{tikzpicture}
\end{document}

在此处输入图片描述

至于您的间距不均匀问题:尝试

\documentclass{article}

\usepackage{geometry}

\usepackage{subcaption}
\usepackage{tikz}

\tikzset{%
    pics/staircase/.style={code={
 \draw[mydash] (-0.5*#1,-0.5*#1) -| (0.5*#1,0.5*#1);
 \foreach \XX in {1,...,#1}
  {\draw[mydash] (-0.5*#1+\XX-1,-0.5*#1+\XX-1) |- ++ (1,1);
  \ifnum\XX<#1
  \draw[mydash,red] (-0.5*#1+\XX,-0.5*#1) -- ++ (0,\XX);
  \draw[mydash,blue] (0.5*#1,0.5*#1-\XX) -- ++ (-\XX,0);
  \fi}}},mydash/.style={thick,shorten <=1mm,dash pattern=on 8mm off 2mm}
}
\newcommand{\staircase}[1]{%
    \begin{tikzpicture}
       \path (0,0) pic{staircase=#1};
   \end{tikzpicture}
}
\begin{document}

\begin{figure}[!h]
   \centering
   \foreach \X in {1,...,4}
   {\begin{subfigure}[b]{\X.5cm}
       \centering
       \staircase{\X}
       \caption*{$F_\X$}
   \end{subfigure}}
   \caption{}
\end{figure}
\end{document}

在此处输入图片描述

相关内容