81

Is there like a library that has default geometric figures like the ones shown below. Am not acquainted yet with 3D diagrams in TikZ.

sample

3
  • 2
    Related : Drawing simple 3D cylinders in TikZ
    – percusse
    Commented Jan 30, 2012 at 16:48
  • 2
    Related: tex.stackexchange.com/q/37442/4011
    – student
    Commented Jan 30, 2012 at 17:02
  • Thanks for redirecting me to the questions above. I had slightly read them some while ago and really didnt scrutinize the examples. I do agree that my question is related to the ones mentioned above. Can you help me with the cones and the sphere?
    – azetina
    Commented Jan 30, 2012 at 19:19

3 Answers 3

70

Here you go with the sphere and cone and truncated cone:

\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}

enter image description here


Edit 1: Here are some inprovements inspired by Altermundus' answer, unfortunately still missing a solution to the "bad cone edge problem:

\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}

enter image description here

6
  • Awsome! I will study your code carefully and try my best to learn more about 3D representations. Thanks again.
    – azetina
    Commented Jan 31, 2012 at 16:52
  • Hmm, I#m not so sure, that my answer will help that much with 3D representation in general, as I kind of did the computations to project the 3D onto a 2D plane. probably I will do some "real" 3D later. Commented Jan 31, 2012 at 18:48
  • The samples are perfect. I just wanted the 3D in a 2D plane like you said. But an extra wouldn't hurt...
    – azetina
    Commented Jan 31, 2012 at 19:10
  • @TomBombadil - Could you generalize your example, so that a sphere of any radius can be drawn at any point?
    – TCSGrad
    Commented Dec 11, 2013 at 8:09
  • This example will help me create a sketch of the celestial sphere - very nice. Commented Dec 22, 2015 at 18:13
44

The code is a draft version. I need to remove xkeyval and to use pgfkeys. I need to add some options :

enter image description here

\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
  • 1
    You want to replace xkeyval features with pgfkeys. But does pgfkeys have the equivalent of \presetkeys? In xkeyval, color={} is the same as color=. xkeyval strips up to 3 levels of outer braces in parsing a key.
    – Ahmed Musa
    Commented Jul 6, 2012 at 12:47
  • Nice, especially the sphere, but there are some issues. I posted a question on Meta concerning these. Commented Jul 6, 2012 at 21:04
  • 1
    So we need to copy/paste this amount of code to just have a 3D cylinder in TikZ nowadays still? Could you please comment on the parameters {3}{0}{5} that you are passing?
    – juliohm
    Commented Jan 19, 2019 at 21:03
32

Object viewed with hidden lines and surfaces. Run with xelatex or latex->dvips->ps2pdf rm=... deletes single polygon faces. the internal numbers can be shown with 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}

enter image description here

1
  • if you're using TeXstudio then you can run XeLatex automatically, just need to add this to the very first line (found that tip here): %!TEX TS-program = xelatex Commented Oct 31, 2023 at 15:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .