% Author: Izaak Neutelings (November, 2018)
% page 8 https://archive.org/details/StaticAndDynamicElectricity
% https://tex.stackexchange.com/questions/56353/extract-x-y-coordinate-of-an-arbitrary-point-on-curve-in-tikz
% https://tex.stackexchange.com/questions/412899/tikz-calculate-and-store-the-euclidian-distance-between-two-coordinates

\documentclass[border=3pt,tikz]{standalone}
\usepackage{amsmath} % for \dfrac
\usepackage{bm}
\usepackage{physics}
\usepackage{tikz,pgfplots}
\usetikzlibrary{angles,quotes} % for pic (angle labels)
\usetikzlibrary{decorations.markings}
\tikzset{>=latex} % for LaTeX arrow head

\usepackage{xcolor}
\colorlet{Ecol}{orange!90!black}
\colorlet{EcolFL}{orange!80!black}
\colorlet{veccol}{green!45!black}
\colorlet{EFcol}{red!60!black}
\tikzstyle{charged}=[top color=blue!20,bottom color=blue!40,shading angle=10]
\tikzstyle{darkcharged}=[very thin,top color=blue!60,bottom color=blue!80,shading angle=10]
\tikzstyle{charge+}=[very thin,top color=red!80,bottom color=red!80!black,shading angle=-5]
\tikzstyle{charge-}=[very thin,top color=blue!50,bottom color=blue!70!white!90!black,shading angle=10]
\tikzstyle{gauss surf}=[green!80!black,top color=green!2,bottom color=green!80!black!70,shading angle=5,fill opacity=0.6]
\tikzstyle{gauss lid}=[gauss surf,middle color=green!80!black!20,shading angle=40,fill opacity=0.6]
\tikzstyle{gauss line}=[green!80!black]
\tikzstyle{gauss dashed line}=[green!60!black!80,dashed,line width=0.2]
\tikzstyle{metal}=[top color=black!5,bottom color=black!15,shading angle=30]
\tikzstyle{vector}=[->,thick,veccol]
\tikzstyle{normalvec}=[->,thick,blue!80!black!80]
\tikzstyle{EField}=[->,thick,Ecol]
\tikzstyle{EField dashed}=[dashed,Ecol,line width=0.6]
\tikzset{
  EFieldLine/.style={thick,EcolFL,decoration={markings,
                     mark=at position #1 with {\arrow{latex}}},
                     postaction={decorate}},
  EFieldLine/.default=0.5}
\tikzstyle{measure}=[fill=white,midway,outer sep=2]


\begin{document}


% PLANE field with electric field
\begin{tikzpicture}[x={(1.0cm,0)},y={(0.55cm,0.5cm)},z={(0,1.0cm)}]
  \def\H{2.0}
  \def\W{0.7}
  \def\L{3.5}
  \def\Ny{5}
  \def\Nz{4}
  \def\NEy{4}
  \def\NEz{3}
  \def\oEy{0.08*\W}
  \def\oEz{0.04*\H}
  \def\E{1.1}
  
  % ELECTRIC FIELD back
  \foreach \i [evaluate={\y=\oEy+(\i-0.5)*(\L-2*\oEy)/\NEy;}] in {1,...,\NEy}{
    \foreach \j [evaluate={\z=\oEz+(\j-0.5)*(\H-2*\oEz)/\NEz;}] in {1,...,\NEz}{
      \draw[EField,very thick] (-\E,\y,\z) --++ (\E,0,0);
    }
  }
  
  % AXIS
  \draw[->,thick] (1.6*\W,0,0) --++ (1.1*\W,0,0) node[right,scale=0.9] {$x$};
  
  % PLANE
  \draw[metal]
    (\W,0,0) --++ (0,\L,0) --++ (0,0,\H) --++ (0,-\L,0) -- cycle;
  \draw[metal]
    (0,0,0) --++ (\W,0,0) --++ (0,0,\H) --++ (-\W,0,0) -- cycle;
  \draw[metal]
    (0,0,\H) --++ (\W,0,0) --++ (0,\L,0) --++ (-\W,0,0) -- cycle;
  
  % ELECTRIC FIELD front
  \foreach \i [evaluate={\y=\oEy+(\i-0.5)*(\L-2*\oEy)/\NEy;}] in {1,...,\NEy}{
    \foreach \j [evaluate={\z=\oEz+(\j-0.5)*(\H-2*\oEz)/\NEz;}] in {1,...,\NEz}{
      \draw[EField,very thick] (\W,\y,\z) --++ (\E,0,0);
    }
  }
  %\node[Ecol,scale=1.05] at (\W+\E,0.9*\L,0.93*\H) {$\vb{E}_\text{ext}$};
  \node[Ecol,scale=1.05] at (-1.3*\E,0.9*\L,0.94*\H) {$\vb{E}_\text{ext}$};
  %\node[Ecol,scale=1.05] at (\W+1.3*\E,0.2*\L,0.1*\H) {$\vb{E}_\text{ext}$};
  
\end{tikzpicture}



% PLANE field with electric field
\def\H{3.5}
\def\W{1.4}
\def\NE{6}
\def\NQ{7}
\def\E{1.8}
\def\R{0.1}
\begin{tikzpicture}
  
  % SLAB
  \draw[metal] (-\W/2,-\H/2) rectangle ++(\W,\H);
  
  % ELECTRIC FIELD back
  \foreach \i [evaluate={\y=-\H/2+(\i-0.5)*\H/\NE;}] in {1,...,\NE}{
    %\draw[EFieldLine=0.62] (-\E,\y) -- (-\W/2,\y);
    %\draw[EFieldLine=0.56] (-\W/2,\y) -- (\W/2,\y);
    %\draw[EFieldLine=0.62] (\W/2,\y) -- (\E,\y);
    \draw[EFieldLine=0.54] (-\E,\y) -- (\E,\y);
  }
  \node[Ecol,above right] at (\E,0.43*\H) {$\vb{E}_\text{ext}$};
  
  % CHARGES
  \foreach \i [evaluate={\y=-0.92*\H/2+(\i-1)*0.92*\H/(\NQ-1);}] in {1,...,\NQ}{
    \draw[charge-] (-0.4*\W,\y) circle (\R) node[scale=0.6] {$-$};
    \draw[charge+] ( 0.4*\W,\y) circle (\R) node[scale=0.6] {$+$};
    \draw[EFieldLine=0.54,line width=0.5] (0.35*\W,\y) -- (-0.35*\W,\y);
  }
  \node[Ecol,above] at (0,\H/2) {$\vb{E}_\text{ind}=-\vb{E}_\text{ext}$};
 
\end{tikzpicture}



% PLANE field with electric field
\begin{tikzpicture}
  
  % SLAB
  \draw[metal] (-\W/2,-\H/2) rectangle ++(\W,\H);
  
  % ELECTRIC FIELD back
  \foreach \i [evaluate={\y=-\H/2+(\i-0.5)*\H/\NE;}] in {1,...,\NE}{
    \draw[EFieldLine=0.62] (-\E,\y) -- (-\W/2,\y);
    \draw[EFieldLine=0.62] (\W/2,\y) -- (\E,\y);
  }
  \node[Ecol,above right] at (\E,0.43*\H) {$\vb{E}_\text{ext}$};
  
  % CHARGES
  \foreach \i [evaluate={\y=-0.92*\H/2+(\i-1)*0.92*\H/(\NQ-1);}] in {1,...,\NQ}{
    \draw[charge-] (-0.4*\W,\y) circle (\R) node[scale=0.6] {$-$};
    \draw[charge+] ( 0.4*\W,\y) circle (\R) node[scale=0.6] {$+$};
  }
  \node[Ecol] at (0,0.08*\H) {$\vb{E}=0$};
 
\end{tikzpicture}



% PLANE field with electric field
\begin{tikzpicture}
  \def\h{0.38*\H}
  \def\w{0.68*\W}
  \def\r{0.08*\W}
  
  % SLAB
  \draw[metal] (-\W/2,-\H/2) rectangle ++(\W,\H);
  
  \begin{scope}
    \clip ({(\W+\w)/2},0.6*\h) rectangle ++(\w/2,-1.2*\h);
    \draw[gauss lid]
      ({(\W+\w)/2},\h/2) arc (90:450:{\r} and {\h/2});
  \end{scope}
  
  % ELECTRIC FIELD
  \foreach \i [evaluate={\y=-\H/2+(\i-0.5)*\H/\NE;}] in {1,...,\NE}{
    \draw[EFieldLine=0.62] (-\W/2,\y) -- (-\E,\y);
    \draw[EFieldLine=0.62] (\W/2,\y) -- (\E,\y);
  }
  %\node[Ecol,above right] at (\E,0.43*\H) {$\vb{E}_\text{ext}$};
  
  % CHARGES
  \foreach \i [evaluate={\y=-\H/2+(\i-0.5)*\H/\NQ;}] in {1,...,\NQ}{
    \node[blue!80!black,scale=0.8] at (-0.4*\W,\y) {$+$};
    \node[blue!80!black,scale=0.8] at ( 0.4*\W,\y) {$+$};
  }
  \node[Ecol] at (0,0.35*\H) {$\vb{E}=0$};
  
  % GAUSS IN FRONT
  \draw[gauss line, dashed]
    ({(\W-\w)/2},-\h/2) arc (-90:-450:{\r} and {\h/2});
  \draw[gauss surf]
    ({(\W-\w)/2},-\h/2) arc (-90:-270:{\r} and {\h/2}) --++
    (\w,0) arc (90:270:{\r} and {\h/2}) -- cycle;
  \begin{scope}
    \clip ({(\W+\w)/2},\h/2) rectangle ++(-\w/2,-\h);
    \draw[gauss lid]
      ({(\W+\w)/2},\h/2) arc (90:450:{\r} and {\h/2});
  \end{scope}
   
\end{tikzpicture}



\end{document}
