我有以下生成圆柱体的代码
\documentclass[border=1cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric,calc,decorations.pathmorphing,shapes,arrows,snakes,patterns}
\usepackage{amsmath}
\begin{document}
\begin{tikzpicture}[scale = 4, every node/.style={scale = 4}, mycyl/.style={cylinder, shape border rotate=90, draw, minimum width=1cm, aspect=0.25,anchor=south, text width=1cm, text height=.1cm}]
\node [mycyl , fill=blue, minimum height=1.6cm] (bl) at (0,0) {};
\node [mycyl , minimum height=.3cm, fill = cyan] (yell) at (0,1) { };
\foreach \y in {.6, .7, .8}{
\draw[dashed, cyan] (.619,\y+.3) arc[x radius=.619cm, y radius=.154cm, start angle=0, end angle=-180];}
\end{tikzpicture}
\end{document}
导致
现在我想重复一下,但要画圆锥形状。我见过许多绘制圆锥形状的例子,这里用了一个:
\documentclass[border=2cm]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\shade[top color=blue!40!white,opacity=0.75] (-1,0) arc (180:0:2cm and 0.5cm) -- (1,-4) -- cycle;
\draw [](-1,0) arc (180:360:2cm and 0.5cm) -- (1,-4) -- cycle;
\draw [](-1,0) arc (180:0:2cm and 0.5cm);
\end{tikzpicture}
\end{document}
但是,除了圆锥形状外,我不确定如何重复上述操作。具体来说,我不确定如何绘制虚线以及如何将圆锥分成两个区域。有人能建议如何做到这一点吗?
答案1
这是一个糟糕的(人类的)解决方案,非常手动。
\documentclass[border=.5cm]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw [fill=blue!40!white,opacity=1] (-1.99,3.98) -- (1.99,3.98) -- (0,0) -- cycle;
\draw [fill=blue!20!white,opacity=1,] (0,4) circle (1.99cm and 0.4cm);
\draw [fill=yellow!40!white,opacity=1] (-1.49,2.98) -- (1.49,2.98) -- (0,0) -- cycle;
\draw [fill=yellow!20!white,opacity=1,] (0,3) circle (1.49cm and 0.3cm);
\end{tikzpicture}
\end{document}
答案2
参数化和pgfplots
是你的朋友。我曾经使用过
x = r cos(theta)
y = r sin(theta)
z = r
并绘制了两次锥体,0\leq z \leq 1
一次是1\leq z \leq 2
% arara: pdflatex
\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[view={60}{30}]
\addplot3[surf,shader=flat,blue!20!white,
samples=20,opacity=0.5,
domain=1:2,y domain=0:2*pi,
z buffer=sort]
({x * cos(deg(y))}, {x * sin(deg(y))}, {x});
\addplot3[surf,shader=flat,blue,
samples=20,opacity=0.5,
domain=0:1,y domain=0:2*pi,
z buffer=sort]
({x * cos(deg(y))}, {x * sin(deg(y))}, {x});
\end{axis}
\end{tikzpicture}
\end{document}
您可以根据自己的需要选择颜色和按键。
答案3
其他解决方案也使用蒂克兹,但它是自动的,您只需要定义圆锥尺寸。
代码
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{shadings,calc}
\begin{document}
\begin{tikzpicture}
%Cone data
\pgfmathsetmacro{\radiush}{2};%Cone base radius
\pgfmathsetmacro{\theight}{5}%Cone height (negative if you want a inverse cone)
\pgfmathsetmacro{\cheightp}{.4}%Cut height in percent of cone height
%Calculating coordinates
\coordinate (center) at (0,0);
\pgfmathsetmacro{\radiusv}{.2 * \radiush};
\pgfmathsetmacro{\sradiush}{\radiush * (1 - \cheightp)};%only for right circular cone
\pgfmathsetmacro{\sradiusv}{.2 * \sradiush};
\coordinate (peak) at ($(center) + (0,\theight)$);
\coordinate (vert1) at ($(center)+(\radiush,0)$);
\coordinate (vert2) at ($(center)-(\radiush,0)$);
\coordinate (svert1) at ($(vert1)!\cheightp!(peak)$);
\coordinate (svert2) at ($(vert2)!\cheightp!(peak)$);
%Drawing
\fill[left color=red!70,right color=red!70,middle color=red!40,shading=axis] (svert1) -- (peak) -- (svert2) arc (180:360:\sradiush cm and \sradiusv cm);
\fill[left color=gray!70,right color=gray!70,middle color=gray!30,shading=axis] (vert1) -- (svert1) arc (0:-180:\sradiush cm and \sradiusv cm) -- (vert2) arc (180:360:\radiush cm and \radiusv cm);
%Uncomment this for an inverted cone
%\fill[inner color=gray!30,outer color=gray!50,shading=radial] (0,0) circle (\radiush cm and \radiusv cm);
%Lines, \h in percent of cone height
\foreach \h in {.38,.34,.30}{
\pgfmathsetmacro{\rh}{\radiush * (1 - \h)}
\pgfmathsetmacro{\rv}{.2 * \rh}
\draw[black!70,densely dashed] ($(vert2)!\h!(peak)$) arc (180:360:\rh cm and \rv cm);
}
\end{tikzpicture}
\end{document}
结果
或者如果高度是负数:
如果您想绘制边框,可以使用\shadedraw
而不是。\fill
有人可能可以改进代码,我只是觉得这很有趣。