尝试理解代码,但无法将其用于新网络

尝试理解代码,但无法将其用于新网络

我有 3 线网络的代码。但我无法操纵它的新图。
这是因为我无法完全理解代码。这是我的代码

\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]{R-\i}{{(\i)*\pairdist-\wiredist},0}{\wireheight}{-\wirewidth}
        \wire[thick,blue]{B-\i}{\i*\pairdist,0}{\wireheight}{\wirewidth}
        \wire[thick,green]{G-\i}{{\i*\pairdist-0.5*\wiredist},0}{\wireheight}{0}
    }
    \end{tikzpicture}
\end{document}

但我正在尝试更改参数来实现这一点。在位置 1 和 2 处,红色和绿色颜色较深,但黑色没有。在上图中,在 3 处,红色和绿色颜色较浅(黑色不变)。

在此处输入图片描述

只要正确解释代码的工作原理,我就会非常感激。

答案1

事实是,您必须重新绘制函数中包含的内容,在我完成的代码中,我重新绘制了所有内容并重新定义了新节点,不仅是一条线,而且是一个形状,这使我能够获得错误的厚度变化,然后我添加了范围结构,这使我能够获得镜像般的绘图xscale=-1,并将所有内容一起移向特定的坐标,然后是第二个定义,使我能够绘制由两条线和中间线形成的单元;所以我可以画几个,节点ID也被使用,我还留下了一些命令来查找任何节点。

尝试改变坐标,这样你就能看到修饰符如何影响,这样你就可以被visual tikz 7.6 链接

结果: 在此处输入图片描述

梅威瑟:

\documentclass[border=2mm]{standalone}
\usepackage{tikz}
\definecolor{myred}{HTML}{8F3132}
\definecolor{mygreen}{HTML}{395D4E}
\usetikzlibrary{calc,arrows.meta}

\begin{document}
    \begin{tikzpicture}
    % New command to draw a single shape with named coordinates ans some controls like colors
    \def\Wire(#1)#2[#3](#4){% This draw one cell #1:position #2:color #3: Identifier #4: scale only 1 or -1
    \begin{scope}[shift={(#1)},xscale=#4]% To modify the shape you must redefine the drawing within scope
        \draw[fill,#2] (0,0)%Starting point
            to ++(0,-1) % when use ++ you find a point relative to the starting point but shifted ++(xshift,yshift)
            to [in=90, out=-90]++(-2,-1) coordinate (a#3) %line direction from initial node in -90 degree direction and comes to the second in 90 direction, drawing a curved path. 
            to ++(0,-2) coordinate (b#3)
            to [in=90, out=-90]++(2,-1) coordinate (c#3)
            to ++(0,-0.5) coordinate (d#3)
            to [in=90, out=-90]++(-1,-1.5) coordinate (e#3)
            to ++(0,-0.1) coordinate (f#3)
            to [in=90, out=-90]++(1,-1.5) coordinate (g#3)
            to ++(0,-1) coordinate (h#3)
            to ++(-0.1,0)%go reverse shifted 0.1
            to ++(0,1)
            to [in=-90, out=90]++(-1+0.08,1.5)% +0.08 to get 0.02 of shifting in this point
            to ++(0,0.1)
            to [in=-90, out=90]++(1-0.08,1.5) %- 0.08 to compensate the value substracted and return to 0.1 shift value.
            to ++(0,0.5)
            to [in=-90, out=90]++(-2,1) % idem
            to ++(0,2)
            to [in=-90, out=90]++(2+0.08,1)
            to ++(0,1)
            to cycle;
            \draw[draw=none] (a#3) -- (b#3) node [midway](text1){};\draw(text1)++(0.4,0) node[#2]{\sf 3};
            \draw[draw=none] (c#3) -- (d#3) node [midway](text2){};\draw(text2)++(-0.4,0) node[#2]{\sf 2};
            \draw[draw=none] (g#3) -- (h#3) node [midway](text3){};\draw(text3)++(-0.4,0) node[#2]{\sf 1};

    \end{scope}
    }

    \def\cell(#1)[#2]{%#1:position, #2:ID
        \begin{scope}[shift={(#1)}]
            \Wire(0,0){myred}[L#2](1)% All the cordinates are labeled as L(identifier) result in aL1,bL1 ... aL2,hL2...
            \draw[thick] (0.25,0) -- ++(0,-9.6); % the middle line
            \Wire(0.5,0){mygreen}[R#2](-1) %All the cordinates are labeled as R(identifier) result in aR1,bR1 ... aR2,hR2...
        \end{scope}
    }
    %Start drawing the thing...

    \cell(0,0)[1]
    \draw node at (0.25,0.5) {\sf Command; \verb+\cell(0,0)[1]+};   
    \cell(-5,0)[2]
    \draw node at (-4.75,0.5) {\sf Command; \verb+\cell(-5,0)[2]+};
    \cell(5,0)[3]
    \draw node at (5.25,0.5) {\sf Command; \verb+\cell(5,0)[3]+};
    \Wire(10,0){blue}[L4](1)
    \draw node (comment1) at (7.5,-10.5) {\sf Command; \verb+\Wire(10,0){blue}[L4](1)+};

    %Demostration nodes:
    \draw[-Stealth,shorten >= 5pt,] (comment1) -- (hL4);
    \draw[-Stealth,shorten >= 2pt,shorten <= 2pt] (fR2) -- (fL1) node [midway,align=center,anchor=south]{Node IDs \\ available};
    %Find nodes
    \draw node[anchor=east] at (bR1) {\sf Node \verb+bR1+} (bR1)[fill=red] circle (2pt);
    \draw node[anchor=east] at (aR2) {\sf Node \verb+aR2+} (aR2)[fill=red] circle (2pt);
    \draw node[anchor=west] at (aL2) {\sf Node \verb+aL2+} (aL2)[fill=red] circle (2pt);
    \draw node[anchor=east] at (eL3) {\sf Node \verb+eL3+} (eL3)[fill=red] circle (2pt);
    \draw node[anchor=east] at (fL4) {\sf Node \verb+fL4+} (fL4)[fill=red] circle (2pt);
    \end{tikzpicture}
\end{document}

更新

这是一个疯狂的想法;为什么不创建一个具有相同图形的裁剪区域并在其中按照文本的位置绘制着色器,这样做我得到:

结果: 在此处输入图片描述

更新的 MWE:

\documentclass[border=2mm]{standalone}
\usepackage{tikz}
\definecolor{myred}{HTML}{8F3132}
\definecolor{mygreen}{HTML}{395D4E}
\usetikzlibrary{calc,arrows.meta}

\begin{document}
    \begin{tikzpicture}
    % New command to draw a single shape with named coordinates ans some controls like colors
    \def\Wire(#1)#2[#3](#4){% This draw one cell #1:position #2:color #3: Identifier #4: scale only 1 or -1
    \begin{scope}[shift={(#1)},xscale=#4]% To modify the shape you must redefine the drawing within scope
        \draw[fill,#2] (0,0)%Starting point
            to ++(0,-1) % when use ++ you find a point relative to the starting point but shifted ++(xshift,yshift)
            to [in=90, out=-90]++(-2,-1) coordinate (a#3) %line direction from initial node in -90 degree direction and comes to the second in 90 direction, drawing a curved path. 
            to ++(0,-2) coordinate (b#3)
            to [in=90, out=-90]++(2,-1) coordinate (c#3)
            to ++(0,-0.5) coordinate (d#3)
            to [in=90, out=-90]++(-1,-1.5) coordinate (e#3)
            to ++(0,-0.1) coordinate (f#3)
            to [in=90, out=-90]++(1,-1.5) coordinate (g#3)
            to ++(0,-1) coordinate (h#3)
            to ++(-0.1,0)%go reverse shifted 0.1
            to ++(0,1)
            to [in=-90, out=90]++(-1+0.08,1.5)% +0.08 to get 0.02 of shifting in this point
            to ++(0,0.1)
            to [in=-90, out=90]++(1-0.08,1.5) %- 0.08 to compensate the value substracted and return to 0.1 shift value.
            to ++(0,0.5)
            to [in=-90, out=90]++(-2,1) % idem
            to ++(0,2)
            to [in=-90, out=90]++(2+0.08,1)
            to ++(0,1)
            to cycle;
            \draw[draw=none] (a#3) -- (b#3) node [midway](text1){};\draw(text1)++(0.4,0) node[#2]{\sf 3};
            \draw[draw=none] (c#3) -- (d#3) node [midway](text2){};\draw(text2)++(-0.4,0) node[#2]{\sf 2};
            \draw[draw=none] (g#3) -- (h#3) node [midway](text3){};\draw(text3)++(-0.4,0) node[#2]{\sf 1};
            \clip (0,0)%Starting point
            to ++(0,-1) % when use ++ you find a point relative to the starting point but shifted ++(xshift,yshift)
            to [in=90, out=-90]++(-2,-1)  %line direction from initial node in -90 degree direction and comes to the second in 90 direction, drawing a curved path. 
            to ++(0,-2) 
            to [in=90, out=-90]++(2,-1) 
            to ++(0,-0.5) 
            to [in=90, out=-90]++(-1,-1.5)
            to ++(0,-0.1)
            to [in=90, out=-90]++(1,-1.5)
            to ++(0,-1)
            to ++(-0.1,0)%go reverse shifted 0.1
            to ++(0,1)
            to [in=-90, out=90]++(-1+0.08,1.5)% +0.08 to get 0.02 of shifting in this point
            to ++(0,0.1)
            to [in=-90, out=90]++(1-0.08,1.5) %- 0.08 to compensate the value substracted and return to 0.1 shift value.
            to ++(0,0.5)
            to [in=-90, out=90]++(-2,1) % idem
            to ++(0,2)
            to [in=-90, out=90]++(2+0.08,1)
            to ++(0,1)
            to cycle;
            \fill[inner color=black,outer color=#2](text1) circle (1.5cm);
            \fill[inner color=black,outer color=#2](text2) circle (1.5cm);
            \fill[inner color=black,outer color=#2](text3) circle (1.5cm);

    \end{scope}
    }

    \def\cell(#1)[#2]{%#1:position, #2:ID
        \begin{scope}[shift={(#1)}]
            \Wire(0,0){red}[L#2](1)% All the cordinates are labeled as L(identifier) result in aL1,bL1 ... aL2,hL2...
            \draw[thick] (0.25,0) -- ++(0,-9.6); % the middle line
            \Wire(0.5,0){green}[R#2](-1) %All the cordinates are labeled as R(identifier) result in aR1,bR1 ... aR2,hR2...
        \end{scope}
    }
    %Start drawing the thing...

    \cell(0,0)[1]
    \draw node at (0.25,0.5) {\sf Command; \verb+\cell(0,0)[1]+};   
    \cell(-5,0)[2]
    \draw node at (-4.75,0.5) {\sf Command; \verb+\cell(-5,0)[2]+};
    \cell(5,0)[3]
    \draw node at (5.25,0.5) {\sf Command; \verb+\cell(5,0)[3]+};
    \Wire(10,0){blue}[L4](1)
    \draw node (comment1) at (7.5,-10.5) {\sf Command; \verb+\Wire(10,0){blue}[L4](1)+};

    %Demostration nodes:
    \draw[-Stealth,shorten >= 5pt,] (comment1) -- (hL4);
    \draw[-Stealth,shorten >= 2pt,shorten <= 2pt] (fR2) -- (fL1) node [midway,align=center,anchor=south]{Node IDs \\ available};
    %Find nodes
    \draw node[anchor=east] at (bR1) {\sf Node \verb+bR1+} (bR1)[fill=red] circle (2pt);
    \draw node[anchor=east] at (aR2) {\sf Node \verb+aR2+} (aR2)[fill=red] circle (2pt);
    \draw node[anchor=west] at (aL2) {\sf Node \verb+aL2+} (aL2)[fill=red] circle (2pt);
    \draw node[anchor=east] at (eL3) {\sf Node \verb+eL3+} (eL3)[fill=red] circle (2pt);
    \draw node[anchor=east] at (fL4) {\sf Node \verb+fL4+} (fL4)[fill=red] circle (2pt);
    \end{tikzpicture}
\end{document}

PSD:第 84 行的一些拼写错误已更正,第一个 MWE;感谢@manooooh。

答案2

我尝试使用Tikz:对路径进行着色而不进行任何填充是否可以定义填充中顶部、底部和中间颜色的位置?,但没有成功。

因此,我只是重新绘制了一些路径

\documentclass[border=2mm]{standalone}

\usepackage{xparse}
\usepackage[svgnames]{xcolor}
\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}
\NewDocumentCommand{\wire}{sO{}mmmmmO{left}}{%
\draw[#2,#3!50]
    (#5)            coordinate (#4-0)
    -- ++(0,#6)     coordinate (#4-1)
    -- ++(#7,0)     coordinate (#4-2)
    -- ++(0,#6)     coordinate (#4-3)
    -- ++(-#7,0)    coordinate (#4-4)
    -- ++(0,#6)     coordinate (#4-5)
    -- ++(#7,0)     coordinate (#4-6)
    -- ++(0,0.5*#6) coordinate (#4-7);
\IfBooleanF{#1}{\draw[#2,#3!80!black]($(#4-0)+(0,5pt)$) -- ($(#4-1)-(0,5pt)$) node [pos=.5, #8] {1} ($(#4-4)+(0,5pt)$) -- ($(#4-5)-(0,5pt)$) node [pos=.5, #8] {2};}
}

\begin{document}
    \begin{tikzpicture}[rounded corners=5pt,>=stealth, shorten >=1pt, shorten <=1pt]
    \foreach \i in {0,...,2}
    {
        \wire[thick]{red}{R-\i}{{(\i)*\pairdist-\wiredist},0}{\wireheight}{-\wirewidth}
        \wire[thick]{Green}{G-\i}{\i*\pairdist,0}{\wireheight}{\wirewidth}[right]
        \wire*[thick]{black}{B-\i}{{\i*\pairdist-0.5*\wiredist},0}{\wireheight}{0}
    }
    \end{tikzpicture}
\end{document}

在此处输入图片描述


编辑

\documentclass[border=2mm]{standalone}

\usepackage{xparse}
\usepackage[svgnames]{xcolor}
\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.2*(\wirewidth+\wiredist)} % distance between pairs of wires

% \wire[options]{name}{start}{height}{width}
\NewDocumentCommand{\wire}{sO{}mmmmmO{left}}{%
\draw[#2,#3!50]
    (#5)            coordinate (#4-0)
    -- ++(0,#6)     coordinate (#4-1)
    -- ++(#7,0)     coordinate (#4-2)
    -- ++(0,#6)     coordinate (#4-3)
    -- ++(-.8*#7,0)    coordinate (#4-4)
    -- ++(0,.5*#6)     coordinate (#4-5)
    -- ++(.8*#7,0)     coordinate (#4-6)
    -- ++(0,#6) coordinate (#4-7)
    -- ++(-#7,0) coordinate (#4-8)
    -- ++(0,.5*#6) coordinate (#4-9);
\IfBooleanF{#1}{\draw[#2,#3!80!black]($(#4-0)+(0,5pt)$) -- ($(#4-1)-(0,5pt)$) node [pos=.5, #8] {1} ($(#4-4)+(0,5pt)$) -- ($(#4-5)-(0,5pt)$) node [pos=.5, #8] {2} ($(#4-6)+(0,5pt)$) -- ($(#4-7)-(0,5pt)$) node [pos=.5, #8] {3};}
}

\begin{document}
    \begin{tikzpicture}[rounded corners=5pt,>=stealth, shorten >=1pt, shorten <=1pt]
    \foreach \i in {0,...,2}
    {
        \wire[thick]{red}{R-\i}{{(\i)*\pairdist-\wiredist},0}{\wireheight}{-\wirewidth}
        \wire[thick]{Green}{G-\i}{\i*\pairdist,0}{\wireheight}{\wirewidth}[right]
        \wire*[thick]{black}{B-\i}{{\i*\pairdist-0.5*\wiredist},0}{\wireheight}{0}
    }
    \end{tikzpicture}
\end{document}

在此处输入图片描述


编辑2

最后一次尝试…

您必须使用 3 个定义的线长度以及命令中定义的坐标\wire来满足您看到x×#7y×#6

\documentclass[border=2mm]{standalone}

\usepackage{xparse}
\usepackage[svgnames]{xcolor}
\usepackage{tikz}
\usetikzlibrary{calc}

\newcommand\wireheight{2} % height of one segment
\newcommand\wirewidth{1.2}  % width of a segment
\newcommand\wiredist{0.5} % distance between wires
\pgfmathsetmacro\pairdist{2.2*(\wirewidth+\wiredist)} % distance between pairs of wires

% \wire*[options]{color}{name}{start}{height}{width}[side for label]
\NewDocumentCommand{\wire}{sO{}mmmmmO{left}}{%
\draw[#2,#3!50]
    (#5)            coordinate (#4-0)
    -- ++(0,#6)     coordinate (#4-1)
    -- ++(.6*#7,0)     coordinate (#4-2)
    -- ++(0,#6)     coordinate (#4-3)
    -- ++(-.4*#7,0)    coordinate (#4-4)
    -- ++(0,.5*#6)     coordinate (#4-5)
    -- ++(.8*#7,0)     coordinate (#4-6)
    -- ++(0,#6) coordinate (#4-7)
    -- ++(-#7,0) coordinate (#4-8)
    -- ++(0,.5*#6) coordinate (#4-9);
\IfBooleanF{#1}{\draw[#2,#3!80!black]($(#4-0)+(0,5pt)$) -- ($(#4-1)-(0,5pt)$) node [pos=.5, #8] {1} ($(#4-4)+(0,5pt)$) -- ($(#4-5)-(0,5pt)$) node [pos=.5, #8] {2};
    \node at ($(#4-6)!.5!(#4-7)$) [#3!80!black, #8] {3};
    }
}

\begin{document}
    \begin{tikzpicture}[rounded corners=5pt,>=stealth, shorten >=1pt, shorten <=1pt]
    \foreach \i in {0,...,2}
    {
        \wire[thick]{red}{R-\i}{{(\i)*\pairdist-\wiredist},0}{\wireheight}{-\wirewidth}
        \wire[thick]{Green}{G-\i}{\i*\pairdist,0}{\wireheight}{\wirewidth}[right]
        \wire*[thick]{black}{B-\i}{{\i*\pairdist-0.5*\wiredist},0}{\wireheight}{0}
    }
    \end{tikzpicture}
\end{document}

相关内容