# Drawing mechanical systems using TikZ

This is a follow-up question to Drawing Mechanical Systems in LaTeX. I have tried to do similar things, i.e. to model simple mechanical systems.

For example:

\begin{tikzpicture}[M1/.style={rectangle,draw=black,minimum size=2cm,thick}]

\tikzstyle{spring}=[thick,decorate,decoration={zigzag,pre length=0.3cm,post length=0.3cm,segment length=5}]
\tikzstyle{ground}=[fill,pattern=north east lines,draw=none,minimum width=0.75cm,minimum height=0.3cm]
\node [M1] (M1) {};
\node (wall1) [ground, minimum width=3cm,yshift=-3cm] {};
\draw (wall1.north west) -- (wall1.north east);
\draw [spring] (wall1.170) -- ($(M1.south east)!(wall1.170)!(M1.south west)$) node[pos=.5,left] {$k_2$};
\draw [spring] (wall1.10) -- ($(M1.south west)!(wall1.10)!(M1.south east)$) node[pos=.5,right] {$k_3$};
\node (wall2) [ground, minimum width=3cm,yshift=3cm] {};
\draw (wall2.south west) -- (wall2.south east);
\draw [spring] (wall2.170) -- ($(M1.north east)!(wall2.170)!(M1.north west)$) node[pos=.5,left] {$k_1$};
\draw [-latex,thick] (1.5,0) -- (1.5,1) node [pos=.5,right] {$x$};
\filldraw circle (.05) node [pos=.5,below] {\tiny $CG$};
\node [rotate=90,above] at (M1.west) {Mass,$m$};
\node at (0,-3.5) {(a)};
\begin{scope}[xshift=5cm]
\node [M1] (M1) {};
\node (wall1) [ground, minimum width=3cm,yshift=-3cm] {};
\draw (wall1.north west) -- (wall1.north east);
\draw [spring] (wall1) -- ($(M1.south east)!(wall1)!(M1.south west)$) node[pos=.5,left] {$k_2^*$};
\node (wall2) [ground, minimum width=3cm,yshift=3cm] {};
\draw (wall2.south west) -- (wall2.south east);
\draw [spring] (wall2) -- ($(M1.north east)!(wall2)!(M1.north west)$) node[pos=.5,left] {$k_1$};
\draw [-latex,thick] (1.5,0) -- (1.5,1) node [pos=.5,right] {$x$};
\filldraw circle (.05) node [pos=.5,below] {\tiny $CG$};
\node [rotate=90,above] at (M1.west) {Mass,$m$};
\node at (0,-3.5) {(b)};
\end{scope}
\end{tikzpicture}


Now I have the following problem:

The spring k1 in part a is connected to the wall, but to the wrong side. I have tried several things, but nothing worked.

Figure here: • @roland: include the image as normal (image icon or CTRL+G) and then remove the ! in front of it. This makes it a link to the image, which is allowed for low-rep users. – Martin Scharrer Jul 5 '11 at 12:35

You should use (wall2.190) -- ($(M1.north east)!(wall2.190)!(M1.north west)$) (notice the use of 190 instead of 170). The first expression specifies the starting point of the spring, which in this case is in the bottom left of the wall. The <node>.<number> anchors specify points on the edge of a node, with the number specifying the angle at which the anchor is placed. 0 is the middle of the right edge, 90 is the middle of the top, 170 is slightly higher than the middle of the left edge, and 190 is slightly lower than the middle of the left edge. The second expression specifies the end point of the spring, which is at the projection of the starting point on the line from the top left of the rectangle to the top right (($(A)!(P)!(B)$) is the projection of P on AB). \documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc,patterns,decorations.pathmorphing,decorations.markings}

\begin{document}

\begin{tikzpicture}[M1/.style={rectangle,draw=black,minimum size=2cm,thick}]
\tikzstyle{spring}=[thick,decorate,decoration={zigzag,pre length=0.3cm,post length=0.3cm,segment length=5}]
\tikzstyle{ground}=[fill,pattern=north east lines,draw=none,minimum width=0.75cm,minimum height=0.3cm]
\node [M1] (M1) {};
\node (wall1) [ground, minimum width=3cm,yshift=-3cm] {};
\draw (wall1.north west) -- (wall1.north east);
\draw [spring] (wall1.170) -- ($(M1.south east)!(wall1.170)!(M1.south west)$) node[pos=.5,left] {$k_2$};
\draw [spring] (wall1.10) -- ($(M1.south west)!(wall1.10)!(M1.south east)$) node[pos=.5,right] {$k_3$};
\node (wall2) [ground, minimum width=3cm,yshift=3cm] {};
\draw (wall2.south west) -- (wall2.south east);
\draw [spring] (wall2.190) -- ($(M1.north east)!(wall2.190)!(M1.north west)$) node[pos=.5,left] {$k_1$};
\draw [-latex,thick] (1.5,0) -- (1.5,1) node [pos=.5,right] {$x$};
\filldraw circle (.05) node [pos=.5,below] {\tiny $CG$};
\node [rotate=90,above] at (M1.west) {Mass,$m$};
\node at (0,-3.5) {(a)};
\begin{scope}[xshift=5cm]
\node [M1] (M1) {};
\node (wall1) [ground, minimum width=3cm,yshift=-3cm] {};
\draw (wall1.north west) -- (wall1.north east);
\draw [spring] (wall1) -- ($(M1.south east)!(wall1)!(M1.south west)$) node[pos=.5,left] {$k_2^*$};
\node (wall2) [ground, minimum width=3cm,yshift=3cm] {};
\draw (wall2.south west) -- (wall2.south east);
\draw [spring] (wall2) -- ($(M1.north east)!(wall2)!(M1.north west)$) node[pos=.5,left] {$k_1$};
\draw [-latex,thick] (1.5,0) -- (1.5,1) node [pos=.5,right] {$x$};
\filldraw circle (.05) node [pos=.5,below] {\tiny $CG$};
\node [rotate=90,above] at (M1.west) {Mass,$m$};
\node at (0,-3.5) {(b)};
\end{scope}
\end{tikzpicture}

\end{document}

• thanks a lot. However, there is a reason why k1 is placed on the top left of the rectangle (figure part a). It is the original position of k1 (out of plane). By simplification, you can then assume to put k1 in plane (figure part b)... – roland Jul 5 '11 at 13:51
• @roland: Sorry for that, I just assumed the figure should be symmetrical. I've corrected my answer now. – Jake Jul 5 '11 at 22:27
• @jake:thanks a lot for your help and explanation! – roland Jul 6 '11 at 16:37
• @roland: You're welcome! If this answers your question, it would be good if you could click the tick mark next to my answer to mark this question as answered. – Jake Jul 6 '11 at 23:00