2

Can I get anybody to help me with drawing this spring mass system in LaTeX? I've tried using another code for a spring mass system in here, but unfortunately I can't get it modified as I want. Hopefully there is one of you guys, who is a bit more experienced than me. enter image description here

  • 2
    I think this has been asked many times before: for example https://tex.stackexchange.com/q/451624/15036 -- does that help you enough? If not search for spring mass and see if any of the others are more useful – Thruston Mar 30 '20 at 19:56
  • the live streaming of it can be found here. – Display Name Mar 30 '20 at 21:03

2 Answers2

8

As has been remarked in the comments there are zillions posts drawing such things. Many of them have some distinctive features. The only point that might be different in this code from many others is that it uses patterns.meta.

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{patterns.meta,decorations.pathmorphing}
\begin{document}
\begin{tikzpicture}[>=stealth]
 \path[pattern={Lines[angle=45,distance={8pt/sqrt(2)}]}] (-2,5) edge ++(4,0)
  rectangle ++ (4,0.5);
 \draw[decorate,decoration={coil,segment length=5pt,aspect=0.7,amplitude=4pt,
    pre=lineto,pre length=5mm,post=lineto,post length=5mm},thick] (0,5) -- (0,1)
    node[below,draw,minimum size=1cm,fill=blue!20](m){$m$};
 \draw[blue] (m.center-|0,0) ++ (0,1) -- ++ (-2,0) 
    edge[<-,edge label'=$x$,shorten >=1pt] (m.center-|-2,0)
  (m.center-|0,0) ++ (0,-1) -- ++ (-2,0) 
    edge[<-,edge label=$-x$,shorten >=1pt] (m.center-|-2,0)
    (m.east) edge[dashed] (m.east-|2,0) 
     (m.east-|2,0) node[right] {Equilibrium};
\end{tikzpicture}
\end{document}

enter image description here

I also want to add that if, as you say, you struggle to modify an existing code to fit your needs, it would IMHO make much more sense to show what you have tried. Then you could learn how you can modify posts to get what you need and thus able to create whatever you want without relying on the availability of others. This is particularly easy with TikZ because usually you only need to modify some keys/styles (and coordinates, of course).

  • Thanks a lot! But when I run your code, it just says "Undefined pattern 'Lines'". Do you know why? – Benedikte Toft Mar 30 '20 at 21:40
  • 2
    @BenedikteToft The most likely explanation is that you have an older TeX installation. The patterns.meta stuff was only added (officially) to tikz rather recently. So you may want to update your LaTeX installation. (Ah, and if you are using overleaf, then there is nothing I can do, they decide to use an outdated TeX installation, which they call "stable" as if the recent installations were "unstable".) –  Mar 30 '20 at 21:44
  • I'm not able to make the roof where the spring is tied :-) – Sebastiano Mar 30 '20 at 22:36
  • 1
    @Sebastiano It could be worse. ;-) –  Mar 30 '20 at 23:28
  • Great!!!!!! ahahah. – Sebastiano Mar 31 '20 at 10:11
6

If you like the drawing...you can create a fast code with TikZ using Matcha: https://www.mathcha.io/editor.

enter image description here

%% Compile and read me!
\documentclass[a4paper,12pt]{article}
\usepackage{tikz}

\begin{document}


% Gradient Info

\tikzset {_d1boahmbc/.code = {\pgfsetadditionalshadetransform{ \pgftransformshift{\pgfpoint{0 bp } { 0 bp }  }  \pgftransformrotate{0 }  \pgftransformscale{2 }  }}}
\pgfdeclarehorizontalshading{_0q4qqrb4y}{150bp}{rgb(0bp)=(0.65,0.81,0.87);
rgb(37.5bp)=(0.65,0.81,0.87);
rgb(62.5bp)=(0.14,0.33,0.54);
rgb(100bp)=(0.14,0.33,0.54)}
\tikzset{every picture/.style={line width=0.75pt}} %set default line width to 0.75pt        

\begin{tikzpicture}[x=0.75pt,y=0.75pt,yscale=-1,xscale=1]
%uncomment if require: \path (0,667); %set diagram left start at 0, and has height of 667

%Shape: Spring [id:dp9040227812016859] 
\draw  [line width=1.5]  (237.28,61.08) .. controls (247.3,63.5) and (257.35,69.92) .. (257.45,82.92) .. controls (257.65,108.92) and (217.65,109.24) .. (217.61,103.24) .. controls (217.56,97.24) and (257.56,96.92) .. (257.76,122.92) .. controls (257.97,148.92) and (217.97,149.23) .. (217.92,143.23) .. controls (217.87,137.24) and (257.87,136.92) .. (258.08,162.92) .. controls (258.28,188.92) and (218.28,189.23) .. (218.24,183.23) .. controls (218.19,177.23) and (258.19,176.92) .. (258.39,202.92) .. controls (258.6,228.92) and (218.6,229.23) .. (218.55,223.23) .. controls (218.5,217.23) and (258.5,216.92) .. (258.71,242.92) .. controls (258.91,268.92) and (218.91,269.23) .. (218.86,263.23) .. controls (218.82,257.23) and (258.82,256.92) .. (259.02,282.92) .. controls (259.22,308.92) and (219.23,309.23) .. (219.18,303.23) .. controls (219.14,298.22) and (247.03,297.17) .. (256.35,312.11) ;
%Shape: Square [id:dp6396081153175814] 
\path  [shading=_0q4qqrb4y,_d1boahmbc] (227.5,313.5) -- (273,313.5) -- (273,359) -- (227.5,359) -- cycle ; % for fading 
 \draw  [color={rgb, 255:red, 0; green, 0; blue, 0 }  ,draw opacity=1 ] (227.5,313.5) -- (273,313.5) -- (273,359) -- (227.5,359) -- cycle ; % for border 

%Straight Lines [id:da3752568722922205] 
\draw    (167,61) -- (316.5,61) (177,57) -- (177,65)(187,57) -- (187,65)(197,57) -- (197,65)(207,57) -- (207,65)(217,57) -- (217,65)(227,57) -- (227,65)(237,57) -- (237,65)(247,57) -- (247,65)(257,57) -- (257,65)(267,57) -- (267,65)(277,57) -- (277,65)(287,57) -- (287,65)(297,57) -- (297,65)(307,57) -- (307,65) ;
%Straight Lines [id:da5553906400035267] 
\draw [color={rgb, 255:red, 74; green, 144; blue, 226 }  ,draw opacity=1 ] [dash pattern={on 0.84pt off 2.51pt}]  (397.5,334) -- (286,334) ;
%Straight Lines [id:da2550415269696811] 
\draw [color={rgb, 255:red, 74; green, 144; blue, 226 }  ,draw opacity=1 ]   (76.5,401) -- (200,401) ;
%Straight Lines [id:da48978661486538844] 
\draw [color={rgb, 255:red, 74; green, 144; blue, 226 }  ,draw opacity=1 ]   (76.5,262) -- (200,262) ;
%Straight Lines [id:da6237187057457076] 
\draw [color={rgb, 255:red, 74; green, 144; blue, 226 }  ,draw opacity=1 ]   (93.5,331) -- (93.02,267) ;
\draw [shift={(93,264)}, rotate = 449.57] [fill={rgb, 255:red, 74; green, 144; blue, 226 }  ,fill opacity=1 ][line width=0.08]  [draw opacity=0] (8.93,-4.29) -- (0,0) -- (8.93,4.29) -- cycle    ;
%Straight Lines [id:da5578569616892095] 
\draw [color={rgb, 255:red, 74; green, 144; blue, 226 }  ,draw opacity=1 ]   (93.5,343) -- (93.5,397) ;
\draw [shift={(93.5,400)}, rotate = 270] [fill={rgb, 255:red, 74; green, 144; blue, 226 }  ,fill opacity=1 ][line width=0.08]  [draw opacity=0] (8.93,-4.29) -- (0,0) -- (8.93,4.29) -- cycle    ;
%Straight Lines [id:da04727108644812783] 
\draw [color={rgb, 255:red, 74; green, 144; blue, 226 }  ,draw opacity=1 ] [dash pattern={on 0.84pt off 2.51pt}]  (212.5,335) -- (101,335) ;

% Text Node
\draw (243,328) node [anchor=north west][inner sep=0.75pt]    {\large $\textcolor[rgb]{1,1,1}{m}$};
% Text Node
\draw (406,323) node [anchor=north west][inner sep=0.75pt]  [color={rgb, 255:red, 74; green, 144; blue, 226 }  ,opacity=1 ] [align=left] {Equilibrium};
% Text Node
\draw (71,290.4) node [anchor=north west][inner sep=0.75pt]  [color={rgb, 255:red, 74; green, 144; blue, 226 }  ,opacity=1 ]  {$x$};
% Text Node
\draw (63,356.4) node [anchor=north west][inner sep=0.75pt]  [color={rgb, 255:red, 74; green, 144; blue, 226 }  ,opacity=1 ]  {$-x$};


\end{tikzpicture}

\end{document}
Sebastiano
  • 54,118