测力计 - 使用锚点和选项创建自定义形状

测力计 - 使用锚点和选项创建自定义形状

我想制作一个称为“测力计”的形状,例如矩形、圆形等,并带有一些锚点和选项。但我不知道如何实现这一目标。

我从这个中取出了测力计的形状话题为了使其成为一个形状,我有一些问题。

  1. 如何为 TikZ 声明形状?这样我就可以写\node[dynamo, rectangle, F={65~N}] at (5,0)
  2. 在声明的形状中,如何创建节点?
  3. 在声明的形状中,如何将选项定义为“矩形”或“F={65~N}”?

这是“我的” MWE:

\documentclass{standalone}

\usepackage{tikz}
\begin{document}
    \begin{tikzpicture}
        %%
        %%  Key wanted :    - Newton value to label the red marker.
        %%                      - Option to use the rectangle or circle boundaries shape.
        %%                      - Node wanted :     - red marker node
        %%                                              - bottom of the hook
        %%                                              - Upper part of the hook
        %%                                              - center of the hook
        %%                                              - upper part of the ring
        %%                                              - 
        
        %% Ring
        \draw[line width=6pt] (2,24) circle (1.5);
        \draw[line width=3pt] (2,22.5) -- ++(0,-3);
        
        %% Hook
        \draw[line width=6pt, gray, rounded corners] (2,9) -- ++(0,-1.5) arc (90:360:1.7);
        
        %% Shape of the dynamometer
%       \draw[line width=3pt, fill=lightgray] (4,20) arc (0:180:2) -- ++(0,-10) arc (180:360:2) -- cycle;   %% bords en cercle
        \draw[line width=3pt, fill=lightgray, rounded corners] (3.5,21) rectangle ++(-3,-12);                   %% bord rectangle arrondi
        
        %% Scale
        % Vertical line
        \draw[line width=3pt] (2,20) -- ++(0,-10); 
        
        % Graduation with red marker
        \foreach \i in {0,...,20}{
            \ifnum\i=5
            \draw[line width=5pt, red] 
            \else
            \draw[line width=0.5pt] 
            \fi
            (1.5,{10+0.5*\i}) -- ++(1,0);
        }
    \end{tikzpicture}
\end{document}

答案1

您可以创建一个节点形状(您可能需要研究一下 PGF/Ti如果您想多次重复使用此绘图(我实际上假设这是您的目标),请参阅 Z 手册以了解一般如何执行此操作),但我不确定形状是否足够灵活以满足您的要求。在我看来,最好pic从中创建一个,因为它可以让您轻松更改标记位置:

\documentclass[border=10pt]{standalone}
\usepackage{tikz}

\tikzset{
    pics/dynamometer/.style={
        code={
            \tikzset{dynamometer/.cd, #1}
            
            %% Ring
            \draw[line width=6pt] (2,24) circle (1.5);
            \draw[line width=3pt] (2,22.5) -- ++(0,-3);
            \coordinate (-ring-top) at (2,25.5);
        
            %% Hook
            \draw[line width=6pt, gray, rounded corners] (2,9) -- ++(0,-1.5) arc (90:360:1.7);
            \coordinate (-hook-top) at (2,7.5);
            \coordinate (-hook-center) at (2,5.8);
            \coordinate (-hook-bottom) at (2,4.1);
        
            %% Shape of the dynamometer
            \draw[line width=3pt, fill=lightgray, rounded corners] (3.5,21) rectangle ++(-3,-12); 
            
            %% Scale
            % Vertical line
            \draw[line width=3pt] (2,20) -- ++(0,-10); 
            
            % Graduation with red marker
            \foreach \i in {0,...,20} {
                \draw[line width=0.5pt] (1.5,{10+0.5*\i}) -- ++(1,0);
            }
            \coordinate (-marker) at (2,{10+0.5*\pgfkeysvalueof{/tikz/dynamometer/marker position}});
            \draw[line width=5pt, red] (1.5,{10+0.5*\pgfkeysvalueof{/tikz/dynamometer/marker position}}) -- ++(1,0);
        }
    },
    dynamometer/marker position/.initial={5}
}

\begin{document}
    \begin{tikzpicture}
        %%
        %%  Key wanted :    - Newton value to label the red marker.
        %%                  - Option to use the rectangle or circle boundaries shape.
        %%                  - Node wanted :     - red marker node
        %%                                      - bottom of the hook
        %%                                      - Upper part of the hook
        %%                                      - center of the hook
        %%                                      - upper part of the ring
        %%                                      
        
        \pic (mymeter) at (0,0) {dynamometer};
        
        \node[circle, inner sep=3pt, fill=yellow] at (mymeter-ring-top) {};
        \node[circle, inner sep=3pt, fill=green] at (mymeter-marker) {};
        \node[circle, inner sep=3pt, fill=cyan] at (mymeter-hook-top) {};
        \node[circle, inner sep=3pt, fill=blue] at (mymeter-hook-center) {};
        \node[circle, inner sep=3pt, fill=magenta] at (mymeter-hook-bottom) {};

        \pic at (5,0) {dynamometer={marker position=15}};
    \end{tikzpicture}
\end{document}

在此处输入图片描述

我不知道秤显示的是什么单位,所以我选择中性marker position作为图片的选项。


如果您想要一个高度可风格化的变体,您也许可以使用这个(它还会根据标记位置扩展钩子):

\documentclass[border=10pt]{standalone}
\usepackage{tikz}

\tikzset{
    pics/dynamometer/.style={
        code={
            \tikzset{dynamometer/.cd, #1}
            
            %% Ring
            \draw[dynamometer/ring] (2,24) circle[radius=1.5];
            \draw[dynamometer/ring connection] (2,22.5) -- ++(0,-3);
            \coordinate (-ring-top) at (2,25.5);
        
            %% Hook
            \draw[dynamometer/hook] (2,{10+0.5*\pgfkeysvalueof{/tikz/dynamometer/marker position}}) -- ++(0,-11.5) arc[start angle=90, end angle=360, radius=1.7];
            \coordinate (-hook-top) at (2,{-1.5+0.5*\pgfkeysvalueof{/tikz/dynamometer/marker position}});
            \coordinate (-hook-center) at (2,{-3.2+0.5*\pgfkeysvalueof{/tikz/dynamometer/marker position}});
            \coordinate (-hook-bottom) at (2,{-4.9+0.5*\pgfkeysvalueof{/tikz/dynamometer/marker position}});
        
            %% Shape of the dynamometer
            \draw[dynamometer/meter] (3.5,21) rectangle ++(-3,-12); 
            
            %% Scale
            % Vertical line
            \draw[dynamometer/scale] (2,20) -- ++(0,-10); 
            
            % Graduation with red marker
            \foreach \i in {0,...,20} {
                \draw[dynamometer/tick] (1.5,{10+0.5*\i}) -- ++(1,0);
            }
            \coordinate (-marker) at (2,{10+0.5*\pgfkeysvalueof{/tikz/dynamometer/marker position}});
            \draw[dynamometer/marker] (1.5,{10+0.5*\pgfkeysvalueof{/tikz/dynamometer/marker position}}) -- ++(1,0);
        }
    },
    dynamometer/marker position/.initial={5},
    dynamometer/ring/.style={line width=6pt},
    dynamometer/ring connection/.style={line width=3pt},
    dynamometer/hook/.style={line width=6pt, gray, rounded corners},
    dynamometer/meter/.style={line width=3pt, fill=lightgray, rounded corners},
    dynamometer/scale/.style={line width=3pt},
    dynamometer/tick/.style={line width=0.5pt},
    dynamometer/marker/.style={line width=5pt, red},
}

\begin{document}
    \begin{tikzpicture}
        %%
        %%  Key wanted :    - Newton value to label the red marker.
        %%                  - Option to use the rectangle or circle boundaries shape.
        %%                  - Node wanted :     - red marker node
        %%                                      - bottom of the hook
        %%                                      - Upper part of the hook
        %%                                      - center of the hook
        %%                                      - upper part of the ring
        %%                                      
        
        \pic (mymeter) at (0,0) {dynamometer};
        
        \node[circle, inner sep=3pt, fill=yellow] at (mymeter-ring-top) {};
        \node[circle, inner sep=3pt, fill=green] at (mymeter-marker) {};
        \node[circle, inner sep=3pt, fill=cyan] at (mymeter-hook-top) {};
        \node[circle, inner sep=3pt, fill=blue] at (mymeter-hook-center) {};
        \node[circle, inner sep=3pt, fill=magenta] at (mymeter-hook-bottom) {};

        \pic at (5,0) {dynamometer={marker position=20, meter/.append style={sharp corners, fill=white}, hook/.append style={draw=blue}}};
    \end{tikzpicture}
\end{document}

在此处输入图片描述


为标签添加了一些选项:

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usepackage{siunitx}

\tikzset{
    pics/dynamometer/.style={
        code={
            \tikzset{dynamometer/.cd, #1}
            
            %% Ring
            \draw[dynamometer/ring] (2,24) circle[radius=1.5];
            \draw[dynamometer/ring connection] (2,22.5) -- ++(0,-3);
            \coordinate (-ring-top) at (2,25.5);
        
            %% Hook
            \draw[dynamometer/hook] (2,{10+0.5*\pgfkeysvalueof{/tikz/dynamometer/marker position}}) -- ++(0,-11.5) arc[start angle=90, end angle=360, radius=1.7];
            \coordinate (-hook-top) at (2,{-1.5+0.5*\pgfkeysvalueof{/tikz/dynamometer/marker position}});
            \coordinate (-hook-center) at (2,{-3.2+0.5*\pgfkeysvalueof{/tikz/dynamometer/marker position}});
            \coordinate (-hook-bottom) at (2,{-4.9+0.5*\pgfkeysvalueof{/tikz/dynamometer/marker position}});
        
            %% Shape of the dynamometer
            \draw[dynamometer/meter] (3.5,21) rectangle ++(-3,-12); 
            
            %% Scale
            % Vertical line
            \draw[dynamometer/scale] (2,20) -- ++(0,-10); 
            
            % Graduation with red marker
            \foreach \i in {0,...,20} {
                \draw[dynamometer/tick] (1.5,{10+0.5*\i}) -- ++(1,0);
            }
            \coordinate (-marker) at (2,{10+0.5*\pgfkeysvalueof{/tikz/dynamometer/marker position}});
            \draw[dynamometer/marker] (1.5,{10+0.5*\pgfkeysvalueof{/tikz/dynamometer/marker position}}) -- ++(1,0);
            \node[dynamometer/marker label] at (-marker) (-marker-label) {\pgfkeysvalueof{/tikz/dynamometer/marker label content}};
        }
    },
    dynamometer/marker position/.initial={5},
    dynamometer/ring/.style={line width=6pt},
    dynamometer/ring connection/.style={line width=3pt},
    dynamometer/hook/.style={line width=6pt, gray, rounded corners},
    dynamometer/meter/.style={line width=3pt, fill=lightgray, rounded corners},
    dynamometer/scale/.style={line width=3pt},
    dynamometer/tick/.style={line width=0.5pt},
    dynamometer/marker/.style={line width=5pt, red},
    dynamometer/marker label/.style={right=0.5cm},
    dynamometer/marker label content/.initial={},
}

\begin{document}
    \begin{tikzpicture}
        %%
        %%  Key wanted :    - Newton value to label the red marker.
        %%                  - Option to use the rectangle or circle boundaries shape.
        %%                  - Node wanted :     - red marker node
        %%                                      - bottom of the hook
        %%                                      - Upper part of the hook
        %%                                      - center of the hook
        %%                                      - upper part of the ring
        %%                                      
        
        \pic (mymeter) at (0,0) {dynamometer={marker label content={\Huge\qty{65}{\newton}}, marker label/.append style={circle, fill=yellow}}};
    \end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

像这样:

在此处输入图片描述

代码:

\documentclass[border=10pt]{standalone}
\usepackage{tikz,siunitx,calc}
\usetikzlibrary{decorations.pathmorphing,patterns}
\sisetup{round-mode = places, round-precision = 3}
\begin{document}
    
    \begin{tikzpicture}[scale=.65]
        \draw[gray,line width=12pt] (0,0) arc (180:0:2);
        \filldraw[brown] (-1,0) rectangle (5,-22);
            \node[white] at (2.1,-1.5) (a) {\bfseries \large Range: 0-1.800 kg};
            \node[white] at (2.1,-2.3) (a) {\bfseries 8 mm $\rightarrow$ 100 g};
            \draw[decoration={aspect=0.2, segment length=1mm, amplitude=2mm,coil},decorate] (2.08,-3) -- (2.05,-4);
            \draw[gray,line width=10pt] (2.1,-4)--(2.1,-21);
            \draw[cyan,line width=20pt,opacity=.4] (2.1,-3)--(2.1,-21);
            \foreach \i in {0,1,...,18}{
                \pgfmathsetmacro\h{-4-.8*\i};
                \pgfmathsetmacro\j{-4.5-.8*\i};
                %\pgfmathsetmacro{\j}{int(90-\i)}
                \pgfmathsetmacro{\k}{.1*\i};
                \draw[white,line width=3pt] (1.6,\h)--(1.2,\h) node[left] (\i) {\bfseries \large $\i$};
                \draw[white,line width=3pt] (2.6,\h)--(3.0,\h) node[right] (\i) {\bfseries \large \num{\k}};
                }
        \draw[red,line width=3,-latex] (1.5,-4)--(2.7,-4);
        \draw[gray,line width=10pt,rounded corners] (2.1,-22)--(2.1,-23) arc (90:350:2);
    \end{tikzpicture}
        
\end{document}

相关内容