如何在 tikz 中制作简单的 2D 压力容器图纸?

如何在 tikz 中制作简单的 2D 压力容器图纸?

两件事情:

1)如何使形状的半圆部分具有与直线相同的粗细?

2)如何用填充灰色填充整体形状?

这是我目前的代码:

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}[scale=0.5]
\draw (10,0.4) arc (90:270:0.4cm);
\draw (10,0.4) -- (12,0.4);
\draw (10,-0.4) -- (12,-0.4);
\draw (12,0.4) arc (90:-90:0.4cm);
\end{tikzpicture}
\end{document}

答案1

它是断开的路径连接和查看器渲染质量的组合。您可以一次性绘制它,也可以用节点代替。

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}
\draw[blue,fill=blue!30] (0,0.4) arc (90:270:0.4cm)-- (2,-0.4) arc (-90:90:0.4cm)--cycle;
\node[draw=red,fill=red!30,rounded corners=0.4cm,minimum height=0.8cm,minimum width=2.8cm,inner sep=0mm,transform shape] at (1,1) {};
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

使用 PSTricks 的解决方案只是为了好玩。

\documentclass[pstricks,border=12pt]{standalone}
\begin{document}
\begin{pspicture}(10,4)
    \pscustom[dimen=m,fillstyle=solid,fillcolor=orange,linewidth=2pt]
    {
        \psarc(2,2){2}{90}{-90}
        \psarc(8,2){2}{-90}{90}
        \closepath
    }
\end{pspicture}
\end{document}

在此处输入图片描述

答案3

仅供比较,在 Metapost 中您可以直接使用halfcircles 绘制路径,也可以使用rboxes库制作圆角框。

在此处输入图片描述

prologues:=3;outputtemplate:="%j%c.eps";

% A macro that returns the path of a rectangle with rounded ends
vardef vessel(expr width, height) = 
   halfcircle rotated -90 scaled height shifted (+1/2 width,0) --
   halfcircle rotated +90 scaled height shifted (-1/2 width,0) --
   cycle
   enddef;

% for the alternative approach
input rboxes;

beginfig(1);

   % draw the shape centred at the origin
   draw vessel(40,20);

   % in order to fill, draw and label the shapes, you can save them as path variables
   path a,b; 

   a = vessel(55,21) shifted 30 up;
   fill a withcolor .4 blue + .6 white;
   draw a withcolor .8 blue;
   label(btex Vessel $V_1$ etex, center a);

   b = vessel(55,21) shifted 55 up;
   fill b withcolor .4 red + .6 white;
   draw b withcolor .8 red;
   label(btex Vessel $V_2$ etex, center b);

   % or you might prefer the "rboxes" approach
   rboxit.c(btex Vessel $V_3$ etex);
   c.dx = 18; c.dy = 5;
   c.s = 70 up; 
   fill bpath.c withcolor .85 white;
   drawboxed(c);

endfig;
end

相关内容