tikz 延伸弹簧,具有恒定的段数

tikz 延伸弹簧,具有恒定的段数

有没有简单的方法可以用 tikz 创建 pdf 动画,其中振荡质量连接到弹簧,弹簧在质量振荡时伸展和压缩。振荡期间“弹簧元件”的数量应保持不变。

到目前为止,我想出了这个漂亮的解决方案。但是,我不太明白如何为段长度参数选择正确的单位:

\documentclass[tikz]{standalone}
\usepackage{tikz}
\usetikzlibrary{shadings, calc, decorations.pathmorphing}

\begin{document}

\def\frames{50}
\def\amplitude{1.5}
\def\z0{3.0}
\def\nloops{5.0}

\foreach \n in {0, 1, ..., \frames}
{
    \begin{tikzpicture}[x=1cm, y=1cm]

    % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % calculations
    % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    \pgfmathsetmacro{\height}{\amplitude * cos(2.0 * pi * \n / \frames r) - \z0};
    \pgfmathsetmacro{\seglength}{(-\amplitude * cos(2.0 * pi * \n / \frames r) + 3.0) / \nloops * 1cm};


    % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % help lines
    % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    \draw[color=white,fill=white] (-1.70, 0.4) rectangle (2.00, -6.0);


    % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % the scene
    % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    % the ceiling   
    \draw[color=black!70, line width=2.0pt, line cap=round] (-1.70, 0) -- (1.70, 0);
    \foreach \x in {-1.60, -1.20, ..., 2.0}
    {
        \draw[color=black!70, line width=1.00pt, line cap=round] (\x, 0) -- ++ (0.35, 0.4);
    }
    \draw[color=black!70, line width=2.00pt, line cap=round] (-0.5, 0) -- +(0.0, -0.2);
    \draw[color=black!70, line width=2.00pt, line cap=round] (+0.5, 0) -- +(0.0, -0.2);


    % the mass
    \draw[ball color=orange!90!black!70, rounded corners=1ex] (-1.0, \height) rectangle +(2.0, -0.75);
    \draw[color=black!70, line width=2.00pt, line cap=round] (-0.5, \height) -- +(0.0, 0.2);
    \draw[color=black!70, line width=2.00pt, line cap=round] (+0.5, \height) -- +(0.0, 0.2);

    % the spring
    \draw[decorate, color=red!70, line width=2.00pt, line cap=round, decoration={coil, amplitude=0.25cm, segment length=\seglength}] ($(-0.5, \height) + (0, 0.2)$) -- (-0.5, -0.2);

    \end{tikzpicture}
}
\end{document}

弹簧的最小延伸量应为 1.5 厘米,最大延伸量应为 4.5 厘米。以下是弹簧最小延伸量的图像:

在此处输入图片描述

有人能给我指出正确的方向吗?

答案1

感谢相关问题的链接。通过研究这些非常好的解决方案,我可以修改我的图。结果不像其他解决方案那么复杂,但要短得多。因此,我想与你分享。

\documentclass[tikz]{standalone}
\usepackage{tikz}
\usetikzlibrary{shadings, calc, decorations.pathmorphing}

\begin{document}

\def\frames{50}
\def\r{0.5cm}

\foreach \n in {0, 1, ..., \frames}
{
    \begin{tikzpicture}

    % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % calculations
    % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    \pgfmathsetlengthmacro{\height}{1.5cm * cos(2.0 * pi * \n / \frames r) - 2.9cm};
    \pgfmathsetlengthmacro{\seglength}{(-1 * \height - 0.4cm) / 4};

    % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % the frame
    % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    \draw[color=white,fill=white] (-1.70, 0.4) rectangle (2.00, -6.0);

    % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % the scene
    % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % the ceiling   
    \draw[color=black!70, line width=2.0pt, line cap=round] (-1.70, 0) -- (1.70, 0);
    \foreach \x in {-1.60, -1.20, ..., 2.0}
    {
        \draw[color=black!70, line width=1.00pt, line cap=round] (\x, 0) -- ++ (0.35, 0.4);
    }
    \draw[color=black!70, line width=2.00pt, line cap=round] (0.0, 0.0) -- +(0.0, -0.2);

    % the mass
    \draw[ball color=orange!90!black!70, rounded corners=1ex] ($(0.0, \height) - (0.0, \r)$) circle[radius=\r];
    \draw[color=black!70, line width=2.00pt, line cap=round] (0.0, \height) -- +(0.0, 0.2);

    % the spring
    \draw[decorate, color=black!70, line width=2.00pt, line cap=round, decoration={zigzag, amplitude=0.25cm, segment length=\seglength}] ($(0.0, \height) + (0, 0.2)$) -- (0.0, -0.2);

    \end{tikzpicture}
}
\end{document}

动画如下所示 在此处输入图片描述

相关内容