在 tikz 中将两个线圈路径添加在一起

在 tikz 中将两个线圈路径添加在一起

我有两个线圈,我用的代码制作这里

我想将线圈相加,例如 cos(x) + cos(2x),得到一个新的线圈。但我不确定我制作它们的方式是否可行。可以做到吗?

如果做不到,请帮忙用数学函数画螺旋线——这样添加螺旋线就很简单了。我先试了一下,但没弄明白。

代码如下:

\documentclass[11pt]{article}

\usepackage{tikz}
\usetikzlibrary{decorations.markings}
\usetikzlibrary{calc,intersections,through}
\usetikzlibrary{bending}
\usetikzlibrary{shapes.geometric}
\tikzset{point/.style={circle,inner sep=0pt,minimum size=3pt,fill=black}}


\usetikzlibrary{arrows}


\usetikzlibrary{decorations.pathmorphing}

\makeatletter

% gluon decoration (based on the original coil decoration)

\pgfdeclaredecoration{gluon}{coil}
{
  \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}}
    \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}}
    \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}}
    \pgfpathcurveto
    {\pgfpoint@oncoil{1.555}{ 1    }{4}}
    {\pgfpoint@oncoil{2    }{ 0.555}{5}}
    {\pgfpoint@oncoil{2    }{ 0    }{6}}
  }
  \state{final}{}
}

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

\makeatother
\begin{document}


\begin{center}
 \begin{tikzpicture}
    \draw[very thick,rotate=90,decoration={
         aspect=0.41, 
         segment length=.5cm,
         amplitude=.5cm,
         gluon},
     decorate,] (0,10) --(0,0);
 \end{tikzpicture}

\medskip

 \begin{tikzpicture}
    \draw[very thick,rotate=90,decoration={
         aspect=0.41, 
         segment length=.25cm,
         amplitude=.25cm,
         gluon},
     decorate,] (0,10) --(0,0);
 \end{tikzpicture}
 \end{center}
\end{document}

答案1

\documentclass[tikz, border=1cm]{standalone}
\begin{document}
\begin{tikzpicture}

\draw[red, domain=0:1, smooth, variable=\t] (0,0) \foreach \n in {0,...,5} {-- plot ( {0.5*(\t+\n)-0.2*cos(\t*2*pi r)+0.2} , {0.5*sin(\t*2*pi r)})};

\draw[green, domain=0:1, smooth, variable=\t, samples=100]  (0,0) \foreach \n in {0,...,5} {-- plot ( {0.5*(\t+\n)-0.01*cos(\t*20*pi r)+0.01} , {0.04*sin(\t*20*pi r)})};

\draw[blue, domain=0:1, smooth, variable=\t, samples=100] (0,0) \foreach \n in {0,...,5} {-- plot ( {0.5*(\t+\n)-0.2*cos(\t*2*pi r)+0.2-0.01*cos(\t*20*pi r)+0.01} , {0.5*sin(\t*2*pi r)+0.04*sin(\t*20*pi r)})};

\end{tikzpicture}
\end{document}

两个线圈及其“和”

答案2

喜欢这张照片吗?

在此处输入图片描述

图片已由代码生成:

\begin{document}

\begin{center}
 \begin{tikzpicture}[scale=.5]
    \draw[very thick,rotate=90,decoration={
         aspect=0.41, 
         segment length=.5cm,
         amplitude=.5cm,
         gluon},
     decorate,] (0,10) --(0,0);
     \filldraw[rotate=90] (0,10) circle(5pt);
     \draw[very thick,rotate=90,decoration={
        aspect=0.41, 
        segment length=.25cm,
        amplitude=.25cm,
        gluon},
     decorate,] (0,20)--(0,10);
     \medskip
 \end{tikzpicture}
 
 \end{center}
\end{document}

序言来自你的代码哪里。

答案3

通过使用 ,您无需定制自己的线圈shorten >= <some value>

输出

\documentclass[margin=3.14mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing}

\begin{document}
\begin{tikzpicture}[
    pics/mycoil/.style={
        code={
        % larger coil
         \draw[
            very thick, 
            decoration={
                coil, aspect=0.41, segment length=0.5cm, amplitude=0.5cm
            },
            decorate, shorten >=9pt
        ] (0, 0) -- (1.5, 0);
        % smaller coil
        \draw[
            very thick, 
            decoration={
                coil, aspect=0.41, segment length=0.25cm, amplitude=0.25cm
            },
            decorate,  shorten >=4.25pt, xshift=-9.5pt
        ] (1.5,0) -- (2.5,0);
    },
}
]
\pic foreach \x in {1,...,5} at (2*\x,0) {mycoil};

% extra for explanation purposes
\draw[very thick,
    decoration={coil, aspect=0.41, segment length=0.25cm, amplitude=0.25cm}, decorate
] (2, -1) --
node[pos=1, right]{\small Remove horizontal line at the end by using \texttt{shorten >= value}}
(3, -1);
\draw node[draw=red, thick, circle, minimum size=4pt, xshift=-3pt] at (3, -1) {};
\end{tikzpicture}
\end{document}

相关内容