7

I'm trying to draw the following diagram in tikz

enter image description here

I wrote the following code and the regenerated graph is below:

\documentclass[tikz,border=5mm]{standalone}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{rotating}


\begin{document}
\begin{tikzpicture}[line width=0.5mm]

    \draw (0,0) circle (3cm);

    \begin{rotate}{60}

        \draw (-1.475,0) -- (1.475,0);
        \draw (-1.87,0) -- (-4,0);
        \draw[->, >=stealth] (1.87,0) -- (4,0) node[anchor= south, style={rotate=-60}] {$\boldsymbol{\gamma}$};
        \draw (0,-1.475) -- (0,1.475);
        \draw (0,-1.87) -- (0,-4);
        \draw[->, >=stealth] (0,1.87) -- (0,4) node[anchor= east, style={rotate=-60}] {$\boldsymbol{\delta}$};

        \draw (1.75,0.15) arc (5:85:1.76);
        \draw (-0.15,1.75) arc (95:175:1.76);
        \draw (-1.75,-0.15) arc (185:265:1.76);
        \draw (0.15,-1.75) arc (275:355:1.76);

        \draw (1.775, -0.15) -- (1.475,-0.15) -- (1.475,0.15) -- (1.775,0.15) node[anchor= north,xshift=-0.2cm,yshift=0.3cm,style={rotate=-60}] {$\boldsymbol{i_{r\delta}}$};
        \draw (-0.15,1.775) -- (-0.15,1.475) -- (0.15,1.475) -- (0.15,1.775) node[anchor= south,xshift=0cm,yshift=-0.6cm,style={rotate=-60}] {$\boldsymbol{i_{r\gamma}}$};
        \draw (-1.775, 0.15) -- (-1.475,0.15) -- (-1.475,-0.15) -- (-1.775,-0.15);
        \draw (0.15,-1.775) -- (0.15,-1.475) -- (-0.15,-1.475) -- (-0.15,-1.775);

        \draw[blue] (1.64,0) node {$\otimes$};
        \draw[blue] (-1.64,0) node {$\odot$};
        \draw[red] (0,1.64) node {$\odot$};
        \draw[red] (0,-1.64) node {$\otimes$};

    \end{rotate}

    \draw (-1.75,0) -- (1.75,0);
    \draw (-2.125,0) -- (-4,0);
    \draw[->, >=stealth] (2.125,0) -- (4,0) node[anchor= west] {$\boldsymbol{\alpha}$};
    \draw (0,-1.75) -- (0,1.75);
    \draw (0,-2.125) -- (0,-4);
    \draw[->, >=stealth] (0,2.125) -- (0,4) node[anchor= south] {$\boldsymbol{\beta}$};

    \draw (1.85,0.15) arc (5:85:1.87);
    \draw (-0.15,1.85) arc (95:175:1.87);
    \draw (-1.85,-0.15) arc (185:265:1.87);
    \draw (0.15,-1.85) arc (275:355:1.87);

    \draw (1.825, -0.15) -- (2.125,-0.15) -- (2.125,0.15) -- (1.825,0.15) node[anchor= west,xshift=0.2cm,yshift=0.1cm] {$\boldsymbol{i_{s\beta}}$};
    \draw (-0.15,1.825) -- (-0.15,2.125) -- (0.15,2.125) -- (0.15,1.825) node[anchor= south,xshift=0.3cm,yshift=0.1cm] {$\boldsymbol{i_{s\alpha}}$};
    \draw (-1.825, 0.15) -- (-2.125,0.15) -- (-2.125,-0.15) -- (-1.825,-0.15);
    \draw (0.15,-1.825) -- (0.15,-2.125) -- (-0.15,-2.125) -- (-0.15,-1.825);

    \draw[blue] (1.96,0) node {$\otimes$};
    \draw[blue] (-1.96,0) node {$\odot$};
    \draw[red] (0,1.96) node {$\odot$};
    \draw[red] (0,-1.96) node {$\otimes$};



    \draw[->, >=stealth] (0.5,0) arc (0:60:0.5) node[pos=0.5 ,anchor= west] {$\boldsymbol{\varepsilon_{rs}}$};



\end{tikzpicture}

\end{document}

enter image description here

The result seems to be good. I wanted to know, is there a better and easier way to write the code?

2
  • 3
    It is generally better to draw long continuous paths. If you do the two interior circles in the same path and fill the interior with while, you could then overlap the background arrows (instead of breaking them into segments). Aug 31, 2018 at 19:37
  • 1
    In addition to what @JohnKormylo says, I have never seen \begin{rotating}... inside a tikzpicture, instead of style={rotate=-60} you may want just to say rotate=60, instead of ` \draw[->, >=stealth]` just ` \draw[-stealth]. Further, you may make little pics instead of \otimes` and \odot since there you can control the line width. Otherwise congratulations to your nice picture!
    – user121799
    Aug 31, 2018 at 21:00

1 Answer 1

8

Just for fun, an option using calc, arrows.meta, backgrounds, and patterns, to draw the cuted shapes using node coordinates and only one calculation for the radious and the angle from the node box. then using contour to put in the labels inside the pattern, for the math fonts I use mathptmx.

RESULT:

enter image description here

MWE:

\documentclass[tikz,border=20pt]{standalone}
\usepackage{mathptmx}
\usepackage[outline]{contour}
\usetikzlibrary{calc,arrows.meta,patterns,backgrounds}

\contourlength{1.1pt}
%Create a new patern for firefox and adobe reader from https://tex.stackexchange.com/a/219808/154390
\pgfdeclarepatternformonly{north east lines b}{\pgfqpoint{0pt}{0pt}}{\pgfqpoint{3.4pt}{3.4pt}}{\pgfqpoint{3.4pt}{3.4pt}}%
{
  \pgfsetlinewidth{0.25pt}
  %Principal line
  \pgfpathmoveto{\pgfqpoint{0pt}{0pt}}
  \pgfpathlineto{\pgfqpoint{3.4pt}{3.4pt}}
  %Complement line north east
  \pgfpathmoveto{\pgfqpoint{-1pt}{2.4pt}}
  \pgfpathlineto{\pgfqpoint{1pt}{4.4pt}}
  %Complement line south west
  \pgfpathmoveto{\pgfqpoint{2.4pt}{-1pt}}
  \pgfpathlineto{\pgfqpoint{4.4pt}{1pt}}
  \pgfusepath{stroke}
}

\begin{document}
    \begin{tikzpicture}[
        >={Stealth[inset=0,length=5pt]}
    ]
    \def\Current[#1][#2](#3)(#4)#5#6[#7]%1: Angle 2: radious 3:type 4:ID 5:color 6:Label text 7:polar text position.
        \draw (#1:#2)++(#7:0.5) node {\contour{white}{#6}};
        \begin{scope}[shift={(#1:#2)}, rotate=#1-90,transform shape]
            \node[inner sep=0,minimum size=13pt](I#4) at (0,0){};
            \ifnum#3=0 \draw[#5,thick](45:5pt) -- (-135:5pt) (-45:5pt) -- (135:5pt) (0,0) circle (5pt); \fi
            \ifnum#3=1 \draw[#5,thick](0,0) circle (5pt);\fill[#5](0,0) circle (2pt); \fi       
        \end{scope}
    }

    %Drawing Stator?
    \Current[0][2](0)(1){blue}{$i_{s\beta}$}[-90]
    \Current[90][2](1)(2){red}{$i_{s\alpha}$}[10]
    \Current[180][2](1)(3){blue}{$i_{s\beta}$}[100]
    \Current[270][2](0)(4){red}{$i_{s\alpha}$}[0]

    \begin{scope}[on background layer]
    \draw[thick,even odd rule,pattern=north east lines b,pattern color=blue]
        let \p1 = (I1.south west),
            \n1 = {veclen(\x1,\y1)},
            \n2 = {atan2(\y1,\x1)}
        in  (\n2:\n1) 
                arc (\n2:90-\n2:\n1) -- (I2.north east) -| (I2.south west) 
                arc (90+\n2:180-\n2:\n1) -- (I3.north east) |- (I3.south west)
                arc (180+\n2:270-\n2:\n1) -- (I4.north east) -| (I4.south west)
                arc (270+\n2:360-\n2:\n1) -- (I1.north east) |- (I1.south west)
        (0,0) circle (3);
    \draw[->,thick](I2.north) -- ++ (0,1.5) node[anchor=45]{$\beta$};
    \draw[->,thick](I1.north) -- ++ (1.5,0) node[anchor=45]{$\alpha$};
    \draw[thick]
        (I1.south) -- (I3.south)
        (I3.north) -- ++ (-1.5,0)
        (I2.south) -- (I4.south)
        (I4.north) -- ++ (0,-1.5);
    \end{scope}

    %Drawing Rotor?
    \contourlength{0pt}
    \begin{scope}[rotate=60]
    \Current[0][1.4](0)(5){blue}{$i_{r\delta}$}[100]
    \Current[90][1.4](1)(6){red}{$i_{r\gamma}$}[-20]
    \Current[180][1.4](1)(7){blue}{$i_{r\delta}$}[70]
    \Current[270][1.4](0)(8){red}{$i_{r\gamma}$}[160]
        \begin{scope}[on background layer]
            \draw[thick,fill=black, fill opacity=0.2]
                let \p1 = (I5.north west),
                    \n1 = {veclen(\x1,\y1)},
                    \n2 = {atan2(\y1,\x1)}
                in  (\n2:\n1) 
                        arc (\n2:90-\n2:\n1) -- (I6.south east) -| (I6.north west) 
                        arc (90+\n2:180-\n2:\n1) -- (I7.south east) |- (I7.north west)
                        arc (180+\n2:270-\n2:\n1) -- (I8.south east) -| (I8.north west)
                        arc (270+\n2:360-\n2:\n1) -- (I5.south east) |- (I5.north west);
            \draw[->,thick, shorten >=10pt](I6.north) -- ++ (0,2.4) node{$\delta$};
            \draw[->,thick, shorten >=10pt](I5.north) -- ++ (2.4,0) node{$\gamma$};
            \draw[thick]
                (I5.south) -- (I7.south)
                (I7.north) -- ++ (-2,0)
                (I6.south) -- (I8.south)
                (I8.north) -- ++ (0,-2);
        \end{scope}
    \end{scope}
    \draw[->,thick] (0:0.7) arc (0:60:0.7) node[pos=0.5 ,anchor= west] {$\varepsilon_{rs}$};
    \end{tikzpicture}
\end{document}

ANIMATION:

Patterns are avoided because generates bad output in gif compression consequently contour is not necessary...

RESULT:

enter image description here

MWE: The foreach loop gives an error but generates the necesary frames in nonstopmode then I convert the pdf output to gif using Imagemagick converter.

% arara: pdflatex: {synctex: yes, action: nonstopmode}
% arara: animate: {density: 150, delay: 15 , other: -background white -alpha remove}
% arara: showanimate
\documentclass[tikz,border=0pt]{standalone}
\usepackage{mathptmx}
\usetikzlibrary{calc,arrows.meta,backgrounds}

\begin{document}
    \foreach \z in {1,2,...,30}{

    \begin{tikzpicture}[
    >={Stealth[inset=0,length=5pt]}
    ]
    \pgfmathparse{int(\z*360/30)}
    \edef\Rotate{\pgfmathresult}
    \path(0,0) circle (4.5cm);
    \def\Current[#1][#2](#3)(#4)#5#6[#7]{%1: Angle 2: radious 3:type 4:ID 5:color 6:Label text 7:polar text position.
        \draw (#1:#2)++(#7:0.5) node {#6};
        \begin{scope}[shift={(#1:#2)}, rotate=#1-90,transform shape]
        \node[inner sep=0,minimum size=13pt](I#4) at (0,0){};
        \ifnum#3=0 
            \draw[#5,fill=white,thick](0,0) circle (5pt);
            \draw[#5,thick](45:5pt) -- (-135:5pt) (-45:5pt) -- (135:5pt); 
        \fi
        \ifnum#3=1 
            \draw[#5,fill=white,thick](0,0) circle (5pt);\fill[#5](0,0) circle (2pt); \fi       
        \end{scope}
    }

    %Drawing Stator?
    \Current[0][2](0)(1){blue}{$i_{s\beta}$}[-90]
    \Current[90][2](1)(2){red}{$i_{s\alpha}$}[10]
    \Current[180][2](1)(3){blue}{$i_{s\beta}$}[100]
    \Current[270][2](0)(4){red}{$i_{s\alpha}$}[0]

    \begin{scope}[on background layer]
    \draw[thick,even odd rule,fill=blue!20]
    let \p1 = (I1.south west),
    \n1 = {veclen(\x1,\y1)},
    \n2 = {atan2(\y1,\x1)}
    in  (\n2:\n1) 
    arc (\n2:90-\n2:\n1) -- (I2.north east) -| (I2.south west) 
    arc (90+\n2:180-\n2:\n1) -- (I3.north east) |- (I3.south west)
    arc (180+\n2:270-\n2:\n1) -- (I4.north east) -| (I4.south west)
    arc (270+\n2:360-\n2:\n1) -- (I1.north east) |- (I1.south west)
    (0,0) circle (3);
    \draw[->,thick](I2.north) -- ++ (0,1.5) node[anchor=45]{$\beta$};
    \draw[->,thick](I1.north) -- ++ (1.5,0) node[anchor=45]{$\alpha$};
    \draw[thick]
    (I1.south) -- (I3.south)
    (I3.north) -- ++ (-1.5,0)
    (I2.south) -- (I4.south)
    (I4.north) -- ++ (0,-1.5);
    \end{scope}

    %Drawing Rotor?
    \begin{scope}[rotate=\Rotate]
    \Current[0][1.4](0)(5){blue}{$i_{r\delta}$}[100]
    \Current[90][1.4](1)(6){red}{$i_{r\gamma}$}[-20]
    \Current[180][1.4](1)(7){blue}{$i_{r\delta}$}[70]
    \Current[270][1.4](0)(8){red}{$i_{r\gamma}$}[160]
    \begin{scope}[on background layer]
    \draw[thick,fill=black, fill opacity=0.2]
    let \p1 = (I5.north west),
    \n1 = {veclen(\x1,\y1)},
    \n2 = {atan2(\y1,\x1)}
    in  (\n2:\n1) 
    arc (\n2:90-\n2:\n1) -- (I6.south east) -| (I6.north west) 
    arc (90+\n2:180-\n2:\n1) -- (I7.south east) |- (I7.north west)
    arc (180+\n2:270-\n2:\n1) -- (I8.south east) -| (I8.north west)
    arc (270+\n2:360-\n2:\n1) -- (I5.south east) |- (I5.north west);
    \draw[->,thick, shorten >=10pt](I6.north) -- ++ (0,2.4) node{$\delta$};
    \draw[->,thick, shorten >=10pt](I5.north) -- ++ (2.4,0) node{$\gamma$};
    \draw[thick]
    (I5.south) -- (I7.south)
    (I7.north) -- ++ (-2,0)
    (I6.south) -- (I8.south)
    (I8.north) -- ++ (0,-2);
    \end{scope}
    \end{scope}
    \draw[->,thick]
        (0:0.7) arc (0:\Rotate:0.7);
    \draw(\Rotate-15:1) node{$\varepsilon_{rs}$};
    \end{tikzpicture}%
}
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .