我在绘制垂直右弹簧(将墙连接到底部块)时遇到了问题。代码是:
\documentclass{article}
\usepackage{tikz,pgfplots}
\pgfplotsset{compat=1.6}
\usetikzlibrary{arrows.meta}
\usetikzlibrary{
decorations.markings,
decorations.pathmorphing,
calc,
patterns,
positioning
}
\begin{document}
\begin{tikzpicture}[every node/.style={draw,outer sep=0pt,thick}]
\tikzstyle{spring}=[thick,decorate,decoration={zigzag,pre length=0.3cm,post length=0.3cm,segment length=6}]
\tikzstyle{damper}=[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=15pt,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,-5pt)$) -- ($(dmp.north)+(0,5pt)$);
}
}, decorate]
\tikzstyle{ground}=[fill,pattern=north east lines,draw=none,minimum width=0.75cm,minimum height=0.3cm]
\node [ground, rotate=180, minimum width=12cm,yshift=-5cm] (wall) {};
\node [minimum width=12cm, minimum height=0.5cm] (m1) {$m_1$};
\draw (wall.north east) -- (wall.north west);
\draw [line width=0.8pt] ($(wall.north west)!0.1!(wall.north east)$) -- ++(0,-1cm)coordinate (z);
\draw [spring] (z) -- ++(0,-3cm)coordinate (u);
\draw [line width=0.8pt] (u) -- ($(m1.north east)!0.1!(m1.north east)$);
\end{tikzpicture}
\end{document}
如何将弹簧的底部端点向左移动,如输出所示?
提前致谢。
答案1
在 TikZ 中,(a |- b)
(分别是(a -| -b)
)是横坐标为(a)
、纵坐标为 的点(分别是纵坐标为、横坐标为 的(b)
点)。(a)
(b)
不要使用\tikzstyle
:它已经过时了。您可以用pgfkeys
诸如 之类的键分配来替换它spring/.style={...}
,如下所示。下次请通过删除未使用的内容(如我在此处所做的那样)使您的示例更接近最小示例(例如,damper
您的示例中未使用该样式)。
\documentclass[tikz, border=2mm]{standalone}
\usetikzlibrary{calc, decorations.pathmorphing, patterns}
\begin{document}
\begin{tikzpicture}[
every node/.style={draw, outer sep=0pt, thick},
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.75cm, minimum height=0.3cm}]
\node[minimum width=12cm, minimum height=0.5cm] (m1) {$m_1$};
\node at (0,5cm) [ground, minimum width=12cm] (wall) {};
\draw (wall.south east) -- (wall.south west);
\draw[line width=0.8pt]
($(wall.south west)!0.9!(wall.south east)$) -- ++(0,-1cm) coordinate (z);
\draw[spring] (z) -- ++(0,-3cm) coordinate (u);
\draw[line width=0.8pt] (u) -- (u |- m1.north);
\end{tikzpicture}
\end{document}
以下是不带 的等效代码calc
,它($(wall.south west)!0.9!(wall.south east)$)
使用不同的技术来查找点。请注意以下两行:
\draw (wall.south west) -- (wall.south east) coordinate[pos=0.9] (p);
\draw[line width=0.8pt] (p) -- ++(0,-1cm) coordinate (z);
完整代码:
\documentclass[tikz, border=2mm]{standalone}
\usetikzlibrary{decorations.pathmorphing, patterns}
\begin{document}
\begin{tikzpicture}[
every node/.style={draw, outer sep=0pt, thick},
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.75cm, minimum height=0.3cm}]
\node[minimum width=12cm, minimum height=0.5cm] (m1) {$m_1$};
\node at (0,5cm) [ground, minimum width=12cm] (wall) {};
\draw (wall.south west) -- (wall.south east) coordinate[pos=0.9] (p);
\draw[line width=0.8pt] (p) -- ++(0,-1cm) coordinate (z);
\draw[spring] (z) -- ++(0,-3cm) coordinate (u);
\draw[line width=0.8pt] (u) -- (u |- m1.north);
\end{tikzpicture}
\end{document}
damper
样式示例
\documentclass[tikz, border=2mm]{standalone}
\usetikzlibrary{calc, decorations.markings, decorations.pathmorphing, patterns}
\begin{document}
\begin{tikzpicture}[
every node/.style={draw, outer sep=0pt, thick},
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.75cm, 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=15pt, minimum height=3pt, draw=none] {};
\draw [thick] ([xshift=2pt] dmp.north east) -- (dmp.south east) --
(dmp.south west) -- ([xshift=2pt] dmp.north west);
\draw [thick] ([yshift=-5pt] dmp.north) -- ++(0,10pt);
},
},
decorate,
}]
\node[minimum width=12cm, minimum height=0.5cm] (m1) {$m_1$};
\node at (0,5cm) [ground, minimum width=12cm] (wall) {};
\draw (wall.south west) -- (wall.south east) coordinate[pos=0.9] (p);
\draw[line width=0.8pt] (p) -- ++(0,-1cm) coordinate (z);
\draw[spring] (z) -- ++(0,-3cm) coordinate (u);
\draw[line width=0.8pt] (u) -- (u |- m1.north);
\coordinate (q) at ($(wall.south west)!0.7!(wall.south east)$);
\draw[damper] (q) -- (q |- m1.north);
\end{tikzpicture}
\end{document}