我正在尝试重现如下图所示的情况:
实际上我想稍微修改一下,因为我想让所有的弹簧都连接到顶部的公共墙上。我以以下代码开始:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{patterns,snakes}
\begin{document}
\begin{tikzpicture}{every node/.style={draw,outer sep=0pt,thick}}
\node (ground) [fill,gray, pattern=north east lines,minimum width=5cm,%
minimum height=.3cm,anchor=north,draw=none] {};
\node (M1) [draw,minimum width=0.1cm,minimum height=0.1cm] at (-2,-2) {m};
\draw [ snake=coil, segment amplitude=5pt, segment length=5pt] (ground.south) -- (M1);
\node (M2) [draw,minimum width=0.1cm,minimum height=0.1cm] at (-1.5,-2.5) {m};
\draw [ snake=coil, segment amplitude=5pt, segment length=5pt] (ground.south) -- (M2);
\end{tikzpicture}
\end{document}
我遇到的第一个问题是地面图案被填满,没有显示斜线图案。第二个问题是我不知道如何正确定位不同的质量。通过波浪,质量的距离和伸长遵循正弦形状,所以我认为应该有一种方法可以自动放置它们。
答案1
更新答案
首次尝试使用螺旋弹簧(需要改进以考虑弹簧的自然长度)。
\documentclass[tikz]{standalone}
\usetikzlibrary{decorations.pathmorphing,calc,patterns}
\makeatletter
\def\pgfdecorationspringstraightlinelength{.5cm}
\def\pgfdecorationspringnumberofelement{8}
\def\pgfdecorationspringnaturallength{5cm}
\pgfkeys{%
/pgf/decoration/.cd,
spring straight line length/.code={%
\pgfmathsetlengthmacro\pgfdecorationspringstraightlinelength{#1}},
spring natural length/.code={%
\pgfmathsetlengthmacro\pgfdecorationspringnaturallength{#1}},
spring number of element/.store in=\pgfdecorationspringnumberofelement
}
\pgfdeclaredecoration{coil spring}{straight line}{%
\state{straight line}[%
persistent precomputation = {%
% Compute the effective length of the spring (without the length
% of the two straight lines): \pgfdecorationspringeffectivelength
\pgfmathsetlengthmacro{\pgfdecorationspringeffectivelength}%
{\pgfdecoratedpathlength-2*\pgfdecorationspringstraightlinelength}
% Compute the effective length of one coil pattern:
% \pgfdecorationspringeffectivelengthofonecoil
\pgfmathsetlengthmacro{\pgfdecorationspringeffectivelengthofonecoil}%
{\pgfdecorationspringeffectivelength/\pgfdecorationspringnumberofelement}
},
width = \pgfdecorationspringstraightlinelength,
next state = draw spring]{%
\pgfpathlineto{%
\pgfqpoint{%
\pgfdecorationspringstraightlinelength}{0pt}}
}
\state{draw spring}%
[width=\pgfdecorationspringeffectivelengthofonecoil,
repeat state=\pgfdecorationspringnumberofelement-1,next state=final]{%
\pgfpathcurveto
{\pgfpoint@onspringcoil{0 }{ 0.555}{1}}
{\pgfpoint@onspringcoil{0.445}{ 1 }{2}}
{\pgfpoint@onspringcoil{1 }{ 1 }{3}}
\pgfpathcurveto
{\pgfpoint@onspringcoil{1.555}{ 1 }{4}}
{\pgfpoint@onspringcoil{2 }{ 0.555}{5}}
{\pgfpoint@onspringcoil{2 }{ 0 }{6}}
\pgfpathcurveto
{\pgfpoint@onspringcoil{2 }{-0.555}{7}}
{\pgfpoint@onspringcoil{1.555}{-1 }{8}}
{\pgfpoint@onspringcoil{1 }{-1 }{9}}
\pgfpathcurveto
{\pgfpoint@onspringcoil{0.445}{-1 }{10}}
{\pgfpoint@onspringcoil{0 }{-0.555}{11}}
{\pgfpoint@onspringcoil{0 }{ 0 }{12}}
}
\state{final}{%
\pgfpathlineto{\pgfpointdecoratedpathlast}
}
}
\def\pgfpoint@onspringcoil#1#2#3{%
\pgf@x=#1\pgfdecorationsegmentamplitude%
\pgf@x=.5\pgf@x%
\pgf@y=#2\pgfdecorationsegmentamplitude%
\pgfmathparse{0.083333333333*\pgfdecorationspringeffectivelengthofonecoil}%
\pgf@xa=\pgfmathresult pt
\advance\pgf@x by#3\pgf@xa%
}
\makeatother
\tikzset{%
Spring/.style = {%
decoration = {%
coil spring,
spring straight line length = .5cm,
% To be added
spring natural length = #1,
spring number of element = 8,
amplitude=3mm},
decorate,
very thick},
Spring/.default = {4cm}}
\begin{document}
\begin{tikzpicture}
\def\SpringNaturalLength{4cm}%
\def\xMax{10}%
\pgfmathdeclarefunction{wave}{1}{%
\pgfmathparse{\SpringNaturalLength*(1+.5*cos(\x*40))}}
\filldraw[gray,pattern=north east lines] (0,0) -| (\xMax,.2) -| (0,0);
\foreach \x in {0,0.75,...,\xMax}{%
\draw[Spring] (\x,0) -- node[at end,fill=red,circle] {} (\x,{-wave(\x)});}
\end{tikzpicture}
\end{document}
原始答案
类似的东西(抱歉,我没有使用线圈来表示弹簧)。
\documentclass[tikz]{standalone}
\usetikzlibrary{decorations.pathmorphing,calc,patterns}
\makeatletter
\def\pgfdecorationspringstraightlinelength{.5cm}
\def\pgfdecorationspringnumberofelement{8}
\def\pgfdecorationspringnaturallength{5cm}
\pgfkeys{%
/pgf/decoration/.cd,
spring straight line length/.code={%
\pgfmathsetlengthmacro\pgfdecorationspringstraightlinelength{#1}},
spring natural length/.code={%
\pgfmathsetlengthmacro\pgfdecorationspringnaturallength{#1}},
spring number of element/.store in=\pgfdecorationspringnumberofelement
}
\pgfdeclaredecoration{zig zag spring}{straight line}{%
\state{straight line}[%
persistent precomputation = {%
% Compute the effective length of the spring (without the length
% of the two straight lines): \pgfdecorationspringeffectivelength
\pgfmathsetlengthmacro{\pgfdecorationspringeffectivelength}%
{\pgfdecoratedpathlength-2*\pgfdecorationspringstraightlinelength}
% Compute the effective length of one zigzag pattern:
% \pgfdecorationspringeffectivelengthofonezigzag
\pgfmathsetlengthmacro{\pgfdecorationspringeffectivelengthofonezigzag}%
{\pgfdecorationspringeffectivelength/\pgfdecorationspringnumberofelement}
% Compute the effective length of one forth of zigzag pattern:
% \pgfdecorationspringeffectivelengthofoneforthofzigzag
\pgfmathsetlengthmacro{%
\pgfdecorationspringeffectivelengthofoneforthofzigzag}%
{\pgfdecorationspringeffectivelengthofonezigzag/4}
% Compute the length of a forth of one zigzag (fixed by spring
% constants)
% 4=1/4
% 0.707=cos(45°)
\pgfmathsetlengthmacro{\pgfdecorationspringlengthofaforthzigzag}%
{\pgfdecorationspringnaturallength
/(\pgfdecorationspringnumberofelement*4*0.707)}
% Compute the (semi) heigth
\pgfmathsetlengthmacro{\pgfdecorationspringeffectivesemiheigth}%
{sqrt(abs(\pgfdecorationspringlengthofaforthzigzag^2-
\pgfdecorationspringeffectivelengthofoneforthofzigzag^2))}%
},
width = \pgfdecorationspringstraightlinelength,
next state = draw spring]{%
\pgfpathlineto{%
\pgfqpoint{%
\pgfdecorationspringstraightlinelength}{0pt}}
}
\state{draw spring}%
[width=\pgfdecorationspringeffectivelengthofonezigzag,
repeat state=\pgfdecorationspringnumberofelement-1,next state=final]{%
\pgfpathlineto{%
\pgfqpoint{%
\pgfdecorationspringeffectivelengthofoneforthofzigzag}{%
\pgfdecorationspringeffectivesemiheigth}}
\pgfpathlineto{%
\pgfpoint{%
3*\pgfdecorationspringeffectivelengthofoneforthofzigzag}{%
-\pgfdecorationspringeffectivesemiheigth}}
\pgfpathlineto{%
\pgfpoint{%
4*\pgfdecorationspringeffectivelengthofoneforthofzigzag}{0pt}}
}
\state{final}{%
\pgfpathlineto{\pgfpointdecoratedpathlast}
}
}
\makeatother
\tikzset{%
Spring/.style = {%
decoration = {%
zig zag spring,
spring straight line length = .5cm,
spring natural length = #1,
spring number of element = 5},
decorate,
very thick},
Spring/.default = {4cm}}
\begin{document}
\begin{tikzpicture}
\def\SpringNaturalLength{4cm}%
\def\xMax{10}%
\pgfmathdeclarefunction{wave}{1}{%
\pgfmathparse{\SpringNaturalLength*(1+.5*cos(\x*40))}}
\filldraw[gray,pattern=north east lines] (0,0) -| (\xMax,.2) -| (0,0);
\foreach \x in {0,0.75,...,\xMax}{%
\draw[Spring] (\x,0) -- node[at end,fill=red,circle] {} (\x,{-wave(\x)});}
\end{tikzpicture}
\end{document}