GeneralHow can I draw a sphere insribes a cylinder?

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
Tran Van Toan
Posts: 23
Joined: Sat Oct 28, 2017 1:18 am

How can I draw a sphere insribes a cylinder?

Post by Tran Van Toan »

I want to draw a picture. I tried.

Code: Select all

\documentclass[12pt,border=3mm]{standalone}
\usepackage{fouriernc}
\usepackage{tikz}
\usepackage{tikz-3dplot}
\usetikzlibrary{calc,backgrounds}
\begin{document}
\tdplotsetmaincoords{60}{110}
\begin{tikzpicture}
[scale=1,tdplot_main_coords]
\path
coordinate (T) at  (0,0,3)
coordinate (A) at  (0,4,0);
\begin{scope}[tdplot_screen_coords, on background layer]
\pgfmathsetmacro{\R}{5}%
\fill[ball color=cyan!50, opacity=1.0] (T) circle (\R);
\end{scope}
\foreach \v/\position in {T/above,A/below} {
    \draw[draw =black, fill=black] (\v) circle (1.2pt) node [\position=0.2mm] {$\v$};
}
\draw[thick, dashed] (T) -- (A) node[midway,right] {$R$};
\end{tikzpicture}
\end{document}
Attachments
ScreenHunter 16.png
ScreenHunter 16.png (41.72 KiB) Viewed 7534 times

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

How can I draw a sphere insribes a cylinder?

Post by Stefan Kottwitz »

Hi,

welcome to the forum!

Your sphere looks already nice. Thanks for posting what you already tried!

Here is a cylinder example, by Tom from here:

Code: Select all

\documentclass{article}
\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) -- (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}
But, I suggest, don't spend your time too much on coding this, if a drawing in Inkscape (for example) could do the same.

But I'm available here for any TikZ question! Just short on time due to a project, but all that's just answering or not too hard to do, I'm ready.

Stefan
LaTeX.org admin
Tran Van Toan
Posts: 23
Joined: Sat Oct 28, 2017 1:18 am

How can I draw a sphere insribes a cylinder?

Post by Tran Van Toan »

Thank you very much for your answer. I based on your link, and tried.

Code: Select all

\documentclass[border=5]{standalone}
\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) -- (2,4)  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,4) circle (2cm and 0.5cm);
  \draw (-2,4) -- (-2,0) arc (180:360:2cm and 0.5cm)
               -- (2,4) ++ (-2,0) circle (2cm and 0.5cm);
  \draw[densely dashed] (-2,0) arc (180:0:2cm and 0.5cm);
  \fill[thick,ball color=red!25] (0,2) circle (2);
\end{tikzpicture}
\end{document}
Attachments
ScreenHunter 17.png
ScreenHunter 17.png (101.5 KiB) Viewed 7528 times
User avatar
Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

How can I draw a sphere insribes a cylinder?

Post by Stefan Kottwitz »

Just add an opacity option to the ball too:

\fill[thick, ball color=red!25, opacity = 0.5] (0,2) circle (2);
sphere-cylinder.png
sphere-cylinder.png (30.91 KiB) Viewed 7527 times
Stefan
LaTeX.org admin
Tran Van Toan
Posts: 23
Joined: Sat Oct 28, 2017 1:18 am

How can I draw a sphere insribes a cylinder?

Post by Tran Van Toan »

Thank you very much.
Tran Van Toan
Posts: 23
Joined: Sat Oct 28, 2017 1:18 am

How can I draw a sphere insribes a cylinder?

Post by Tran Van Toan »

Three spheres inside a cylinder.

Code: Select all

\documentclass[border=5]{standalone}
\usepackage{tikz}
\usepackage{tkz-euclide}
\usetkzobj{all}
\usetikzlibrary{shadings}
\begin{document}
\begin{tikzpicture}
\def\R{2}
  \fill[top color    = gray!50!black,
        bottom color = gray!10,
        middle color = gray,
        shading      = axis,
        opacity      = 0.25]
    (0,0) circle (\R cm and 0.5cm);
  \fill[left color   = gray!50!black,
        right color  = gray!50!black,
        middle color = gray!50,
        shading      = axis,
        opacity      = 0.25]
    (\R,0) -- (\R,6*\R)  arc (360:180:\R cm and 0.5cm)
          -- (-\R,0) arc (180:360:\R cm and 0.5cm);
  \fill[top color    = gray!90!,
        bottom color = gray!2,
        middle color = gray!30,
        shading      = axis,
        opacity      = 0.25]
    (0,6*\R) circle (\R cm and 0.5cm);
  \draw (-\R,6*\R) -- (-\R,0) arc (180:360:\R cm and 0.5cm)
               -- (\R,6*\R) ++ (-\R,0) circle (\R cm and 0.5cm);
  \draw[densely dashed] (-\R,0) arc (180:0:\R cm and 0.5cm);
 \fill[thick, ball color=red!90, opacity = 0.5] (0,\R) circle (\R);
\fill[thick, ball color=orange!90, opacity = 0.5] (0,3*\R) circle (\R);
\fill[thick, ball color=blue!90, opacity = 0.5] (0,5*\R) circle (\R);
 \end{tikzpicture}
\end{document}
Attachments
ScreenHunter 21.png
ScreenHunter 21.png (60.75 KiB) Viewed 7513 times
User avatar
Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

How can I draw a sphere insribes a cylinder?

Post by Stefan Kottwitz »

Looks pretty good now!

Stefan
LaTeX.org admin
Tran Van Toan
Posts: 23
Joined: Sat Oct 28, 2017 1:18 am

How can I draw a sphere insribes a cylinder?

Post by Tran Van Toan »

How can I arrangle three balls like this picture?
Attachments
ScreenHunter 41.png
ScreenHunter 41.png (84.32 KiB) Viewed 7475 times
User avatar
Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

How can I draw a sphere insribes a cylinder?

Post by Stefan Kottwitz »

In your example above, I used a smaller y position the the three ball lines near the end, and removed the opacity, and adjusted the cylinder height to 5.6:

Code: Select all

\documentclass[border=5]{standalone}
\usepackage{tikz}
\usepackage{tkz-euclide}
\usetkzobj{all}
\usetikzlibrary{shadings}
\begin{document}
\begin{tikzpicture}
\def\R{2}
  \fill[top color    = gray!50!black,
        bottom color = gray!10,
        middle color = gray,
        shading      = axis,
        opacity      = 0.25]
    (0,0) circle (\R cm and 0.5cm);
  \fill[left color   = gray!50!black,
        right color  = gray!50!black,
        middle color = gray!50,
        shading      = axis,
        opacity      = 0.25]
    (\R,0) -- (\R,5.6*\R)  arc (360:180:\R cm and 0.5cm)
          -- (-\R,0) arc (180:360:\R cm and 0.5cm);
  \fill[top color    = gray!90!,
        bottom color = gray!2,
        middle color = gray!30,
        shading      = axis,
        opacity      = 0.25]
    (0,5.6*\R) circle (\R cm and 0.5cm);
  \draw (-\R,5.6*\R) -- (-\R,0) arc (180:360:\R cm and 0.5cm)
               -- (\R,5.6*\R) ++ (-\R,0) circle (\R cm and 0.5cm);
  \draw[densely dashed] (-\R,0) arc (180:0:\R cm and 0.5cm);
 \fill[thick, ball color=red!90] (0,\R) circle (\R);
 \fill[thick, ball color=orange!90] (0,2.8*\R) circle (\R);
 \fill[thick, ball color=blue!90] (0,4.6*\R) circle (\R);
 \end{tikzpicture}
\end{document}
So it became:
cylinder-balls.png
cylinder-balls.png (55.6 KiB) Viewed 7450 times
Stefan
LaTeX.org admin
Tran Van Toan
Posts: 23
Joined: Sat Oct 28, 2017 1:18 am

How can I draw a sphere insribes a cylinder?

Post by Tran Van Toan »

Thank you very much.
Post Reply