10

I've got a slight problem with the way TikZ draws my 3D figure. The edges of the top and bottom of the cylinder in the picture don't seem to match correctly even though I don't think I made a mistake. Does anybody know why it does this or is there a better way to draw this? (still quite new to TikZ and 3D drawing)

    \documentclass[11pt,a4paper,onesided]{report}

\usepackage[ngerman]{babel}



\usepackage{amsmath,amsfonts,amssymb,amsthm}
\usepackage{pgfplots}


\usetikzlibrary{calc,fit,patterns,decorations.markings,matrix,3d}
\begin{document}

    \begin{tikzpicture}[scale=1]
    \draw[thin,->] (-2,0) -- (5,0);
    \draw[thin,->] (0,-2) -- (0,3);

    %Zylinder
    \draw[] (1.5,-1.5) -- (1.5,1.5);
    \draw[] (3.5,-1.5) -- (3.5,1.5);
    \begin{scope}[canvas is zx plane at y=0]
    \draw (0,2.5) circle (2cm);
    \draw[fill=gray,opacity=0.5] (0,2.5) circle (1cm);
    %
    \draw[->] (-2,0) -- (3,0);
    \end{scope}

    \begin{scope}[canvas is zx plane at y=1.5]
    \draw (0,2.5) circle (1cm);
    \end{scope}

    \begin{scope}[canvas is zx plane at y=-1.5]
    \draw (0,2.5) circle (1cm);
    \end{scope}     

    \begin{scope}[every node/.append style={
        xslant=1,sloped}
    ]    
    \node at (2.4,-.2) {\scalebox{1}[.7]{$T_0$}};
    \node at (4,.4) {\scalebox{1}[.7]{$T$}};
    \node at (-.8,-.2) {\scalebox{1}[.7]{$\mathbb{R}^n$}};
    \end{scope}


    \end{tikzpicture}

\end{document}

The result:

enter image description here

Gonzalo Medina
  • 505,128
Michael A
  • 593

2 Answers2

9

Here's an option producing a more pleasing result:

enter image description here

The code:

\documentclass{article}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{tikz}
\usetikzlibrary{calc,fit,patterns,decorations.markings,matrix,3d}

\begin{document}

\begin{tikzpicture}[scale=1]
\def\Anglei{-66}

\draw[thin,->] (-2,0) -- (5,0);
\draw[thin,->] (0,-2) -- (0,3);

%Zylinder
\begin{scope}[canvas is zx plane at y=0]
\draw (0,2.5) circle (2cm);
\draw[fill=gray,opacity=0.5] (0,2.5) circle (1cm);
\draw[->] (-2,0) -- (3,0);
\end{scope}

\begin{scope}[canvas is zx plane at y=1.5]
\path (0,2.5) circle (1cm);
\coordinate (circ1a) at ( $ (0,2.5) + (\Anglei:1cm) $ );
\coordinate (circ1b) at ( $ (0,2.5) + (180+\Anglei:1cm) $ );
\end{scope}

\begin{scope}[canvas is zx plane at y=-1.5]
\path (0,2.5) circle (1cm);
\coordinate (circ2a) at ( $ (0,2.5) + (\Anglei:1cm) $ );
\coordinate (circ2b) at ( $ (0,2.5) + (180+\Anglei:1cm) $ );
\end{scope} 

\begin{scope}[canvas is xy plane at z=0]
\draw (circ1a) -- (circ2a);
\draw (circ1b) -- (circ2b);
\end{scope}

\begin{scope}[canvas is zx plane at y=1.5]
\draw (0,2.5) circle (1cm);
\end{scope}

\begin{scope}[canvas is zx plane at y=-1.5]
\draw (0,2.5) circle (1cm);
\end{scope} 

\begin{scope}[every node/.append style={
xslant=1,sloped}
]
\node at (2.4,-.2) {\scalebox{1}[.7]{$T_0$}};
\node at (4,.4) {\scalebox{1}[.7]{$T$}};
\node at (-.8,-.2) {\scalebox{1}[.7]{$\mathbb{R}^n$}};
\end{scope}
\end{tikzpicture}

\end{document}

The idea is to place some auxiliary coordinates in the appropriate diameter on the circles; for this, the appropriate angle is required and, as Heiko Oberdiek mentions in his comment, since the slanted axis is rotated by 45°, this angle is is (90° + 45°)/2 = 67.5° (modulo the sign).

\def\Anglei{-67.5}

\begin{scope}[canvas is zx plane at y=1.5]
\path (0,2.5) circle (1cm);
\coordinate (circ1a) at ( $ (0,2.5) + (\Anglei:1cm) $ );
\coordinate (circ1b) at ( $ (0,2.5) + (180+\Anglei:1cm) $ );
\end{scope}

\begin{scope}[canvas is zx plane at y=-1.5]
\path (0,2.5) circle (1cm);
\coordinate (circ2a) at ( $ (0,2.5) + (\Anglei:1cm) $ );
\coordinate (circ2b) at ( $ (0,2.5) + (180+\Anglei:1cm) $ );
\end{scope} 

Perhaps dashing some arcs might also be a good idea:

\documentclass{article}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{tikz}
\usetikzlibrary{calc,fit,patterns,decorations.markings,matrix,3d}

\begin{document}

\begin{tikzpicture}[scale=1]
\def\Anglei{-67.5}
\def\Angleii{233}
\def\Angleiii{170}

\draw[thin,->] (-2,0) -- (5,0);
\draw[thin,->] (0,-2) -- (0,3);

%Zylinder
\begin{scope}[canvas is zx plane at y=0]
\coordinate (circmb) at ( $ (0,2.5) + (\Angleii:2cm) $ );
\coordinate (circma) at ( $ (0,2.5) + (\Angleiii:2cm) $ );
\draw[dashed] (circmb) arc [start angle=\Angleii,end angle=\Angleiii,radius=2cm];
\draw (circma) arc [start angle=\Angleiii,end angle=\Angleii-360,radius=2cm];
\fill[fill=gray,opacity=0.5] (0,2.5) circle (1cm);

\coordinate (circm1) at ( $ (0,2.5) + (\Anglei:1cm) $ );
\coordinate (circm2) at ( $ (0,2.5) + (180+\Anglei:1cm) $ );

\draw (circm1) arc [start angle=\Anglei,end angle=180+\Anglei,radius=1cm];
\draw[dashed] (circm2) arc [start angle=180+\Anglei,end angle=360+\Anglei,radius=1cm];

\draw[->] (-2,0) -- (3,0);
\end{scope}

\begin{scope}[canvas is zx plane at y=1.5]
\path (0,2.5) circle (1cm);
\coordinate (circ1a) at ( $ (0,2.5) + (\Anglei:1cm) $ );
\coordinate (circ1b) at ( $ (0,2.5) + (180+\Anglei:1cm) $ );
\end{scope}

\begin{scope}[canvas is zx plane at y=-1.5]
\path (0,2.5) circle (1cm);
\coordinate (circ2a) at ( $ (0,2.5) + (\Anglei:1cm) $ );
\coordinate (circ2b) at ( $ (0,2.5) + (180+\Anglei:1cm) $ );
\end{scope} 

\begin{scope}[canvas is xy plane at z=0]
\draw (circ1a) -- (circ2a);
\draw (circ1b) -- (circ2b);
\end{scope}

\begin{scope}[canvas is zx plane at y=1.5]
\draw (0,2.5) circle (1cm);
\end{scope}

\begin{scope}[canvas is zx plane at y=-1.5]
\draw (circ2a) arc [start angle=\Anglei,end angle=180+\Anglei,radius=1cm];
\draw[dashed] (circ2b) arc [start angle=180+\Anglei,end angle=360+\Anglei,radius=1cm];
\end{scope} 

\begin{scope}[every node/.append style={
xslant=1,sloped}
]
\node at (2.4,-.2) {\scalebox{1}[.7]{$T_0$}};
\node at (4,.4) {\scalebox{1}[.7]{$T$}};
\node at (-.8,-.2) {\scalebox{1}[.7]{$\mathbb{R}^n$}};
\end{scope}
\end{tikzpicture}

\end{document}

enter image description here

Gonzalo Medina
  • 505,128
3

You didn’t make a mistake: that’s exactly what you’d get if you took two hoops and connected rods at the diameter. Here’s what it looks like when you draw two more lines in between:

Cylinder with four guidelines

You might also consider using pgfplots to draw the cylinder as a surface or wireframe. You can also move the lines so that they’re at (cos t, sin t) in the zx-plane for some more visually-pleasing value of t than 0 and π.

\PassOptionsToPackage{svgnames}{xcolor}
\documentclass{standalone}
\usepackage{iftex}

\ifPDFTeX
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amssymb}
\else
\usepackage{amsmath}
\usepackage{unicode-math}
\setmathfont{Latin Modern Math}
\fi

\usepackage{pgfplots}
\pgfplotsset{width=\textwidth,compat=1.12}
\usetikzlibrary{calc,fit,patterns,decorations.markings,matrix,3d}

\begin{document}

    \begin{tikzpicture}[scale=1]
    \draw[thin,->] (-2,0) -- (5,0);
    \draw[thin,->] (0,-2) -- (0,3);

    %Zylinder
    \begin{scope}[canvas is xy plane at z=0]
    \draw[color=DarkGray] (1.5,-1.5) -- (1.5,1.5);
    \draw[color=DarkGray] (3.5,-1.5) -- (3.5,1.5);
    \end{scope}
    \begin{scope}[canvas is xy plane at z=sqrt(3)/2]
    \draw[color=DarkGray] (2,-1.5) -- (2,1.5);
    \draw[color=DarkGray] (3,-1.5) -- (3,1.5);
    \end{scope}
    \begin{scope}[canvas is zx plane at y=0]
    \draw (0,2.5) circle (2cm);
    \draw[fill=gray,opacity=0.5] (0,2.5) circle (1cm);
    %
    \draw[->] (-2,0) -- (3,0);
    \end{scope}

    \begin{scope}[canvas is zx plane at y=1.5]
    \draw (0,2.5) circle (1cm);
    \end{scope}

    \begin{scope}[canvas is zx plane at y=-1.5]
    \draw (0,2.5) circle (1cm);
    \end{scope}     

    \begin{scope}[every node/.append style={
        xslant=1,sloped}
    ]    
    \node at (2.4,-.2) {\scalebox{1}[.7]{$T_0$}};
    \node at (4,.4) {\scalebox{1}[.7]{$T$}};
    \node at (-.8,-.2) {\scalebox{1}[.7]{$\mathbb{R}^n$}};
    \end{scope}
    \end{tikzpicture}

\end{document}
Davislor
  • 44,045
  • Thanks for your help! Could you elaborate a bit more on what exactly you mean with drawing the cylinder as a wireframe? I cant find anything more specific on this. Also, what exactly are these more appealing values you mean? your code creates almost the same image. – Michael A Sep 12 '15 at 14:03
  • I unfortunately am without a working computer at the moment, but here is an example: http://tex.stackexchange.com/questions/265983/drawing-intersection-of-plane-and-parabolic-surface-in-pgfplots/266020#266020 – Davislor Sep 13 '15 at 02:51