使用 TikZ 绘制更漂亮的波浪线

使用 TikZ 绘制更漂亮的波浪线

我的问题受到以下启发:使用 tikz 绘制费曼图

答案中的光子线(TikZ 蛇形线)看起来与费曼图上的通常情况不同:在可以使用包获得的“参考”视图中,feynmp波在“波”的中间开始和结束(可以检查编译给出的答案的差异)。在代码中,metapost这是以一种愚蠢但有效的方式实现的:它取一条线并根据线长添加“波”。首先,它计算要放在线上的波的(整数)数量,以使一个波的长度尽可能接近某个给定的数字,然后计算一个波的长度并最终绘制它们。Metapost 代码相当简短和简单。

有没有办法feynmp在 TikZ 中完全重现这种行为?

答案1

这是一个新的装饰,,complete sines它可以完成您所描述的任务:它计算适合给定路径的具有指定波长的全波的数量,然后拉伸波长以便路径可以完全填充:

\documentclass[a4paper,12pt]{article}
\usepackage{tikz}
\usetikzlibrary{decorations}
\begin{document}

\pgfdeclaredecoration{complete sines}{initial}
{
    \state{initial}[
        width=+0pt,
        next state=sine,
        persistent precomputation={\pgfmathsetmacro\matchinglength{
            \pgfdecoratedinputsegmentlength / int(\pgfdecoratedinputsegmentlength/\pgfdecorationsegmentlength)}
            \setlength{\pgfdecorationsegmentlength}{\matchinglength pt}
        }] {}
    \state{sine}[width=\pgfdecorationsegmentlength]{
        \pgfpathsine{\pgfpoint{0.25\pgfdecorationsegmentlength}{0.5\pgfdecorationsegmentamplitude}}
        \pgfpathcosine{\pgfpoint{0.25\pgfdecorationsegmentlength}{-0.5\pgfdecorationsegmentamplitude}}
        \pgfpathsine{\pgfpoint{0.25\pgfdecorationsegmentlength}{-0.5\pgfdecorationsegmentamplitude}}
        \pgfpathcosine{\pgfpoint{0.25\pgfdecorationsegmentlength}{0.5\pgfdecorationsegmentamplitude}}
}
    \state{final}{}
}

\begin{tikzpicture}[
    every path/.style={
        decoration={
            complete sines,
            segment length=1cm,
            amplitude=1cm
        },
        decorate
    }]
\draw (0,0) -- (2,0);
\draw [yshift=-1.2cm] (0,0) -- (2.5,0);
\draw [yshift=-2.4cm] (0,0) -- (3,0);
\end{tikzpicture}
\end{document}

以下是三条路径的示例,长度分别为 2 厘米、2.5 厘米和 3 厘米,标称波长为 1 厘米。在第一种情况下,有两个波长为 1 厘米的全波;在第二种情况下,有两个波长为 1.25 厘米的全波;在第三种情况下,有三个波长为 1 厘米的全波。

如果您在链接的示例中使用它,如下所示:

gluon/.style={decorate, draw=black,
        decoration={complete sines,amplitude=8pt, segment length=11pt}}
     }

这将产生这样的结果

相关内容