TikZ 中的 3D 物体

TikZ 中的 3D 物体

有没有类似这样的库,里面有默认的几何图形,比如下面这些。我还不熟悉 TikZ 中的 3D 图表。

样本

答案1

这是球体、圆锥体和截头圆锥体:

\documentclass[parskip]{scrartcl}
\usepackage[margin=15mm]{geometry}
\usepackage{tikz}
\usetikzlibrary{3d,calc}

\begin{document}

\begin{tikzpicture}
    \draw (-1,0) arc (180:360:1cm and 0.5cm);
    \draw[dashed] (-1,0) arc (180:0:1cm and 0.5cm);
    \draw (0,1) arc (90:270:0.5cm and 1cm);
    \draw[dashed] (0,1) arc (90:-90:0.5cm and 1cm);
    \draw (0,0) circle (1cm);
    \shade[ball color=blue!10!white,opacity=0.20] (0,0) circle (1cm);
\end{tikzpicture}

\begin{tikzpicture}
    \draw (-1,0) arc (180:360:1cm and 0.5cm) -- (0,3) -- cycle;
    \draw[dashed] (-1,0) arc (180:0:1cm and 0.5cm);
    \shade[left color=blue!5!white,right color=blue!40!white,opacity=0.3] (-1,0) arc (180:360:1cm and 0.5cm) -- (0,3) -- cycle;
\end{tikzpicture}

\begin{tikzpicture}
    \draw (-1,0) arc (180:360:1cm and 0.5cm);
    \draw (-1,0) arc (180:0:1cm and 0.5cm);
    \draw (-2,-3) arc (180:370:2cm and 1cm);
    \draw[dashed] (-2,-3) arc (180:10:2cm and 1cm);
    \draw(-2,-2.9)  -- (-1,0);
    \draw(2,-2.9)   -- (1,0);
    \shade[left color=blue!5!white,right color=blue!60!white,opacity=0.3] (-1,0) arc (180:360:1cm and 0.5cm) -- (2,-3) arc (360:180:2cm and 1cm) -- cycle;
    \shade[left color=blue!5!white,right color=blue!60!white,opacity=0.3] (0,0) circle (1cm and 0.5cm);
\end{tikzpicture}

\end{document}

在此处输入图片描述


编辑1:以下是一些改进灵感阿尔特蒙德斯答案,不幸的是仍然缺少一个解决方案“锥边问题

\documentclass[parskip]{scrartcl}
\usepackage[margin=15mm]{geometry}
\usepackage{tikz}
\usetikzlibrary{shadings}

\begin{document}

\begin{tikzpicture}
\fill[top color=gray!50!black,bottom color=gray!10,middle color=gray,shading=axis,opacity=0.25] (0,0) circle (2cm and 0.5cm);
\fill[left color=gray!50!black,right color=gray!50!black,middle color=gray!50,shading=axis,opacity=0.25] (2,0) -- (0,6) -- (-2,0) arc (180:360:2cm and 0.5cm);
\draw (-2,0) arc (180:360:2cm and 0.5cm) -- (0,6) -- cycle;
\draw[densely dashed] (-2,0) arc (180:0:2cm and 0.5cm);
\end{tikzpicture}
\hspace{1cm}
\begin{tikzpicture}
\fill[top color=gray!50!black,bottom color=gray!10,middle color=gray,shading=axis,opacity=0.25] (0,0) circle (2cm and 0.5cm);
\fill[left color=gray!50!black,right color=gray!50!black,middle color=gray!50,shading=axis,opacity=0.25] (2,0) -- (2,6) arc (360:180:2cm and 0.5cm) -- (-2,0) arc (180:360:2cm and 0.5cm);
\fill[top color=gray!90!,bottom color=gray!2,middle color=gray!30,shading=axis,opacity=0.25] (0,6) circle (2cm and 0.5cm);
\draw (-2,6) -- (-2,0) arc (180:360:2cm and 0.5cm) -- (2,6) ++ (-2,0) circle (2cm and 0.5cm);
\draw[densely dashed] (-2,0) arc (180:0:2cm and 0.5cm);
\end{tikzpicture}

\end{document}

在此处输入图片描述

答案2

该代码是草稿版本。我需要删除 xkeyval 并使用 pgfkeys。我需要添加一些选项:

在此处输入图片描述

\documentclass[11pt]{scrartcl} 
\PassOptionsToPackage{dvipsnames,svgnames}{xcolor}     
\usepackage{xkeyval,tkz-base}
\usetikzlibrary{arrows,calc}
 \makeatletter%   
% \pgfkeys{
% /tkzcone/.cd,
% }    

\define@cmdkey[TKZ]{ell}{color}{}
\define@cmdkey[TKZ]{ell}{shift}{}  
\presetkeys[TKZ]{ell}{color = {},shift = 0}{}
 %  (#2,#3) coordonnée du centre (#4,#5) Ra et Rb 

\newcommand*{\ellipseThreeD}[1][]{\tkz@ellipseThreeD[#1]}% 
\def\tkz@ellipseThreeD[#1](#2,#3)(#4,#5){%
\setkeys[TKZ]{ell}{#1}%
  \draw[yshift=\cmdTKZ@ell@shift cm,dashed] (#4,0) arc(0:180:#4 and #5);
  \draw[yshift=\cmdTKZ@ell@shift cm ] (-#4,0) arc(180:360:#4 and #5); 
  \path[fill=\cmdTKZ@ell@color,opacity=0.5,shade](#2 cm,#3 cm) ellipse (#4 and #5);  
}

\newcommand*{\sellipseThreeD}[1][]{\tkz@sellipseThreeD[#1]}% 
\def\tkz@sellipseThreeD[#1](#2,#3)(#4,#5){%
\setkeys[TKZ]{ell}{#1}%
  \draw[yshift=\cmdTKZ@ell@shift cm,dashed] (#4,0) arc(0:180:#4 and #5);
  \draw[yshift=\cmdTKZ@ell@shift cm ] (-#4,0) arc(180:360:#4 and #5); 
} 

\def\tkzCone{\pgfutil@ifnextchar[{\tkz@cone}{\tkz@cone[]}} 
\def\tkz@cone[#1]#2#3#4{%
% #1    styles
% #2    rayon R
% #3    coeff d'aplatissement k
% #4    Hauteur du cône H   
% \pgfkeys{%
% /tkzcone/.cd
% }% 
% \pgfqkeys{/tkzcone}{#1}%     
\pgfmathsetmacro{\bb}{#2*#3}          
\pgfmathsetmacro{\yy}{\bb*\bb/#4}  
\pgfmathsetmacro{\xx}{#2*sqrt((1-\yy)/#4)} 
\fill[color=Maroon!10] (0,#4)--(-\xx,\yy)  arc(180:360:\xx cm and .5 cm); 
\ellipseThreeD[color=Maroon!30](0,0)(\xx cm,.5 cm)
\draw (0,#4)--(\xx,\yy);
\draw (0,#4)--(-\xx,\yy); 
}% 

\def\tkzCylinder{\pgfutil@ifnextchar[{\tkz@cylinder}{\tkz@cylinder[]}} 
\def\tkz@cylinder[#1]#2#3#4{% 
\pgfmathsetmacro{\bb}{#2*#3}          
\pgfmathsetmacro{\yy}{\bb*\bb/#4}  
\pgfmathsetmacro{\xx}{#2*sqrt((1-\yy)/#4)}
  \fill[color=Maroon!10] (-\xx cm,0)--(-\xx cm,#4 cm)  
         arc(180:360:\xx cm and .5 cm)--(\xx cm,0) 
         arc(360:180:\xx cm and .5 cm);   
\ellipseThreeD[color=Maroon!30](0,0)(\xx cm,.5 cm)
\begin{scope}[yshift=#4 cm]
  \draw[fill=\cmdTKZ@ell@color,opacity=0.5,shade](0,0) ellipse (\xx cm and .5 cm) ;  
\end{scope}
\draw (\xx cm,0)--(\xx cm,#4 cm);
\draw (-\xx cm,0)--(-\xx cm,#4 cm); 
}%  

\def\tkzTruncatedCone{\pgfutil@ifnextchar[{\tkz@TruncatedCone}{\tkz@TruncatedCone[]}} 
\def\tkz@TruncatedCone[#1]#2#3#4{%   
\pgfmathsetmacro{\bb}{#2*#3}          
\pgfmathsetmacro{\yy}{\bb*\bb/#4}  
\pgfmathsetmacro{\xx}{#2*sqrt((1-\yy)/#4)}
  \fill[color=Maroon!10] (-\xx cm,0)--(-0.5*\xx cm,#4 cm)  
    arc(180:360:0.5*\xx cm and .25 cm)--(\xx cm,0) arc(360:180:\xx cm and .5 cm);     
\ellipseThreeD[color=Maroon!30](0,0)(\xx cm,.5 cm)
\begin{scope}[yshift=#4 cm]
  \draw[fill=\cmdTKZ@ell@color,opacity=0.5,shade](0,0) ellipse (0.5*\xx cm and .25 cm);  
\end{scope}
 \draw (\xx cm,0)--(0.5*\xx cm,#4 cm);
 \draw (-\xx cm,0)--(-0.5*\xx cm,#4 cm); 
}%   

\def\tkzSphere{\pgfutil@ifnextchar[{\tkz@Sphere}{\tkz@Sphere[]}} 
\def\tkz@Sphere[#1]#2#3#4{%
\pgfmathsetmacro{\bb}{#2*#3}          
\pgfmathsetmacro{\yy}{\bb*\bb/#4}  
\pgfmathsetmacro{\xx}{#2*sqrt((1-\yy)/#4)}      
\filldraw[ball color=Maroon!10] (0,0) circle[radius=\xx];
\sellipseThreeD(0,0)(\xx cm,.25 cm)  
\begin{scope}[rotate=-90]
\sellipseThreeD(0,0)(\xx cm,.25 cm)  
\end{scope}   
}% 

\newcommand{\parapp}[3]{%
\fill[Maroon!10,opacity=.5] (0,0,0)-- (#1,0,0) -- (#1,#3,0)  -- (0,#3,0) --cycle;
\fill[Maroon!10,opacity=.5] (0,0,#2)-- (#1,0,#2) -- (#1,#3,#2)  -- (0,#3,#2) --cycle;
\fill[Maroon!10,opacity=.5] (0,#3,0)-- (0,#3,#2) -- (#1,#3,#2) -- (#1,#3,0)--cycle;
\fill[Maroon!10,opacity=.5] (0,0,0)-- (0,0,#2) -- (#1,0,#2) -- (#1,0,0)--cycle; 
\draw[] (0,0,#2) -- (#1,0,#2) -- (#1,#3,#2) --(0,#3,#2) --(0,0,#2)
        (#1,0,#2) -- (#1,0,0)  -- (#1,#3,0) --(0,#3,0) -- (0,#3,#2)    
        (#1,#3,#2) -- (#1,#3,0);
\draw[dashed] (0,0,0) -- (0,0,#2) (0,0,0)-- (#1,0,0) (0,0,0)-- (0,#3,0);

}    
\begin{document}    

 \tikz  \tkzCone{3}{0}{5};
  \hspace{1cm}
 \tikz   \tkzCylinder{3}{0}{5};
  \hspace{1cm}   
 \tikz   \tkzTruncatedCone{3}{0}{2};

\vspace{1cm}

\tikz   \parapp{6}{2}{4};      
  \hspace{1cm}
\tikz  \parapp{3}{3}{3} ;

\vspace{1cm}   
\tikz \tkzSphere{2}{0}{2};

\end{document}

答案3

xelatex用隐藏的线和面查看对象。使用或删除单个多边形面运行latex->dvips->ps2pdf rm=...。内部数字可以用numfaces=all

\documentclass[a4paper]{article}
\usepackage{pst-solides3d}  
\begin{document}

\psset{Decran=20,viewpoint=40 20 20 rtp2xyz,lightsrc=viewpoint,action=draw**,
       fillcolor=blue!30!white,linewidth=0.1pt}
\begin{pspicture}(-1.5,-2)(2,2)
\psSolid[object=parallelepiped,a=5,b=6,c=2](0,0,2)
\end{pspicture}
\begin{pspicture}(-1.5,-2)(1.5,2)
\psSolid[object=sphere,r=3,ngrid=20 20](0,0,2)
\end{pspicture}
\begin{pspicture}(-1.5,-2)(1.5,2)
\psSolid[object=cone,r=3,h=5,ngrid=2 20]
\end{pspicture}
\begin{pspicture}(-1.5,-2)(1.5,2)
\psSolid[object=troncconecreux,r0=3,r1=2,h=5,ngrid=2 20,incolor=green!60!white]
\end{pspicture}

\psset{incolor=red!60!white}
\begin{pspicture}(-1.5,-2)(2,2)
\psSolid[object=parallelepiped,a=5,b=6,c=2,rm=0,hollow](0,0,2)
\end{pspicture}
\begin{pspicture}(-1.5,-2)(1.5,2)
\psSolid[object=sphere,r=3,ngrid=20 20,hollow,
     rm=100 101 102 120 121 122 140 141 142](0,0,2)
\end{pspicture}
\begin{pspicture}(-1.5,-2)(1.5,2)
\psSolid[object=cone,r=3,h=5,ngrid=2 20,rm=20 21 22,hollow]
\end{pspicture}
\begin{pspicture}(-1.5,-2)(1.5,2)
\psSolid[object=troncconecreux,r0=3,r1=2,h=5,ngrid=2 20,hollow,rm=20 21 22]
\end{pspicture}

\end{document}

在此处输入图片描述

相关内容