绘制 3D 圆柱体的图形

绘制 3D 圆柱体的图形

我正在尝试绘制这个圆柱体的三维图形,

在此处输入图片描述

最初,我使用的代码是

\usepackage{pgfplots}
    \pgfplotsset{}

\begin{document}
\tdplotsetmaincoords{70}{30}
 \begin{tikzpicture}[tdplot_main_coords]

\draw[->] (0,3,0) -- (0,-3,0) node[above right] {$x$};

\draw[->] (-3,0,0) -- (3,0,0) node[below right] {$y$};

\draw[->] (0,0,-3) -- (0,0,3) node[below right] {$z$};

\draw plot[variable=\x,domain=0:360,samples=180] ({cos(\x)},-1.25,{sin(\x)});

\draw plot[variable=\x,domain=-45:135,samples=180] ({cos(\x)},1.25,{sin(\x)});
\foreach \x in {135,-45};

{\draw ({cos(\x)},-1.25,{sin(\x)}) -- ({cos(\x)},1.25,{sin(\x)});}

\end{tikzpicture}

但事实是这样的,

在此处输入图片描述

我不介意图表的颜色;但形状必须与原始形状相同。如能提供任何帮助,我将不胜感激。

答案1

我猜这不是真正的圆柱体,而是一个底部为椭圆形的圆柱体。如果你只需要这个,你可以使用此代码yscale=0.5

\documentclass[tikz,border=3mm]{standalone}
\usepackage{tikz-3dplot}
\tikzset{declare function={
vcrity(\ph,\th)=atan2(sin(\th)*sin(\ph),min(cos(\ph),-1/sqrt(2))*cos(\th));% critical t value y cylinder
vcritz(\ph,\th)=\ph;% critical t value y cylinder
},pics/ycylinder/.style={code={
\tikzset{3d/cylinder/.cd,#1}
\def\pv##1{\pgfkeysvalueof{/tikz/3d/cylinder/##1}}
\pgfmathsetmacro{\vmin}{vcrity(\tdplotmainphi,\tdplotmaintheta)}
\pgfmathsetmacro{\vmax}{\vmin-180}
\path[3d/cylinder/mantle]
  let \p1=($(0,1,0)-(0,0,0)$),\n1={atan2(\y1,\x1)} in
  [shading angle=\n1]
  plot[variable=\t,domain=\vmin:\vmax,smooth]
    ({\pv{r}*cos(\t)},0,{\pv{r}*sin(\t)})
    -- 
    plot[variable=\t,domain=\vmax:\vmin,smooth]
    ({\pv{r}*cos(\t)},\pv{h},{\pv{r}*sin(\t)})
    --cycle;
\pgfmathtruncatemacro{\itest}{sign(cos(\tdplotmainphi))}
\ifnum\itest=-1
    \path[3d/cylinder/top] let \p1=($(0,1,0)-(0,0,0)$),\n1={atan2(\y1,\x1)} in
    [shading angle=\n1]
    plot[variable=\t,domain=0:360,smooth cycle]
    ({\pv{r}*cos(\t)},\pv{h},{\pv{r}*sin(\t)}) ;
\fi
\ifnum\itest=1
    \path[3d/cylinder/top] let \p1=($(0,1,0)-(0,0,0)$),\n1={atan2(\y1,\x1)} in
    [shading angle=\n1]
    plot[variable=\t,domain=0:360,smooth cycle]
    ({\pv{r}*cos(\t)},0,{\pv{r}*sin(\t)}) ;
\fi
}},3d/.cd,cylinder/.cd,r/.initial=1,h/.initial=1,
mantle/.style={draw,left color=blue,right
color=red,middle color=purple,fill opacity=0.5},top/.style={draw,left color=blue!30,right
color=red!30,middle color=purple!10,fill opacity=0.5}}
\begin{document}
\tdplotsetmaincoords{70}{120}
\begin{tikzpicture}[tdplot_main_coords,scale=2,yscale=0.5,transform shape]
 \draw (-2,0,0) -- (2,0,0) (0,-2,0) -- (0,2,0) (0,0,-1) -- (0,0,1);
 \path (0,-2,0) pic{ycylinder={r=1,h=4,top/.append style={draw}}};
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

一种可能的解决方案是使用shapes.geometric库。使用这个库,cylinder很容易获得。

\documentclass[border=5pt]{standalone}

\usepackage{tikz,tikz-3dplot}
\usetikzlibrary{shapes.geometric}

\begin{document}
\tdplotsetmaincoords{70}{30}
 \begin{tikzpicture}[tdplot_main_coords]

\draw[->] (0,3,0) -- (0,-3,0) node[above] {$x$};
\draw[->] (-3,0,0) -- (3,0,0) node[below right] {$y$};
\draw[->] (0,0,-3) -- (0,0,3) node[below right] {$z$};

\node [cylinder, canvas is xz plane at y=0, draw,
    minimum height=3cm, minimum width=1.5cm] (c) {};

\path[dotted](c.150)  edge [bend left=12] (c.210);
\end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容