我怎样才能用 Tikz 画一列火车

我怎样才能用 Tikz 画一列火车

我怎样才能用 Tikz 画一列像图片这样的火车简单的火车

答案1

这是使用基本 TikZ 可以实现的功能。声明的函数是显式贝塞尔曲线,因为我需要知道一个坐标才能为挡风玻璃形成一个闭合循环:

\documentclass{standalone}

\usepackage{tikz}
    \usetikzlibrary{calc}

\begin{document}
    
    \begin{tikzpicture}[
        declare function = {%
            windshieldupx(\x)   = {6*(1 - \x)*\x*\x + 3*\x*\x*\x};
            windshieldupy(\x)   = {3*(1 - \x)*(1 - \x)*\x + 6*(1 - \x)*\x*\x + 2*\x*\x*\x};
            windshielddownx(\x) = {3*(1 - \x)*(1 - \x)*(1 - \x) + 9*(1 - \x)*(1 - \x)*\x + 3*(1 - \x)*\x*\x + windshieldupx(0.11)*\x*\x*\x};
            windshielddowny(\x) = {2*(1 - \x)*(1 - \x)*(1 - \x) + 3*0.4*(1 - \x)*(1 - \x)*\x + 3*0.4*(1 - \x)*\x*\x + windshieldupy(0.11)*\x*\x*\x};
        }
    ]
    
        \definecolor{window}{RGB}{5,173,190}
        \definecolor{trainbody}{RGB}{64,75,125}
        \definecolor{aroundwindow}{RGB}{50,59,97}
        \definecolor{bottomtrain}{RGB}{27,158,249}
        \definecolor{trainline}{RGB}{164,214,249}
        
        \tikzset{%
            trainwindow/.style = {%
                rectangle,
                fill           = window,
                draw           = none,
                minimum width  = 1cm,
                minimum height = 0.5cm
            }
        }
            
        \newcommand{\trainlength}{25.5}
                    
        \fill[
            trainbody
        ] plot[domain = 0:1, samples = 100] ({windshieldupx(\x)},{windshieldupy(\x)}) {%
            [rounded corners = 0.5cm]
                -- (\trainlength, 2)
        }
            -- ++(0, -2) -- cycle;
            
        \fill[%
            fill = window
        ] plot[domain = 0.11:1, samples = 100] ({windshieldupx(\x)},{windshieldupy(\x)})
            plot[domain = 0:1, samples = 100] ({windshielddownx(\x)},{windshielddowny(\x)})
            -- ({windshieldupx(0.11)},{windshieldupy(0.11)})
            -- cycle;
    
        \fill[%
            aroundwindow
        ] (3.5, {windshieldupy(0.09)}) .. controls (3.5, 0.5) and (3.5, 1.5) .. (5, 1.5)
            -- (\trainlength, 1.5)
            -- (\trainlength, {windshieldupy(0.09)})
            -- cycle;
    
        \foreach \x in {5,6.5,...,\trainlength} {
            \node[trainwindow] at (\x, {(windshieldupy(0.09) + 1.5)/2}) {};
        }
    
        \fill[%
            bottomtrain
        ] {%
            [rounded corners = 0.5cm]
            (\trainlength, 0.1) 
                -- (\trainlength, -1)
                -- (-0.3, -1)
                -- (-0.3, 0.1)}
            -- cycle;
            
        \begin{scope}
        
            \path[clip] {%
                [rounded corners = 0.5cm]
                (\trainlength, 0.1) 
                -- (\trainlength, -1)
                -- (-0.3, -1)
                -- (-0.3, 0.1)}
            -- cycle;
            
            \fill[trainline] (-0.3, -0.2) rectangle (\trainlength, 0.1);
            
        \end{scope}
    
        \foreach \x in {3, 11.5, 20} {
    
            \fill[
                aroundwindow
            ] (\x, -1) .. controls (\x, -0.9) and (\x, -0.9) .. ({\x + 0.1}, -0.7)
                -- ({\x + 2.9}, -0.7) .. controls ({\x + 3}, -0.9) and ({\x + 3}, -0.9) .. ({\x + 3}, -1)
                -- cycle;
        
            \begin{scope}
            
                \path[clip] (\x, -1) .. controls (\x, -0.9) and (\x, -0.9) .. ({\x + 0.1}, -0.7)
                    -- ({\x + 2.9}, -0.7) .. controls ({\x + 3}, -0.9) and ({\x + 3}, -0.9) .. ({\x + 3}, -1)
                    -- cycle;
                
                \fill[trainline] (\x, -0.8) rectangle ({\x + 3}, -0.7);
            
            \end{scope}
            
            \fill[%
                trainbody
            ] ({\x + 0.25}, -0.8) arc (-180:0:0.5); 
        
            \fill[%
                trainbody
            ] ({\x + 1.75}, -0.8) arc (-180:0:0.5); 
    
        }

    \end{tikzpicture}

\end{document}

得出的结果是:

在此处输入图片描述

对于旅行车,可以使用类似的东西,但由于没有挡风玻璃,所以更简单:

\documentclass{standalone}

\usepackage{tikz}
    \usetikzlibrary{calc}

\begin{document}
    
    \begin{tikzpicture}
    
        \definecolor{window}{RGB}{5,173,190}
        \definecolor{trainbody}{RGB}{64,75,125}
        \definecolor{aroundwindow}{RGB}{50,59,97}
        \definecolor{bottomtrain}{RGB}{27,158,249}
        \definecolor{trainline}{RGB}{164,214,249}
        
        \tikzset{%
            trainwindow/.style = {%
                rectangle,
                fill           = window,
                draw           = none,
                minimum width  = 1cm,
                minimum height = 0.5cm
            }
        }
            
        \newcommand{\trainlength}{25.5}

        \fill[trainbody] {%
            [rounded corners = 0.5cm]
                (1.5, 0) |- ({\trainlength - 1.5}, 2)
            } 
            -- ({\trainlength - 1.5}, 0)
            -- cycle;
    
        \fill[aroundwindow] (1.5, {windshieldupy(0.09)}) rectangle ({\trainlength - 1.5}, 1.5);
        
        \foreach \x in {3,4.5,...,23.5} {
            \node[trainwindow] at (\x, {(windshieldupy(0.09) + 1.5)/2}) {};
        }
    
        \fill[bottomtrain] (1.5, 0.1)
            -- ({\trainlength - 1.5}, 0.1) {%
            [rounded corners = 0.5cm]
                -- ({\trainlength - 1.5}, -1)
                -- (1.5, -1)
            } -- cycle;
    
        \fill[trainline] (1.5, -0.2) rectangle ({\trainlength - 1.5}, 0.1);

        \foreach \x in {4, 18.5} {
    
            \fill[
                aroundwindow
            ] (\x, -1) .. controls (\x, -0.9) and (\x, -0.9) .. ({\x + 0.1}, -0.7)
                -- ({\x + 2.9}, -0.7) .. controls ({\x + 3}, -0.9) and ({\x + 3}, -0.9) .. ({\x + 3}, -1)
                -- cycle;
            
            \begin{scope}
            
                \path[clip] (\x, -1) .. controls (\x, -0.9) and (\x, -0.9) .. ({\x + 0.1}, -0.7)
                    -- ({\x + 2.9}, -0.7) .. controls ({\x + 3}, -0.9) and ({\x + 3}, -0.9) .. ({\x + 3}, -1)
                    -- cycle;
                
                \fill[trainline] (\x, -0.8) rectangle ({\x + 3}, -0.7);
                
            \end{scope}
                
            \fill[%
                trainbody
            ] ({\x + 0.25}, -0.8) arc (-180:0:0.5); 
            
            \fill[%
                trainbody
            ] ({\x + 1.75}, -0.8) arc (-180:0:0.5); 
            
        }

    \end{tikzpicture}
    
\end{document}

得出的结果是:

在此处输入图片描述

相关内容