我尝试使用以下代码对图片中的几个弹簧和减震器进行标准化:
\documentclass[11pt]{article}
\usepackage{graphics,epsfig,graphicx}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsfonts}
\usepackage{amsthm}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing,patterns}
\usetikzlibrary{calc,patterns,decorations.markings}
\usetikzlibrary{positioning}
\begin{document}
\tikzset{
spring/.style={thick,decorate,decoration={zigzag,pre length=0.3cm,post length=0.3cm,segment length=6}},
blank/.style={draw=none,fill=none,pos=0.5},
ground/.style={fill,pattern=north east lines,draw=none,minimum width=0.5cm,minimum height=0.3cm},
damper/.style={thick,
decoration={markings, mark connection node=dmp,
mark=at position 0.5 with
{
\node (dmp) [thick,inner sep=0pt,transform shape,rotate=-90,minimum width=10pt,minimum height=3pt,draw=none] {};
\draw [thick] ($(dmp.north east)+(2pt,0)$) -- (dmp.south east) -- (dmp.south west) -- ($(dmp.north west)+(2pt,0)$);
\draw [thick] ($(dmp.north)+(0,-3pt)$) -- ($(dmp.north)+(0,3pt)$);
}
}, decorate
}
}
\begin{tikzpicture}[every node/.style={draw,outer sep=0pt,thick}]
\begin{scope}[xshift=7cm]
\node (M) [minimum width=1cm, minimum height=1cm] {$m$} node[blank,below=0.5] {$d_1$};
%\draw [<-,thick](-2.6,.6) -- (-1.5,-.2);
\node (wall) [ground, rotate=-90, minimum width=2cm,yshift=-3cm] {};
\draw (wall.north east) -- (wall.north west);
\draw [spring] (wall.120) -- ($(M.north west)!(wall.120)!(M.south west)$) node[blank,above,yshift=1mm] {$k_1$};
\draw [damper] (wall.20) -- ($(M.north west)!(wall.20)!(M.south west)$);
\end{scope}
\begin{scope}[xshift=9cm]
\node (M1) [minimum width=1cm, minimum height=1cm] {$m$} node[blank,below=0.5] {$d_2$};
\draw [spring] (M.10) -- ($(M1.north west)!(M.10)!(M1.south west)$) node[blank,above,yshift=1mm] {$k_2$};
\draw [damper] (M.-40) -- ($(M1.north west)!(M.-40)!(M1.south west)$);
\end{scope}
\begin{scope}[xshift=12cm]
\node (S) [blank,minimum width=1cm, minimum height=1cm] {$\cdots$} node[blank,below=0.225] {$\cdots$};
\end{scope}
\begin{scope}[xshift=11cm]
\draw [spring] (M1.10) -- ($(S.north west)!(M1.10)!(S.south west)$) node[blank,above,yshift=1mm] {$k_3$};
\draw [damper] (M1.-40) -- ($(S.north west)!(M1.-40)!(S.south west)$);
\end{scope}
\begin{scope}[xshift=14cm]
\node (M2) [minimum width=1cm, minimum height=1cm] {$m$} node[blank,below=0.5] {$d_{n-1}$};
\draw [spring] (S.10) -- ($(M2.north west)!(S.10)!(M2.south west)$) node[blank,above,yshift=1mm] {$k_{n-1}$};
\draw [damper] (S.-40) -- ($(M2.north west)!(S.-40)!(M2.south west)$);
\end{scope}
\begin{scope}[xshift=16cm]
\node (M3) [minimum width=1cm, minimum height=1cm] {$m$} node[blank,below=0.5] {$d_n$};
\draw [spring] (M2.10) -- ($(M3.north west)!(M1.10)!(M3.south west)$) node[blank,above,yshift=1mm] {$k_n$};
\draw [damper] (M2.-40) -- ($(M3.north west)!(M1.-40)!(M3.south west)$);
\end{scope}
\begin{scope}[xshift=17cm]
\node[] (wall1) [ground, rotate=90, minimum width=2cm,yshift=-3cm] {};
\draw (wall1.north west) -- (wall1.north east);
\draw [spring] (wall1.60) -- ($(M3.north east)!(wall1.60)!(M3.south east)$) node[blank,above,yshift=1mm] {$k_1$};
\draw [damper] (wall1.160) -- ($(M3.north east)!(wall1.-160)!(M3.south east)$);
\end{scope}
\end{tikzpicture}
\end{document}
我需要帮助让所有弹簧和减震器的长度一致。
答案1
按照 Bobyandbob 的建议使用库的语法positioning
是个好主意,但不要为每个节点明确设置距离,只需将其设置为一次即可node distance
。我还更改了其他一些内容,例如(a -| b)
对阻尼器使用垂直坐标(),并使用label
添加d_N
节点。
我还box
为m
节点定义了一种样式。
无关注释:graphics,epsfig,graphicx
您只需要加载graphicx
。它graphics
已经加载了,而且我相信现在epsfig
只是一个包装器graphicx
,所以不需要它。但无论如何tikz
都会加载graphicx
,所以严格来说,您可以删除整个\usepackage
调用。
\documentclass[11pt]{article}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsfonts}
\usepackage{amsthm}
\usepackage{tikz}
\usetikzlibrary{
decorations.markings,
decorations.pathmorphing,
calc,
patterns,
positioning
}
\tikzset{
spring/.style={thick,decorate,decoration={zigzag,pre length=0.3cm,post length=0.3cm,segment length=6}},
blank/.style={draw=none,fill=none,pos=0.5},
ground/.style={fill,pattern=north east lines,draw=none,minimum width=0.5cm,minimum height=0.3cm},
damper/.style={thick,
decoration={markings, mark connection node=dmp,
mark=at position 0.5 with
{
\node (dmp) [thick,inner sep=0pt,transform shape,rotate=-90,minimum width=10pt,minimum height=3pt,draw=none] {};
\draw [thick] ($(dmp.north east)+(2pt,0)$) -- (dmp.south east) -- (dmp.south west) -- ($(dmp.north west)+(2pt,0)$);
\draw [thick] ($(dmp.north)+(0,-3pt)$) -- ($(dmp.north)+(0,3pt)$);
}
}, decorate
},
box/.style={draw,thick,minimum width=1cm, minimum height=1cm}
}
\begin{document}
\noindent\begin{tikzpicture}[
% modify this to set distance between nodes
node distance=1.2cm
]
\node (wall) [ground, rotate=-90, minimum width=2cm] {};
\node (M1) [box, right=of wall.north,label=below:$d_1$] {$m$};
\node (M2) [box,right=of M1,label=below:$d_2$] {$m$};
\draw (wall.north east) -- (wall.north west);
\draw [spring] (wall.north) -- (M1) node[blank,above,yshift=1mm] {$k_1$};
\draw [damper] (wall.20) -- (wall.20 -| M1.west) coordinate (d1);
\draw [spring] (M1) -- (M2) node[blank,above,yshift=1mm] {$k_2$};
\draw [damper] (d1-|M1.east) -- (d1 -| M2.west);
\coordinate [right=of M2] (k3);
\draw [spring] (M2) -- (k3) node[blank,above,yshift=1mm] {$k_3$};
\draw [damper] (d1-|M2.east) -- (d1 -| k3) coordinate (d3);
\coordinate [right=1cm of k3] (k4);
\node (M3) [box,right=of k4,label=below:$d_{n-1}$] {$m$};
\draw [spring] (k4) -- (M3) node[blank,above,yshift=1mm] {$k_{n-1}$};
\draw [damper] (d1-|k4) -- (d1 -| M3.west);
\node (M4) [box,right=of M3,label=below:$d_{n}$] {$m$};
\draw [spring] (M3) -- (M4) node[blank,above,yshift=1mm] {$k_{n}$};
\draw [damper] (d1-|M3.east) -- (d1 -| M4.west);
\node (wall2) [ground, right=of M4, anchor=north,rotate=90,minimum width=2cm] {};
\draw [spring] (M4) -- (wall2) node[blank,above,yshift=1mm] {$k_{1}$};
\draw [damper] (d1-|M4.east) -- (d1 -| wall2.north);
\path (k3) -- node[draw=none]{$\cdots$} (k4);
\path (d3) -- node[draw=none]{$\cdots$} (d3-|k4);
\end{tikzpicture}
\end{document}
第二版
下面是代码的第二个版本。我单独添加它,因为我对大多数样式做了一些修改。例如,节点名称作为参数添加到样式中,而不是(nodename)
外部节点选项,对于和box
,ground
我都指定了node contents
,因此{text}
不需要。您认为这是好是坏由您决定,我添加它是为了好玩,并介绍更多 TikZ 选项。我还使用循环来绘制所有弹簧和减震器,这缩短了代码。
\documentclass[11pt]{article}
\usepackage{tikz}
\usetikzlibrary{
decorations.markings,
decorations.pathmorphing,
calc,
patterns,
positioning
}
\tikzset{
spring/.style={
thick,
decorate,
decoration={
zigzag,
pre length=0.3cm,
post length=0.3cm,
segment length=6
}
},
ground/.style={
fill,
pattern=north east lines,
draw=none,
minimum width=0.3cm, % height > width,
minimum height=2cm, % instead of rotating
name=#1,
node contents={}
},
damper/.style={
thick,
decoration={
markings,
mark connection node=dmp,
mark=at position 0.5 with
{
% as for the wall, swap width and height to make it clearer what the drawing does
\node (dmp) [thick,inner sep=0pt,minimum width=3pt,minimum height=10pt,draw=none] {};
\draw [thick] ($(dmp.north east)+(2pt,0)$) -| (dmp.south west) -- ($(dmp.south east)+(2pt,0)$);
\draw [thick] ($(dmp.east)+(0,-3pt)$) -- ($(dmp.east)+(0,3pt)$);
}
},
decorate
},
box/.style 2 args={
% style 2 args means use as box={a}{b}
% both arguments are mandatory
draw,thick,
minimum width=1cm,
minimum height=1cm,
name=#1, % #1 refers to first argument, #2 to second
label=below:$d_{#2}$,
node contents={$m$}
},
k-nodes/.style={
midway,
above=1.5mm
}
}
\begin{document}
\begin{center}
\begin{tikzpicture}[
% modify this to set distance between nodes (i.e. length of springs/dampers
node distance=1.1cm
]
% first set up nodes
\node [ground=wall];
\coordinate (ds) at ($(wall.east)!4mm!(wall.south east)$); % starting point for dampers
\node [box={M1}{1}, right=of wall];
\node [box={M2}{2}, right=of M1];
\coordinate [right=of M2] (k3); % end point of k3 spring
\coordinate [right=1cm of k3] (k4); % start point of k4 spring
\coordinate (d3) at (ds -| k3); % end point of third damper
\node [box={M3}{n-1}, right=of k4];
\node [box={M4}{n}, right=of M3];
\node [ground=wall2, right=of M4];
% draw sides of walls
\draw (wall.north east) -- (wall.south east);
\draw (wall2.north west) -- (wall2.south west);
% draw all springs and dampers
\foreach \A/\B/\N in {
wall/M1/k_1,
M1/M2/k_2,
M2/k3/k_3,
k4/M3/k_{n-1},
M3/M4/k_n,
M4/wall2/k_1}
{
\draw [spring] (\A) -- (\B) node[k-nodes] {$\N$};
\draw [damper] (ds -| \A.east) -- (ds -| \B.west);
}
% add dots in the middle
\path (k3) -- node {$\cdots$} (k4);
\path (d3) -- node {$\cdots$} (d3-|k4);
\end{tikzpicture}
\end{center}
\end{document}