介电材料中的电荷

介电材料中的电荷

我正在尝试寻找一种简单的方法来绘制它,但找不到解决方案

在此处输入图片描述

答案1

这是以下解决方案约翰·科米洛的评论。我使用\foreach命令绘制所有椭圆,但我旋转每个椭圆并根据其位置更改其短轴。

\documentclass[border=2mm]{standalone}
\usepackage{tikz}

\definecolor{positive}{HTML}{FADDB9}
\definecolor{negative}{HTML}{7BC192}

\begin{document}
\begin{tikzpicture}
\foreach\y in {-3,...,3} \foreach\x in {-4,...,4} % row, column
{%
  % atan2 implementation
  \ifnum\x = 0
    \ifnum\y > 0
      \def\a{90}
    \else
      \def\a{-90} 
    \fi
  \else\ifnum\x > 0
    \pgfmathsetmacro\a{atan(\y/\x)}     % angle in quadrants  I and IV
  \else
    \pgfmathsetmacro\a{atan(\y/\x)+180} % angle in quadrants II and III
  \fi\fi
  % dipoles
  \pgfmathtruncatemacro\b{\x*\x+\y*\y}
  \ifnum\b > 0
    \pgfmathsetmacro\bb{0.08*sqrt(\b)} % semi-axis b = distance to the origin (scaled)
    \begin{scope}[shift={(\x,\y)},rotate=\a]
      \fill[positive] (0,\bb) arc (90:-90:0.4 and \bb);
      \fill[negative] (0,\bb) arc (90:270:0.4 and \bb);
      \draw (0,0) ellipse (0.4 and \bb);
      \node[transform shape,scale=0.6] at ( 0.2,0) {$+$};
      \node[transform shape,scale=0.6] at (-0.2,0) {$-$};
    \end{scope}
  \fi
}
% central charge
\draw[fill=red] (0,0) circle (0.15) node[scale=0.6] {$+$} node [red,below left] {$q$};
\end{tikzpicture}
\end{document}

在此处输入图片描述 编辑:两个变化:

  1. 下列的塞巴斯蒂亚诺根据我的建议,我修改了代码以适应正电荷和负电荷。我添加了一个参数q,该参数应取值 +1、-1,并相应地改变 x 轴。然后我需要一个\ifnum用于显示中心电荷的参数。
  2. atan2。为什么不使用内置函数?你会想。我也是……

新的(最短的)代码:

\documentclass[border=2mm]{standalone}
\usepackage{tikz}

\definecolor{positive}{HTML}{FADDB9}
\definecolor{negative}{HTML}{7BC192}

\def\q{-1} % central charge: +1,-1 we only need the sign

\begin{document}
\begin{tikzpicture}
\foreach\y in {-3,...,3} \foreach\x in {-4,...,4} % row, column
{%
  % dipoles
  \pgfmathsetmacro\a{atan2(\y,\x)}
  \pgfmathtruncatemacro\b{\x*\x+\y*\y}
  \ifnum\b > 0
    \pgfmathsetmacro\bb{0.08*sqrt(\b)} % semi-axis b = distance to the origin (scaled)
    \begin{scope}[shift={(\x,\y)},rotate=\a,x=\q cm]
      \fill[positive] (0,\bb) arc (90:-90:0.4 and \bb);
      \fill[negative] (0,\bb) arc (90:270:0.4 and \bb);
      \draw (0,0) ellipse (0.4 and \bb);
      \node[transform shape,scale=0.6] at ( 0.2,0) {$+$};
      \node[transform shape,scale=0.6] at (-0.2,0) {$-$};
    \end{scope}
  \fi
}
% central charge
\draw[fill=red] (0,0) circle (0.15) node [red,below left] {$q$};
\ifnum\q = 1
  \node[scale=0.6] at (0,0) {$+$};
\else
  \node[scale=0.6] at (0,0) {$-$};
\fi
\end{tikzpicture}
\end{document}

带有负中心电荷的相同图片: 在此处输入图片描述

答案2

pic我通过创建一个名为的atom,然后将pics 沿着从原点到循环内的整数点的路径(参见选项[sloped],不需要或角度计算)放置,简化了@Juan Castano 的代码和计算。atan2

在此处输入图片描述

\documentclass[tikz,border=5mm]{standalone}
\begin{document}
\begin{tikzpicture}
\tikzset{pics/atom/.style args={major #1 minor #2}{code={%  
\fill[red] (0,#2) arc (90:-90:#1 and #2);
\fill[green!50] (0,#2) arc (90:270:#1 and #2);
\draw (0,0) ellipse(#1 and #2);
}}}

\def\atompath{(0,0)--(\x,\y) pic{atom=major .4 minor \bb} node[right,scale=.6]{$+$} node[left,scale=.6]{$-$}}
        
\foreach \x in {-4,...,4}
\foreach \y in {-3,...,3}
{%
\pgfmathtruncatemacro\b{\x*\x+\y*\y}
\ifnum \b>0    
\pgfmathsetmacro{\bb}{.08*sqrt(\b)}
\ifnum \x<0 
\path[nodes={sloped,pos=1,xscale=-1}] \atompath;
\else
\path[nodes={sloped,pos=1}] \atompath;
\fi 
\fi
}
    
% central charge
\draw[fill=red] (0,0) circle(.18) node[scale=0.6]{$+$} + (-135:.4) node[red]{$q$};
\end{tikzpicture}
\end{document}

相关内容