我有一条很长的路径,想以迭代的方式绘制它的一部分
\documentclass{article}
\usepackage{tikz}
\begin{document}
\usetikzlibrary{shapes,arrows}
\usetikzlibrary{calc}
\usetikzlibrary{shapes,arrows}
\begin{tikzpicture}
\newcommand{\room}{
\coordinate (a) at (0,0) ;
\coordinate (b) at (5,0);
\coordinate (c) at (5,5);
\coordinate (d) at (0,4);
\coordinate (e) at (1,3);
\coordinate (f) at (1.4,3) ;
\coordinate (g) at (1.4,2.6) ;
\coordinate (h) at (1,2.6) ;
\coordinate (i) at (1,3) ;
}
\begin{scope}[xshift= 0cm]
\room
\draw plot coordinates{(a) (b)};
\end{scope}
\begin{scope}[xshift= 6cm]
\room
\draw plot coordinates{(a) (b) (c)};
\end{scope}
\begin{scope}[xshift= 12cm]
\room
\draw plot coordinates{(a) (b) (c) (d)};
\end{scope}
%... more plots to come here
\end{tikzpicture}
\end{document}
答案1
你是指这样的吗?
\documentclass{article}
\usepackage{tikz}
\begin{document}
\usetikzlibrary{shapes,arrows}
\usetikzlibrary{calc}
\begin{tikzpicture}[scale=0.2]
\newcommand{\room}{
\coordinate (a1) at (0,0) ;
\coordinate (a2) at (5,0);
\coordinate (a3) at (5,5);
\coordinate (a4) at (0,4);
\coordinate (a5) at (1,3);
\coordinate (a6) at (1.4,3) ;
\coordinate (a7) at (1.4,2.6) ;
\coordinate (a8) at (1,2.6) ;
\coordinate (a8) at (1,3) ;
}
\foreach \i in {2,...,8}{
\pgfmathsetmacro{\li}{\i-1}
\pgfmathsetmacro{\si}{6*\i}
\begin{scope}[xshift=\si cm]
\room
\foreach \k in {1,...,\li}
{
\pgfmathsetmacro{\hk}{\k+1}
\draw plot coordinates{(a\k) (a\hk)};
}
\end{scope}
}
\end{tikzpicture}
\end{document}
答案2
一种解决方案可能是引入另一个命令,\drawcurrentroom
该命令创建房间、绘制当前路径并改变下一个图像的距离。
为了移动图像我使用了昨天向您展示的方法。
这是我的 MWE:
\documentclass{article}
\usepackage{tikz}
\newlength\mydistance
\setlength{\mydistance}{0cm}
\usetikzlibrary{shapes,arrows}
\usetikzlibrary{calc}
\usetikzlibrary{shapes,arrows}
\newcommand{\room}{
\coordinate (a) at (0,0) ;
\coordinate (b) at (5,0);
\coordinate (c) at (5,5);
\coordinate (d) at (0,4);
\coordinate (e) at (1,3);
\coordinate (f) at (1.4,3) ;
\coordinate (g) at (1.4,2.6) ;
\coordinate (h) at (1,2.6) ;
\coordinate (i) at (1,3) ;
}
\newcommand{\drawcurrentroom}[1]{
\begin{scope}[xshift=\mydistance]
\room
\foreach \x/\y in {#1}
\draw (\x)--(\y);
\end{scope}
\addtolength{\mydistance}{6cm}
}
\begin{document}
\begin{tikzpicture}[scale=0.85]
\drawcurrentroom{a/b}
\drawcurrentroom{a/b,b/c}
\drawcurrentroom{a/b,b/c,c/d}
\end{tikzpicture}
\end{document}
该命令\drawcurrentroom
将连接列表作为参数:它们应该用逗号分隔;例如:
\drawcurrentroom{a/b,b/c}
意味着有一条从a
到 的路径b
,还有另一条从b
到 的路径c
。如果你总是使用递增序列,也许可以实现更多的自动化工作,但我认为这很容易。
图形结果为:
编辑我忘了说了,当你画另一幅图时最好将长度重置为 0;因此:
\begin{tikzpicture}[scale=0.85]
\drawcurrentroom{a/b}
\drawcurrentroom{a/b,b/c}
\drawcurrentroom{a/b,b/c,c/d}
\end{tikzpicture}
\setlength{\mydistance}{0cm}
\begin{tikzpicture}[scale=0.85]
\drawcurrentroom{a/b}
\drawcurrentroom{a/b,b/c}
\drawcurrentroom{a/b,b/c,c/d}
\end{tikzpicture}