3

I have a code for 3 wires network. But I'm not able to manipulate it new figure.
This is because I'm not able to understand the code completely. This is my code

\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}

But I am trying to change the parameters to get this. At places 1 and 2 we have darker colors for red and green but not for black. At 3 we have light color for red and green (color black is constant) in the above figure.

enter image description here

Only even the working of the code is explained properly, I will be very grateful.

AboAmmar
  • 46,352
  • 4
  • 58
  • 127
Shamina
  • 1,078
  • Do you want to have a gradient or sharp transitions between the lighter and darker regions? –  Jul 24 '18 at 11:42
  • @marmot Sharp transition, exactly at position 1 and 2. But it should mimic the above figure. – Shamina Jul 24 '18 at 11:55
  • But then you already got an answer which does it, and I'd recommend to add a comment about a potentially missing feature there. –  Jul 24 '18 at 11:59
  • @marmot Spacings are different and also the segment at 1 is larger than segment at 2. Then network bulge out at 3, where green and red meet. – Shamina Jul 24 '18 at 12:09

2 Answers2

8

The fact is that you must redraw what is contained in the function, in the code that I have done, I have redrawn everything and redifined new nodes, not just a line, but a shape, which allows me to obtain a false variation in thickness, then I have added the scope structure, which allows me to obtain the mirror-like drawing xscale=-1, and move everything together towards a specific coordinate, then a second definition that allows me to draw a cell formed by the two wires and the middle line; so I can draw several, node IDs are also used, I also leave some commands to find any node.

Try changing the coordinates so that you can see how the modifiers affect, so you can be guided by the visual tikz 7.6 links

RESULT: enter image description here

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};

    \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}

Update

In a crazy idea; why not to create a cropped area with the same figure and draw within it the shaders in the positions of the texts, doing that I get:

RESULT: enter image description here

UPDATED 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: some spelling error corrected in line 84, first MWE; thanks to @manooooh.

J Leon V.
  • 11,533
  • 16
  • 47
1

I tried to use Tikz: Shading a path without any filling and Is it possible to define the position of top bottom and middle color in fill?, with no success.

Thus, I just re-draw some paths

\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}

enter image description here


Edit

\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}

enter image description here


Edit 2

Very last attempt…

You have to play with the 3 defined wire lengths plus with the coordinates defined in the \wire command to fit your needs where you see x×#7 or y×#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}
NBur
  • 4,326
  • 10
  • 27
  • Thanks for your answer but it's different from the figure I have. – Shamina Jul 24 '18 at 11:56
  • That is to say? What is missing? – NBur Jul 24 '18 at 11:59
  • Spacings are not same at 1,2 and 3. Also length of the segment at point 1 is longer than 2. There is no 3rd part in your figure (where network bulge out at 3, where green and red meet). – Shamina Jul 24 '18 at 12:06
  • 1
    I guess you'll be able to make segments marked 3 ligther… – NBur Jul 24 '18 at 14:43
  • Wonderful! But is there a way to bulge out at 3 and make green and red closer than before. Else, there is no way to differentiate between actual 3 and segment between 1 and 2. And 3 I don't want darker color but the usual, only at 1 and 2, I want darker. Many thanks – Shamina Jul 24 '18 at 14:55