7

How to draw a simple cone with height and radius with TikZ?

In the above thread, a cone was created. However, if I invert the cone, the lines from the origin to the cone edges cuts through the cone. This is not visually pleasing. How can I get the line to only intersect the (\pm 2, 3) without going through ellipse opening?

\documentclass[tikz]{standalone}
\usetikzlibrary{calc}
\usetikzlibrary{intersections}
\usetikzlibrary{shadings}

\begin{document}
\begin{tikzpicture}
  \coordinate (h) at (0, 3);
  \coordinate (O) at (0, 0);

  \draw (O) -- +(0, 4);
  \draw (O) -- +(3, 0);
  \draw (O) -- +(-3, 0);

  \fill[
  top color = gray!50,
  bottom color = gray!10,
  shading = axis,
  opacity = 0.25
  ]
  (h) ellipse[x radius = 2, y radius = 0.5];
  \fill[
  left color = gray!50!black,
  right color = gray!50!black,
  middle color = gray!50,
  shading = axis,
  opacity = 0.25
  ]
  (2, 3) -- (O) -- (-2, 3) arc[x radius = 2, y radius = .5, start angle = 180,
  end angle = 360];

  \draw (-2, 3) arc[x radius = 2, y radius = .5, start angle = 180,
  end angle = 360] -- (O) -- cycle;
  \draw[dashed] (-2, 3) arc[x radius = 2, y radius = .5, start angle = 180,
  end angle = 0];
\end{tikzpicture} 
\end{document}

enter image description here

12

I suppose what you want to do is find a line starting at the tip of the cone that is tangential to the ellipse.

So let's do the math. Let r1 be the horizontal radius of the ellipse, and let r2 be the vertical radius. Let z be the distance from the tip of the cone to the midpoint of the ellipse. We have the following situation:

enter image description here

Here, O is the origin, P is the tip of the cone, S is the point at which the line is tangential to the ellipse, alpha is the angle between the horizontal axis and the line segment OS, and the vector v is a normal vector of the ellipse at point S.

We want the vector PS to be perpendicular to the vector v. I've used LaTeX for the remainder of the math: (Unfortunately tex.stackexchange.com does not have LaTeX formulas enabled.)

enter image description here

Note that, interestingly, the required angle does not depend on r2! Curiously, the required angle is the angle of a triangle whose diagonal side has length z, and whose side opposite to the angle has length r2. There must be a simpler proof for this (using congruent triangles?). But it is already way after bedtime, so I'll leave that as an exercise :)

In your particular case, you have r2 = 0.5 and z = 3. This gives alpha = 9.594 degrees. This means you should draw a line from the tip of the cone to the point (pm 2 * cos(9.594), 3 - 0.5 * sin(9.594)) = (1.972, 2.91666), instead of to (2, 3) as you are doing right now.

This code should get you going:

\documentclass[tikz]{standalone}

\usetikzlibrary{calc}

\begin{document}
\begin{tikzpicture}
  \def\rx{2}    % horizontal radius of the ellipse
  \def\ry{0.5}  % vertical radius of the ellipse
  \def\z{3}     % distance from center of ellipse to origin

  \pgfmathparse{asin(\ry/\z)}
  \let\angle\pgfmathresult

  \coordinate (h) at (0, \z);
  \coordinate (O) at (0, 0);     
  \coordinate (A) at ({-\rx*cos(\angle)}, {\z-\ry*sin(\angle)});
  \coordinate (B) at ({\rx*cos(\angle)}, {\z-\ry*sin(\angle)});

  \draw[fill=gray!50] (A) -- (O) -- (B) -- cycle;
  \draw[fill=gray!30] (h) ellipse ({\rx} and {\ry});
\end{tikzpicture} 
\end{document}

A number of example outputs for different values of rx ,ry, and z.

enter image description here

  • (1.972, 2.8333) this point doesn't make contact with my ellipse. Can post your latex code for your diagram? – dustin Apr 15 '14 at 17:13
  • just note that \pgfmathparse{asin(\ry/\z)}\let\angle\pgfmathresult can be replaced by \pgfmathsetmacro{\angle}{asin(\b/\h) – Black Mild Sep 21 '18 at 18:09
3

There is another solution to the problem using package intersections. It can be easily shown that the vertical position of the cone tip y' and vertical position of tangent point on ellipse y0 are easily connected to the vertical radius ry of ellipse as

y' * y0 = ry * ry

enter image description here

So, all you have to do is draw ellipse, the appropriate (red) line and find intersections as in the example below

\documentclass[tikz]{standalone}

\usetikzlibrary{intersections}

\begin{document}
\begin{tikzpicture}
\def\rx{2}    % horizontal radius of the ellipse
\def\ry{0.5}  % vertical radius of the ellipse
\def\z{3}     % distance from center of ellipse to origin

\path[name path=ellipse] (0,\z) ellipse ({\rx} and {\ry});
\path[name path=horizontal] (-\rx,\z-\ry*\ry/\z) -- (\rx,\z-\ry*\ry/\z);
\path [name intersections={of = ellipse and horizontal}];

\draw[fill=gray!50] (intersection-1) -- (0,0) -- (intersection-2) -- cycle;
\draw[fill=gray!30] (0,\z) ellipse ({\rx} and {\ry}); 

\end{tikzpicture} 
\end{document}

to get this

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.