答案1
下面的代码定义了一个命令
\wire[options]{name}{start}{height}{width}
绘制从 开始的单根导线start
,其中每个套索又height
高又width
宽。重要点(起点、弯道、终点)被命名name-0
为name-7
。一个有用的选项是导线的颜色。例如,
\wire[green]{G-1}{0,0}{2}{1}
将从坐标处开始绘制一条绿色电线(0,0)
,每个绞索具有高度2
和宽度1
;这些点被命名G-1-0
为G-1-7
。
下面的代码使用\foreach
循环绘制了三对黑色/绿色的电线,电线名称分别为B-0
、G-0
、B-1
、G-1
、B-2
和G-2
。为了说明名称的用法,一些距离用一些任意文本标记。
\documentclass[border=2mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\newcommand\wireheight{2} % height of one segment
\newcommand\wirewidth{1} % width of a segment
\newcommand\wiredist{0.5} % distance between wires
\pgfmathsetmacro\pairdist{2*(\wirewidth+\wiredist)} % distance between pairs of wires
% \wire[options]{name}{start}{height}{width}
\newcommand\wire[5][]%
{\draw[#1]
(#3) coordinate (#2-0)
-- ++(0,#4) coordinate (#2-1)
-- ++(#5,0) coordinate (#2-2)
-- ++(0,#4) coordinate (#2-3)
-- ++(-#5,0) coordinate (#2-4)
-- ++(0,#4) coordinate (#2-5)
-- ++(#5,0) coordinate (#2-6)
-- ++(0,0.5*#4) coordinate (#2-7);
}
\begin{document}
\begin{tikzpicture}[rounded corners,>=stealth, shorten >=1pt, shorten <=1pt]
\foreach \i in {0,...,2}
{\wire[thick]{B-\i}{\i*\pairdist,0}{\wireheight}{\wirewidth}
\wire[thick,green]{G-\i}{{(\i+1)*\pairdist-\wiredist},0}{\wireheight}{-\wirewidth}
}
\draw[<-] ($(B-0-2)!0.5!(B-0-3)$) -- +(-0.5,0);
\draw[<-] ($(G-0-2)!0.5!(G-0-3)$) -- +(0.5,0) node[above]{5mm};
\draw[<-] ($(G-0-4)!0.5!(G-0-5)$) -- +(-0.5,0);
\draw[<-] ($(B-1-4)!0.5!(B-1-5)$) -- +(0.5,0) node[above]{7mm};
\draw[<->] ($(B-1-0)!0.5!(B-1-1)$) -- node[above]{10mm} ($(G-1-0)!0.5!(G-1-1)$);
\draw[<->] ($(G-1-2)!0.5!(G-1-3)$) -- node[above]{9mm} ($(B-2-2)!0.5!(B-2-3)$);
\end{tikzpicture}
\end{document}