2013年11月22日金曜日

Tikzで鉄心に巻いたコイルの作図

TeX Stack ExchangeのModify Tikz coil decorationに,芯に巻いたコイルの作図の仕方が載っています.coil decorationを使って実現してあるのですが,以前に書いたようにcoil decorationではパラメータの選び方によって端に直線的な部分が残ってしまい使い勝手がよくありません.

以前の記事でcoil decorationを修正したgluon decorationを紹介しましたが,今回はこれを流用して端に直線部分が残らないものを作ってみます.

まず,上記のModify Tikz coil decorationの記事にあるように以下の内容をpgflibrarydecorations.mypathmorphing.code.texという名前で保存します.

\def\pgfpoint@oncoil#1#2#3{%
  \pgf@x=#1\pgfdecorationsegmentamplitude%
  \pgf@x=\pgfdecorationsegmentaspect\pgf@x%
  \pgf@y=#2\pgfdecorationsegmentamplitude%
  \pgf@xa=0.083333333333\pgfdecorationsegmentlength%
  \advance\pgf@x by#3\pgf@xa%
}

% gluonup decoration
%
% Parameters: \pgfdecorationsegmentamplitude, \pgfdecorationsegmentlength,

\pgfdeclaredecoration{gluonup}{initial}
{
  \state{initial}[
    width=+0pt,
    next state=coil,
    persistent precomputation={
      \pgfmathsetmacro\matchinglength{
        (ceil(\pgfdecoratedinputsegmentlength / \pgfdecorationsegmentlength) - \pgfdecoratedinputsegmentlength / \pgfdecorationsegmentlength) > 0.5
        ? (\pgfdecoratedinputsegmentlength - 2 * \pgfdecorationsegmentaspect * \pgfdecorationsegmentamplitude) / (floor(\pgfdecoratedinputsegmentlength / \pgfdecorationsegmentlength) + 0.499)
        : (\pgfdecoratedinputsegmentlength - 2 * \pgfdecorationsegmentaspect * \pgfdecorationsegmentamplitude) / (ceil(\pgfdecoratedinputsegmentlength / \pgfdecorationsegmentlength) + 0.499)
      }
      \setlength{\pgfdecorationsegmentlength}{\matchinglength pt}
    },
  ]{}
  \state{coil}[switch if less than=%
    0.5\pgfdecorationsegmentlength+%
    \pgfdecorationsegmentaspect\pgfdecorationsegmentamplitude+%
    \pgfdecorationsegmentaspect\pgfdecorationsegmentamplitude to last,
               width=+\pgfdecorationsegmentlength]
  {
    \pgfpathcurveto
    {\pgfpoint@oncoil{0    }{ 0.555}{1}}
    {\pgfpoint@oncoil{0.445}{ 1    }{2}}
    {\pgfpoint@oncoil{1    }{ 1    }{3}}
    \pgfpathmoveto{\pgfpoint@oncoil{1    }{-1    }{9}}
    \pgfpathcurveto
    {\pgfpoint@oncoil{0.445}{-1    }{10}}
    {\pgfpoint@oncoil{0    }{-0.555}{11}}
    {\pgfpoint@oncoil{0    }{ 0    }{12}}
  }
  \state{last}[next state=final]
  {
    \pgfpathcurveto
    {\pgfpoint@oncoil{0    }{ 0.555}{1}}
    {\pgfpoint@oncoil{0.445}{ 1    }{2}}
    {\pgfpoint@oncoil{1    }{ 1    }{3}}
    \pgfpathmoveto{\pgfpoint@oncoil{2    }{ 0    }{6}}
  }
  \state{final}{}
}



% gluondown decoration
%
% Parameters: \pgfdecorationsegmentamplitude, \pgfdecorationsegmentlength,

\pgfdeclaredecoration{gluondown}{initial}
{
  \state{initial}[
    width=+0pt,
    next state=coil,
    persistent precomputation={
      \pgfmathsetmacro\matchinglength{
        (ceil(\pgfdecoratedinputsegmentlength / \pgfdecorationsegmentlength) - \pgfdecoratedinputsegmentlength / \pgfdecorationsegmentlength) > 0.5
        ? (\pgfdecoratedinputsegmentlength - 2 * \pgfdecorationsegmentaspect * \pgfdecorationsegmentamplitude) / (floor(\pgfdecoratedinputsegmentlength / \pgfdecorationsegmentlength) + 0.499)
        : (\pgfdecoratedinputsegmentlength - 2 * \pgfdecorationsegmentaspect * \pgfdecorationsegmentamplitude) / (ceil(\pgfdecoratedinputsegmentlength / \pgfdecorationsegmentlength) + 0.499)
      }
      \setlength{\pgfdecorationsegmentlength}{\matchinglength pt}
    },
  ]{}
  \state{coil}[switch if less than=%
    0.5\pgfdecorationsegmentlength+%
    \pgfdecorationsegmentaspect\pgfdecorationsegmentamplitude+%
    \pgfdecorationsegmentaspect\pgfdecorationsegmentamplitude to last,
               width=+\pgfdecorationsegmentlength]
  {
    \pgfpathmoveto{\pgfpoint@oncoil{1    }{1    }{3}}
    \pgfpathcurveto
    {\pgfpoint@oncoil{1.555}{ 1    }{4}}
    {\pgfpoint@oncoil{2    }{ 0.555}{5}}
    {\pgfpoint@oncoil{2    }{ 0    }{6}}
    \pgfpathcurveto
    {\pgfpoint@oncoil{2    }{-0.555}{7}}
    {\pgfpoint@oncoil{1.555}{-1    }{8}}
    {\pgfpoint@oncoil{1    }{-1    }{9}}
  }
  \state{last}[next state=final]
  {
    \pgfpathmoveto{\pgfpoint@oncoil{1    }{ 1    }{3}}
    \pgfpathcurveto
    {\pgfpoint@oncoil{1.555}{ 1    }{4}}
    {\pgfpoint@oncoil{2    }{ 0.555}{5}}
    {\pgfpoint@oncoil{2    }{ 0    }{6}}
  }
  \state{final}{}
}

\endinput

これは以前記事にしたgluon decorationをgluonup, gluondowの2つに分割して描画するというものです.
書いてある内容はあまり理解していませんが,いじくっていたらそれっぽい出力が得られたのでよしとします.

実際に次のようなファイルを作成してみます.

\documentclass[a4paper]{article}
\usepackage[dvipdfmx]{graphicx} 
\PassOptionsToPackage{svgnames}{xcolor}
\usepackage{tikz}
\usetikzlibrary{decorations}
\usepgflibrary{decorations.mypathmorphing}

\begin{document}
\begin{tikzpicture}
\draw [decorate, decoration={gluon, aspect=0.4, amplitude=4mm,  segment length=4mm},gray!50,very thick] (0,0) -- (5,0);
  \draw [decorate, decoration={gluonup, aspect=0.4, amplitude=4mm,  segment length=4mm},blue] (0,0) -- (5,0);
\node[anchor=west] at (5.1,0){gluonup};
\end{tikzpicture}

\bigskip
\begin{tikzpicture}
\draw [decorate, decoration={gluon, aspect=0.4, amplitude=4mm,  segment length=4mm},gray!50,very thick] (0,0) -- (5,0);
  \draw [decorate, decoration={gluondown, aspect=0.4, amplitude=4mm,  segment length=4mm},red] (0,0) -- (5,0);
\node[anchor=west] at (5.1,0){gluondown};
\end{tikzpicture}

\bigskip
 \begin{tikzpicture}
 \pgfmathsetmacro{\w}{3.5}
 \pgfmathsetmacro{\h}{.8}
 \pgfmathsetmacro{\r}{0.5*\h}
 \pgfmathsetmacro{\rx}{0.35*\r}
 \pgfmathsetmacro{\ry}{\r-0.0005}
 \pgfmathsetmacro{\amp}{5mm}
 \pgfmathsetmacro{\sgl}{3.2mm}

 %コイル1 (鉄心に隠れる方)
  \draw [decorate, decoration={gluonup,   aspect=0.4, amplitude=\amp,  segment length=\sgl}] (0,0) -- (\w,0);

 %鉄心1
  \filldraw[fill=white] (0,\r) -- (\w,\r) 
  arc [start angle=90, end angle = -90, x radius=\rx, y radius=\ry]
  -- (0, -\r) 
  arc [start angle=-90, end angle = 90, x radius=\rx, y radius=\ry];

 %導線
  \draw[very thick,draw=white] (0,0) |- (\w,-1.8*\h) -- (\w,0);
  \draw (0,0) |- (\w,-1.8*\h) -- (\w,0);

 %鉄心2
  \draw[fill=white](0,0) circle [x radius=\rx, y radius=\ry];

 %コイル2 (鉄心の前面)
  \draw [very thick,draw=white,decorate, decoration={gluondown, aspect=0.4, amplitude=\amp, segment length=\sgl}] (0,0) -- (\w,0);

  \draw [decorate, decoration={gluondown,   aspect=0.4, amplitude=\amp,  segment length=\sgl}] (0,0) -- (\w,0);
\end{tikzpicture}

\end{document}


これをタイプセットすると次の図が得られます.

直線的なパスに沿ってのコイルはこれでうまくいくのですが,円に沿ったコイルを描こうとするとうまくいきません.
これは元のcoil decorationでも同様で,コイル一巻きを反復して描画していることが原因のようです.