我有一张图,其中颜色应沿导线渐变。确切地说,颜色应像正弦函数。
\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,red]{G-\i}{{(\i)*\pairdist-\wiredist},0}{\wireheight}{-\wirewidth}
\wire[thick,blue]{B-\i}{\i*\pairdist,0}{\wireheight}{\wirewidth}
}
\draw[<->] ($(G-1-2)!0.5!(G-1-3)$) -- +(-0.5,0) node[midway,above]{$\kappa_{2}$};
\draw[<->] ($(G-1-2)!-0.5!(G-2-3)$) -- +(-0.5,0) node[midway, above]{$\kappa_{1}$};
\end{tikzpicture}
\end{document}
我想要蓝色和红色的渐变色。比如底部为零,然后沿着电线增加,经过一个周期后达到初始值,以此类推。只是为了说明我的意思。这是一个周期。 我只是试图从电线的粗细来显示颜色的强度(没有其他东西可以显示)
答案1
我尝试使用Alain Matthes 的绝招针对您的 MWE,但由于错误而无法正常工作dimension too large
。更准确地说,我尝试使用着色部分。事实上,他提到他从 Mark Wilbrow 那里学到了着色部分,但我无法找到相应的帖子。如果阅读本文的人知道这一点,那么如果您能让我知道以便我可以给予适当的赞扬,那就太好了。
无论如何,我们可以通过放弃rounded corners
并用弧线手工绘制它们来使其工作。然后,我们可以采用稍微修改过的 Mark-Wibrow-Alain-Matthes 装饰版本,并使用正弦函数(而不是线性函数)来确定颜色的组成。
\documentclass[border=2mm,tikz]{standalone}
\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-#5) coordinate (#2-1)
arc(00:90:#5/2) arc(-90:-180:#5/2) coordinate (#2-2)
-- ++(0,#4-#5) coordinate (#2-3)
arc(180:90:#5/2) arc(-90:00:#5/2) coordinate (#2-4)
-- ++(0,#4-#5) coordinate (#2-5)
arc(00:90:#5/2) arc(-90:-180:#5/2) coordinate (#2-6)
-- ++(0,0.5*#4) coordinate (#2-7);
}
\usetikzlibrary{decorations}
\begin{document}
\makeatletter % starting point: from https://tex.stackexchange.com/a/14295/121799
\pgfkeys{/pgf/decoration/.cd,
start color/.store in =\startcolor,
end color/.store in =\endcolor
}
\newcounter{alongline}
\pgfdeclaredecoration{sinoidal color change}{initial}{
\state{initial}[width=0pt, next state=line]{\setcounter{alongline}{0}}
\state{line}[width=0.5pt]{%
%\pgfsetarrows{-}%
\stepcounter{alongline}
\pgfmathsetmacro{\x}{100*sin(1.8*\thealongline*(50pt/\pgfdecoratedpathlength))}
% \typeout{\thealongline:\x,\pgfdecoratedpathlength}
\pgfpathmoveto{\pgfpointorigin}%
\pgfpathlineto{\pgfqpoint{.75pt}{0pt}}%
\pgfsetstrokecolor{\endcolor!\x!\startcolor}%
\pgfusepath{stroke}%
}
\state{final}{%
%\pgfsetlinewidth{\pgflinewidth}%
\pgfpathmoveto{\pgfpointorigin}%
%\color{\endcolor!\x!\startcolor}%
\pgfusepath{stroke}%
}
}
\makeatother
\begin{tikzpicture}[>=stealth, shorten >=1pt, shorten <=1pt]
\foreach \i in {0,...,2}
{
\begin{scope}[xscale=-1]
\wire[thick,decoration={sinoidal color change,
start color=red, end color=blue},decorate]{G-\i}{{-(\i)*\pairdist-\wiredist},0}{\wireheight}{\wirewidth}
\end{scope}
\wire[thick,decoration={sinoidal color change,
start color=yellow, end color=red},decorate]{B-\i}{\i*\pairdist,0}{\wireheight}{\wirewidth}
}
\draw[black,<->] ($(G-1-2)!0.5!(G-1-3)$) -- ($(G-2-2)!0.5!(G-2-3)$) node[midway,above]{$\kappa_{2}$};
%\draw[<->] ($(G-1-2)!-0.5!(G-2-3)$) -- +(-0.5,0) node[midway, above]{$\kappa_{1}$};
\end{tikzpicture}
\end{document}
我尝试过模仿你的电线。如果这是你想要的方式,我很乐意改进装饰。
编辑:这是颜色在两种“边界颜色”之间振荡的版本。振荡次数由数字给出\NumMax
。
\documentclass[border=2mm,tikz]{standalone}
\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-#5) coordinate (#2-1)
arc(00:90:#5/2) arc(-90:-180:#5/2) coordinate (#2-2)
-- ++(0,#4-#5) coordinate (#2-3)
arc(180:90:#5/2) arc(-90:00:#5/2) coordinate (#2-4)
-- ++(0,#4-#5) coordinate (#2-5)
arc(00:90:#5/2) arc(-90:-180:#5/2) coordinate (#2-6)
-- ++(0,0.5*#4) coordinate (#2-7);
}
\usetikzlibrary{decorations}
\begin{document}
\makeatletter % starting point: from https://tex.stackexchange.com/a/14295/121799
\pgfkeys{/pgf/decoration/.cd,
start color/.store in =\startcolor,
end color/.store in =\endcolor
}
\newcounter{alongline}
\pgfmathsetmacro{\NumMax}{3}
\pgfdeclaredecoration{sinoidal color change}{initial}{
\state{initial}[width=0pt, next state=line]{\setcounter{alongline}{0}}
\state{line}[width=0.5pt]{%
%\pgfsetarrows{-}%
\stepcounter{alongline}
\pgfmathsetmacro{\x}{50*(1+cos(\NumMax*1.8*\thealongline*(50pt/\pgfdecoratedpathlength)))}
% \typeout{\thealongline:\x,\pgfdecoratedpathlength}
\pgfpathmoveto{\pgfpointorigin}%
\pgfpathlineto{\pgfqpoint{.75pt}{0pt}}%
\pgfsetstrokecolor{\endcolor!\x!\startcolor}%
\pgfusepath{stroke}%
}
\state{final}{%
%\pgfsetlinewidth{\pgflinewidth}%
\pgfpathmoveto{\pgfpointorigin}%
%\color{\endcolor!\x!\startcolor}%
\pgfusepath{stroke}%
}
}
\makeatother
\begin{tikzpicture}[>=stealth, shorten >=1pt, shorten <=1pt]
\foreach \i in {0,...,2}
{
\begin{scope}[xscale=-1]
\wire[thick,decoration={sinoidal color change,
start color=red, end color=blue},decorate]{G-\i}{{-(\i)*\pairdist-\wiredist},0}{\wireheight}{\wirewidth}
\end{scope}
\wire[thick,decoration={sinoidal color change,
start color=yellow, end color=red},decorate]{B-\i}{\i*\pairdist,0}{\wireheight}{\wirewidth}
}
\draw[black,<->] ($(G-1-2)!0.5!(G-1-3)$) -- ($(G-2-2)!0.5!(G-2-3)$) node[midway,above]{$\kappa_{2}$};
%\draw[<->] ($(G-1-2)!-0.5!(G-2-3)$) -- +(-0.5,0) node[midway, above]{$\kappa_{1}$};
\end{tikzpicture}
\end{document}