有更緊湊的方法來繪製這個四​​分之一車模型嗎?

有更緊湊的方法來繪製這個四​​分之一車模型嗎?

我寫了這段程式碼:

\documentclass[tikz,border=10pt]{standalone}
\usepackage{tikz}
\begin{document}
\pagestyle{empty}

\begin{tikzpicture}

    % Sprung mass
    \shade[top color=gray, bottom color=white, shading angle={135}]
        [draw=black,fill=gray!20,rounded corners=1.2ex,very thick] (1.5,.5) rectangle (6.5,2.5);
    \draw (10,1.5) node {Sprung mass};      
    \draw (4,1.5) node {$M$};

    \draw[->,very thick] (1.5,1.5) -- (0,1.5) -- (0,0);
    \draw (0,-0.5) node {$Z$};

    % Suspension
    \draw[-,very thick] (2.5,.5) -- (3,0.25) -- (2.5,0) -- (3,-.25) -- (2.5,-.5) -- (3,-.75) -- (2.5,-1) -- (3,-1.25) -- (2.5,-1.5) -- (3,-1.75);
    \draw (2,-0.5) node {$K_s$};

    \draw[-,very thick] (5,.5) -- (5,-.3);
    \draw[draw=black,fill=black,very thick] (4.7,-.3) rectangle (5.3,-.5);
    \draw[-,very thick] (4.5,-.3) -- (4.5,-.7) -- (5.5,-.7) -- (5.5,-.3);
    \draw[-,very thick] (5,-.7) -- (5,-1.75);
    \draw (6,-0.5) node {$C_s$};
    \draw (10,-0.5) node {Suspension};

    % Unsprung mass
    \shade[top color=gray, bottom color=white, shading angle={135}]
        [draw=black,fill=gray!20,rounded corners=1.2ex,very thick] (2,-1.75) rectangle (6,-2.75);
    \draw (10,-2.25) node {Unsprung mass};      
    \draw (4,-2.25) node {$m$};

    \draw[->,very thick] (2,-2.25) -- (0,-2.25) -- (0,-3.75);
    \draw (0,-4.25) node {$Z_u$};

    % Tire
    \draw[-,very thick] (3.75,-2.75) -- (4.25,-3) -- (3.75,-3.25) -- (4.25,-3.5) -- (3.75,-3.75) -- (4.25,-4) -- (3.75,-4.25) -- (4.25,-4.5) -- (3.75,-4.75) -- (4.25,-5);
    \draw (3.25,-4) node {$K_t$};
    \draw (10,-4) node {Tire};

    % Road
    \draw[-,thick] (0.5,-5) -- (0.0,-5.5);
    \draw[-,thick] (1.0,-5) -- (0.5,-5.5);
    \draw[-,thick] (1.5,-5) -- (1.0,-5.5);
    \draw[-,thick] (2.0,-5) -- (1.5,-5.5);
    \draw[-,thick] (2.5,-5) -- (2.0,-5.5);
    \draw[-,thick] (3.0,-5) -- (2.5,-5.5);
    \draw[-,thick] (3.5,-5) -- (3.0,-5.5);
    \draw[-,thick] (4.0,-5) -- (3.5,-5.5);
    \draw[-,thick] (4.5,-5) -- (4.0,-5.5);
    \draw[-,thick] (5.0,-5) -- (4.5,-5.5);
    \draw[-,thick] (5.5,-5) -- (5.0,-5.5);
    \draw[-,thick] (6.0,-5) -- (5.5,-5.5);
    \draw[-,thick] (6.5,-5) -- (6.0,-5.5);
    \draw[-,thick] (7.0,-5) -- (6.5,-5.5);
    \draw[-,thick] (7.5,-5) -- (7.0,-5.5);
    \draw[-,thick] (8.0,-5) -- (7.5,-5.5);
    \draw[-,thick] (8.5,-5) -- (8.0,-5.5);
    \draw[->,very thick] (8.5,-5) -- (0,-5) -- (0,-6.5);
    \draw (0,-7) node {$Z_r$};
\end{tikzpicture}

\end{document}

結果是這樣的:

四分之一車模型

問題不是如何繪製這個模型,但是是否有更好、更短的方法用比我更簡單的程式碼來繪製它。

答案1

這是我使用 Tikz 的嘗試。這是一項艱鉅的工作,但程式碼應該更短並且更容易理解。

變更和實施:

  1. 使用foreach指令來繪製小對角線。一行而不是你必須寫的所有內容。
  2. 為了使連接器形狀,我調整了傑克的解決方案固定厚度等。
  3. 設定 .a 內的節點屬性\tikzset。這樣您只需輸入一個關鍵字即可啟動多個選項。結果:節省空間。
  4. 出於組織目的,我首先創建了節點,然後創建了路徑。右邊的節點原本是用標籤製作的,但是使用節點來編寫它們在定位方面更容易。
  5. 鋸齒線是使用裝飾創建的,並將節點定位在該midway位置。它們都使用 內部的屬性snake arrow,因此如果您在那裡進行編輯,則所有使用此鍵的路徑都將被修改。
  6. 定位、計算、箭頭、裝飾都是使用 Tikz 庫完成的,請檢查序言。這可能會在將來對你派上用場。

輸出

圖1

程式碼

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

\usetikzlibrary{arrows, calc,decorations.pathmorphing,positioning,decorations.markings}

\tikzset{
    shadedrec/.style={
        rectangle,
        draw=black,
        top color=gray, 
        bottom color=white, 
        shading angle={135},
        text width=3cm,
        inner sep=1em,
        rounded corners=1.2ex,
        very thick,
        text centered},
    snake arrow/.style={
        decorate,
        decoration={zigzag,amplitude=3mm,segment length=5mm,post length=0mm}},
    damper/.style={
        very thick,
        decoration={markings,  
        mark connection node=dmp,
        mark=at position 0.5 with 
        {
            \node (dmp) [very thick,transform shape,text width=.3cm,rotate=-90,minimum height=3pt,draw=none, fill=black,outer xsep=2pt, outer ysep=1pt] {};
            \draw [very thick] ($(dmp.north east)+(-.6pt,0)$) -- ($(dmp.south east)+(-.6pt,0)$) -- ($(dmp.south west)+(-.6pt,0)$) -- ($(dmp.north west)+(-.6pt,0)$);
            \draw [very thick,rotate=-90] ($(dmp.north)+(0,-5pt)$) -- ($(dmp.north)+(0,5pt)$);
        }
    }, decorate}
}

\begin{document}
\pagestyle{empty}

\begin{tikzpicture}

% Shapes

    \node[shadedrec, anchor=center] (S1) at (4,3) {$M$};
    \node[shadedrec, anchor=center, below=2 of S1] (S2) {$m$};

%Nodes side
    \node[anchor=center,text centered,right=2cm of S1.east] (sm) {Sprung mass};
    \node[below=of sm] (susp) {Suspension};
    \node[below=of susp] (usm) {Unsprung mass};
    \node[below=of usm] {Tire};

% Paths

%side arrows
    \draw[->,very thick] (S1.west) -- ++ (-1.5,0) -- ++ (0,-1.5) node[below] {$Z$};
    \draw[->,very thick] (S2.west) -- ++ (-1.5,0) -- ++ (0,-1.5) node[below] {$Z_u$};

%zigzag lines
    \draw[very thick, snake arrow] ($(S1.south west)!.5!(S1.south)$) -- ++ (0,-2) node[left,midway,xshift=-1em] {$K_s$};
    \draw[very thick, snake arrow] (S2.south) -- ++ (0,-2) 
        node[left,midway,xshift=-1em] {$K_t$};

%Connector shape
    \draw[damper] ($(S2.north east)!.5!(S2.north)$) -- ($(S1.south east)!.5!(S1.south)$) node[right,midway,xshift=1em] {$C_s$};


% Road
    \coordinate (A) at ($(S2.west)+(5.5,-2.45)$);
    \draw[->,very thick] (A) -- ++(-7,0) -- ++ (0,-1.5) node[below] {$Z_r$};

\begin{scope}[shift={($(S2.west)+(-1.5,-2.45)$)}]

    \foreach \x in {0.5,1,...,7} { %This one draws the little diagonal lines
    \draw (\x,0) -- ({\x-.5},-.5);
    }

\end{scope} 
\end{tikzpicture}

\end{document}

答案2

您可以利用以下循環構造tikz

\foreach \k in {0.5,1.0,...,8.5} { 
   \draw[-,thick] (\k,-5) -- (\k-0.5,-5.5);
   }

相關內容