7

How to create custom TikZ node "cylinder with bottom", where hidden line in cylinder bottom is shown using dashed line.

Something like the following creation, except node center should be in the middle of cylinder (as if it was in the middle of the center of 3D cylinder), not in center of the side -- it looks imbalanced

https://www.writelatex.com/1149537sksqzs#/2737083/

\documentclass[tikz]{standalone}

\usepackage{tikz}
\usetikzlibrary{shapes.geometric,calc}

\begin{document}
\begin{tikzpicture}
  \node[cylinder,draw=black,thick,aspect=0.7,
        minimum height=1.7cm,minimum width=1.5cm,
        shape border rotate=90,
        cylinder uses custom fill,
        cylinder body fill=red!30,
        cylinder end  fill=red!10]
   (A) {A};
  \draw[dashed]
    let \p1 = ($ (A.after bottom) - (A.before bottom) $),
        \n1 = {0.5*veclen(\x1,\y1)},
        \p2 = ($ (A.bottom) - (A.after bottom)!.5!(A.before bottom) $),
        \n2 = {veclen(\x2,\y2)}
  in
    (A.before bottom) arc [start angle=0, end angle=180,
    x radius=\n1, y radius=\n2];
\end{tikzpicture}
\end{document}

3D cylinder with bottom

1
  • 2
    It seams to be a problem of line width. Try x radius=\n1 - \pgflinewidth ?
    – Tarass
    Commented Jun 3, 2014 at 17:58

1 Answer 1

9

As Tarass correctly guessed, it is a problem of line width, but it affects both to the arc radius and to the initial point (A.before bottom). This is a possible fix:

\documentclass[tikz]{standalone}

\usepackage{tikz}
\usetikzlibrary{shapes.geometric,calc}

\title{TikZ: cylinder with bottom - example}
\begin{document}
\begin{tikzpicture}
  \node[cylinder,draw=black,thick,aspect=0.7,minimum height=1.7cm,minimum width=1.5cm,shape border rotate=90,cylinder uses custom fill, cylinder body fill=red!30,cylinder end fill=red!10] (A) {A};
  \draw[dashed]
    let \p1 = ($ (A.after bottom) - (A.before bottom) $),
        \n1 = {0.5*veclen(\x1,\y1)-\pgflinewidth},
        \p2 = ($ (A.bottom) - (A.after bottom)!.5!(A.before bottom) $),
        \n2 = {veclen(\x2,\y2)-\pgflinewidth}
  in
    ([xshift=-\pgflinewidth] A.before bottom) arc [start angle=0, end angle=180,
    x radius=\n1, y radius=\n2];
\end{tikzpicture}
\end{document}

enter image description here

2
  • Good shot, I haven't a tex box to try it.
    – Tarass
    Commented Jun 3, 2014 at 19:17
  • 1
    You should replace -\pgflinewidth with -0.5\pgflinewidth everywhere.
    – Nico
    Commented Dec 22, 2016 at 13:05

You must log in to answer this question.

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