使用“foreach”循环在 TikZ 中绘制楼梯

使用“foreach”循环在 TikZ 中绘制楼梯

我正在尝试绘制一个楼梯,我可以设置楼梯的数量,即\renewcommand{/stairs}{5}它会创建楼梯。我相信为了每个循环都可以完成此操作。我该如何简化我的代码?

理想情况下,我想设置楼梯数量,并设置竖板和踏板长度以及它们的显示位置。例如,我可以把楼梯编号设为 7,然后系统会自动为我创建 7 个台阶吗?

这是我的代码:

\documentclass[11pt]{exam}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric,arrows,calc,matrix,tikzmark,positioning,patterns,decorations.markings,trees,angles,quotes,fit,math}
\printanswers
% \usepackage{showframe}
%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\includegraphics[width=8cm]{dir1/stairs.png}
\newcommand{\stepstemporary}{4*\tread}
\newcommand{\Width}{3.4}
\newcommand{\riserheight}{0.6}
\newcommand{\tread}{0.5}
\begin{tikzpicture}
\tikzstyle{outline}=[thick]
% \node(P) at (1.5,2.25) {\includegraphics[width=8cm]{dir1/stairs.png}};
\coordinate(A) at (0,0);
\coordinate(B) at ($(A)+(20:\Width)$);
\coordinate(C) at ($(A)+(90:\riserheight)+(155:\tread)$);
\coordinate(D) at ($(A)+(90:2*\riserheight)+(155:2*\tread)$);
\coordinate(E) at ($(A)+(90:3*\riserheight)+(155:3*\tread)$);
\coordinate(F) at ($(A)+(90:4*\riserheight)+(155:4*\tread)$);

%front side stringer
\draw[outline,fill=gray!50](A)--++(90:\riserheight)--++(155:\tread)--++(90:\riserheight)--++(155:\tread)--++(90:\riserheight)--++(155:\tread)--++(90:\riserheight)--++(155:\tread)--++(0,-4*\riserheight)--cycle;
%back side stringer
\draw[outline,fill=gray!50]($(A)+(20:\Width)$)--++(90:\riserheight)--++(155:\tread)--++(90:\riserheight)--++(155:\tread)--++(90:\riserheight)--++(155:\tread)--++(90:\riserheight)--++(155:\tread)--++(0,-4*\riserheight)--cycle;
%front riser
\draw[fill=gray!40](A)--++(20:\Width)--++(90:\riserheight)--++(200:\Width);
\draw[fill=gray!40](C)--++(20:\Width)--++(90:\riserheight)--++(200:\Width);
\draw[fill=gray!40](D)--++(20:\Width)--++(90:\riserheight)--++(200:\Width);
\draw[fill=gray!40](E)--++(20:\Width)--++(90:\riserheight)--++(200:\Width);
%%treads lighter colour
\draw[fill=gray!10](C)--++(20:\Width)--++(-25:\tread)--++(200:\Width)--cycle;

\draw[fill=gray!10](D)--++(20:\Width)--++(-25:\tread)--++(200:\Width)--cycle;

\draw[fill=gray!10](E)--++(20:\Width)--++(-25:\tread)--++(200:\Width)--cycle;
\draw[fill=gray!10](F)--++(20:\Width)--++(-25:\tread)--++(200:\Width)--cycle;

\draw[|-|]([shift=(300:5mm)]A)--node[sloped,fill=white]{120 cm}([shift=(300:5mm)]B);

\draw[|-|]([shift=(20:5mm)]$(C)+(20:\Width)$)--node[,xshift=1cm,fill=white]{20 cm}([shift=(20:5mm)]$(20:\Width)+(0,\riserheight)$);

\draw[|-|]([shift=(20:0mm)]$(A)+(20:\Width)+(0,3*\riserheight)$)--node[,xshift=1cm,fill=white]{18 cm}([shift=(20:0mm)]$(A)+(20:\Width)+(0,2*\riserheight)$);
% \foreach \i in {1,...\steps}
\end{tikzpicture}
\end{document}

我正在尝试完成这幅图: 楼梯

答案1

以下是一种方法:

在此处输入图片描述

如果您重新格式化前侧纵梁代码为

\draw[outline,fill=red!50](A)--++(90:\riserheight)
    --++(155:\tread)--++(90:\riserheight)
    --++(155:\tread)--++(90:\riserheight)
    --++(155:\tread)--++(90:\riserheight)
    --++(155:\tread)--++(0,-4*\riserheight)
    --cycle;

那么很容易看出这可以转换为foreach循环,如下所示:

\draw[outline,fill=red!50](A)--++(90:\riserheight)
\foreach \StairNum in {2,...,\steps} {
    --++(155:\tread)--++(90:\riserheight)
} 
--++(155:\tread)--++(0,-\steps*\riserheight)
-- cycle;

类似地,对于这些步骤,您只需要计算端点的新坐标:

\foreach \StairNum in {1,...,\steps} {
    \pgfmathtruncatemacro{\StairNumMinusOne}{\StairNum-1}%
    \coordinate (X) at ($(90:(\StairNumMinusOne*\riserheight)+(155:\StairNumMinusOne*\tread)$);
    \coordinate (Y) at ($(90:\StairNum*\riserheight)+(155:\StairNum*\tread)$);
    %front riser
    \draw[fill=blue!40](X)--++(20:\Width)--++(90:\riserheight)--++(200:\Width);
    %%treads lighter colour
    \draw[fill=blue!10](Y)--++(20:\Width)--++(-25:\tread)--++(200:\Width)--cycle;
}

笔记:

  • 我没有包括后侧纵梁因为无论如何这似乎是不可见的。
  • 尺寸标注也需要改进。

代码:

\documentclass[11pt]{exam}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric,arrows,calc,matrix,tikzmark,positioning,patterns,decorations.markings,trees,angles,quotes,fit,math}
\printanswers

\pagecolor{white}
% \usepackage{showframe}
%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
%\includegraphics[width=8cm]{dir1/stairs.png}
\newcommand{\steps}{7}
\newcommand{\stepstemporary}{\steps*\tread}
\newcommand{\Width}{3.4}
\newcommand{\riserheight}{0.6}
\newcommand{\tread}{0.5}
\begin{tikzpicture}
\tikzstyle{outline}=[thick]
% \node(P) at (1.5,2.25) {\includegraphics[width=8cm]{dir1/stairs.png}};
\coordinate(A) at (0,0);
\coordinate(B) at ($(A)+(20:\Width)$);
\coordinate(C) at ($(A)+(90:\riserheight)+(155:\tread)$);
\coordinate(D) at ($(A)+(90:2*\riserheight)+(155:2*\tread)$);
\coordinate(E) at ($(A)+(90:3*\riserheight)+(155:3*\tread)$);
\coordinate(F) at ($(A)+(90:4*\riserheight)+(155:4*\tread)$);

front side stringer
\draw[outline,fill=red!50](A)--++(90:\riserheight)
    --++(155:\tread)--++(90:\riserheight)
    --++(155:\tread)--++(90:\riserheight)
    --++(155:\tread)--++(90:\riserheight)
    --++(155:\tread)--++(0,-4*\riserheight)
    --cycle;
%%back side stringer
%\draw[outline,fill=red!50]($(A)+(20:\Width)$)--++(90:\riserheight)--++(155:\tread)--++(90:\riserheight)--++(155:\tread)--++(90:\riserheight)--++(155:\tread)--++(90:\riserheight)--++(155:\tread)--++(0,-4*\riserheight)--cycle;

\draw[outline,fill=black!50](A)--++(90:\riserheight)
\foreach \StairNum in {2,...,\steps} {
    --++(155:\tread)--++(90:\riserheight)
} 
--++(155:\tread)--++(0,-\steps*\riserheight)
-- cycle;

\foreach \StairNum in {1,...,\steps} {
    \pgfmathtruncatemacro{\StairNumMinusOne}{\StairNum-1}%
    \coordinate (X) at ($(90:(\StairNumMinusOne*\riserheight)+(155:\StairNumMinusOne*\tread)$);
    \coordinate (Y) at ($(90:\StairNum*\riserheight)+(155:\StairNum*\tread)$);
    %front riser
    \draw[fill=blue!40](X)--++(20:\Width)--++(90:\riserheight)--++(200:\Width);
    %%treads lighter colour
    \draw[fill=blue!10](Y)--++(20:\Width)--++(-25:\tread)--++(200:\Width)--cycle;
}

\draw[|-|]([shift=(300:5mm)]A)--node[sloped,fill=white]{120 cm}([shift=(300:5mm)]B);

\draw[|-|]([shift=(20:5mm)]$(C)+(20:\Width)$)--node[,xshift=1cm,fill=white]{20 cm}([shift=(20:5mm)]$(20:\Width)+(0,\riserheight)$);

\draw[|-|]([shift=(20:0mm)]$(A)+(20:\Width)+(0,3*\riserheight)$)--node[,xshift=1cm,fill=white]{18 cm}([shift=(20:0mm)]$(A)+(20:\Width)+(0,2*\riserheight)$);
\end{tikzpicture}
\end{document}

答案2

我制作了一个带有多个台阶的单元楼梯\stairs。它根据视角、\width\riserheight进行拉伸\tread

\documentclass[tikz, border=1cm]{standalone}
\begin{document}
\begin{tikzpicture}
\newcommand{\stairs}{5}
\newcommand{\width}{3.4}
\newcommand{\riserheight}{0.6}
\newcommand{\tread}{0.5}
\begin{scope}[x={(1.2*\tread cm,-0.8*\tread cm)}, y={(1*\width cm,0.8*\width cm)}, z={(0 cm,\riserheight cm)}]
\foreach \i in {1,...,\stairs} {
  \fill[gray!10] (\i-1,0,{\stairs-\i+1}) -- (\i-1,1,{\stairs-\i+1}) -- (\i,1,{\stairs-\i+1}) -- (\i,0,{\stairs-\i+1}) -- cycle;
  \fill[gray!40] (\i,0,{\stairs-\i}) -- (\i,1,{\stairs-\i}) -- (\i,1,{\stairs+1-\i}) -- (\i,0,{\stairs+1-\i}) -- cycle;
}
\draw[fill=gray!50] (0,0,0) -- (0,0,\stairs) \foreach \i in {1,...,\stairs} {-- (\i,0,{\stairs+1-\i}) -- (\i,0,{\stairs+1-\i-1})} -- cycle;
\draw (0,0,\stairs) -- (0,1,\stairs)  \foreach \i in {1,...,\stairs} {-- (\i,1,{\stairs+1-\i}) -- (\i,1,{\stairs+1-\i-1})} -- (\stairs,0,0);
\end{scope}
\end{tikzpicture}
\end{document}

楼梯

相关内容