LaTeX forum ⇒ GeneralHow can I draw a sphere insribes a cylinder?

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

How can I draw a sphere insribes a cylinder?

Postby Tran Van Toan » Sat Oct 28, 2017 1:43 am

I want to draw a picture. I tried.

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

Tags:

User avatar
Stefan Kottwitz
Site Admin
Posts: 8541
Joined: Mon Mar 10, 2008 9:44 pm
Location: Hamburg, Germany
Contact:

Postby Stefan Kottwitz » Sat Oct 28, 2017 2:17 am

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:

  1. \documentclass{article}
  2. \usepackage[margin=15mm]{geometry}
  3. \usepackage{tikz}
  4. \usetikzlibrary{shadings}
  5. \begin{document}
  6. \begin{tikzpicture}
  7. \fill[top color = gray!50!black,
  8. bottom color = gray!10,
  9. middle color = gray,
  10. shading = axis,
  11. opacity = 0.25]
  12. (0,0) circle (2cm and 0.5cm);
  13. \fill[left color = gray!50!black,
  14. right color = gray!50!black,
  15. middle color = gray!50,
  16. shading = axis,
  17. opacity = 0.25]
  18. (2,0) -- (2,6) arc (360:180:2cm and 0.5cm)
  19. -- (-2,0) arc (180:360:2cm and 0.5cm);
  20. \fill[top color = gray!90!,
  21. bottom color = gray!2,
  22. middle color = gray!30,
  23. shading = axis,
  24. opacity = 0.25]
  25. (0,6) circle (2cm and 0.5cm);
  26. \draw (-2,6) -- (-2,0) arc (180:360:2cm and 0.5cm)
  27. -- (2,6) ++ (-2,0) circle (2cm and 0.5cm);
  28. \draw[densely dashed] (-2,0) arc (180:0:2cm and 0.5cm);
  29. \end{tikzpicture}
  30. \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
Site admin

Tran Van Toan
Posts: 17
Joined: Sat Oct 28, 2017 1:18 am

Postby Tran Van Toan » Sat Oct 28, 2017 2:55 am

Thank you very much for your answer. I based on your link, and tried.
  1. \documentclass[border=5]{standalone}
  2. \usepackage{tikz}
  3. \usetikzlibrary{shadings}
  4. \begin{document}
  5. \begin{tikzpicture}
  6. \fill[top color = gray!50!black,
  7. bottom color = gray!10,
  8. middle color = gray,
  9. shading = axis,
  10. opacity = 0.25]
  11. (0,0) circle (2cm and 0.5cm);
  12. \fill[left color = gray!50!black,
  13. right color = gray!50!black,
  14. middle color = gray!50,
  15. shading = axis,
  16. opacity = 0.25]
  17. (2,0) -- (2,4) arc (360:180:2cm and 0.5cm)
  18. -- (-2,0) arc (180:360:2cm and 0.5cm);
  19. \fill[top color = gray!90!,
  20. bottom color = gray!2,
  21. middle color = gray!30,
  22. shading = axis,
  23. opacity = 0.25]
  24. (0,4) circle (2cm and 0.5cm);
  25. \draw (-2,4) -- (-2,0) arc (180:360:2cm and 0.5cm)
  26. -- (2,4) ++ (-2,0) circle (2cm and 0.5cm);
  27. \draw[densely dashed] (-2,0) arc (180:0:2cm and 0.5cm);
  28. \fill[thick,ball color=red!25] (0,2) circle (2);
  29. \end{tikzpicture}
  30. \end{document}
Attachments
ScreenHunter 17.png
ScreenHunter 17.png (101.5 KiB) Viewed 270 times

User avatar
Stefan Kottwitz
Site Admin
Posts: 8541
Joined: Mon Mar 10, 2008 9:44 pm
Location: Hamburg, Germany
Contact:

Postby Stefan Kottwitz » Sat Oct 28, 2017 3:35 am

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 269 times


Stefan
Site admin

Tran Van Toan
Posts: 17
Joined: Sat Oct 28, 2017 1:18 am

Postby Tran Van Toan » Sat Oct 28, 2017 5:26 am

Thank you very much.

Tran Van Toan
Posts: 17
Joined: Sat Oct 28, 2017 1:18 am

Postby Tran Van Toan » Sat Oct 28, 2017 10:57 am

Three spheres inside a cylinder.
  1. \documentclass[border=5]{standalone}
  2. \usepackage{tikz}
  3. \usepackage{tkz-euclide}
  4. \usetkzobj{all}
  5. \usetikzlibrary{shadings}
  6. \begin{document}
  7. \begin{tikzpicture}
  8. \def\R{2}
  9. \fill[top color = gray!50!black,
  10. bottom color = gray!10,
  11. middle color = gray,
  12. shading = axis,
  13. opacity = 0.25]
  14. (0,0) circle (\R cm and 0.5cm);
  15. \fill[left color = gray!50!black,
  16. right color = gray!50!black,
  17. middle color = gray!50,
  18. shading = axis,
  19. opacity = 0.25]
  20. (\R,0) -- (\R,6*\R) arc (360:180:\R cm and 0.5cm)
  21. -- (-\R,0) arc (180:360:\R cm and 0.5cm);
  22. \fill[top color = gray!90!,
  23. bottom color = gray!2,
  24. middle color = gray!30,
  25. shading = axis,
  26. opacity = 0.25]
  27. (0,6*\R) circle (\R cm and 0.5cm);
  28. \draw (-\R,6*\R) -- (-\R,0) arc (180:360:\R cm and 0.5cm)
  29. -- (\R,6*\R) ++ (-\R,0) circle (\R cm and 0.5cm);
  30. \draw[densely dashed] (-\R,0) arc (180:0:\R cm and 0.5cm);
  31. \fill[thick, ball color=red!90, opacity = 0.5] (0,\R) circle (\R);
  32. \fill[thick, ball color=orange!90, opacity = 0.5] (0,3*\R) circle (\R);
  33. \fill[thick, ball color=blue!90, opacity = 0.5] (0,5*\R) circle (\R);
  34. \end{tikzpicture}
  35. \end{document}
Attachments
ScreenHunter 21.png
ScreenHunter 21.png (60.75 KiB) Viewed 255 times

User avatar
Stefan Kottwitz
Site Admin
Posts: 8541
Joined: Mon Mar 10, 2008 9:44 pm
Location: Hamburg, Germany
Contact:

Postby Stefan Kottwitz » Sat Oct 28, 2017 4:17 pm

Looks pretty good now!

Stefan
Site admin

Tran Van Toan
Posts: 17
Joined: Sat Oct 28, 2017 1:18 am

Postby Tran Van Toan » Mon Nov 06, 2017 2:44 am

How can I arrangle three balls like this picture?
Attachments
ScreenHunter 41.png
ScreenHunter 41.png (84.32 KiB) Viewed 217 times

User avatar
Stefan Kottwitz
Site Admin
Posts: 8541
Joined: Mon Mar 10, 2008 9:44 pm
Location: Hamburg, Germany
Contact:

Postby Stefan Kottwitz » Mon Nov 06, 2017 7:45 pm

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:

  1. \documentclass[border=5]{standalone}
  2. \usepackage{tikz}
  3. \usepackage{tkz-euclide}
  4. \usetkzobj{all}
  5. \usetikzlibrary{shadings}
  6. \begin{document}
  7. \begin{tikzpicture}
  8. \def\R{2}
  9. \fill[top color = gray!50!black,
  10. bottom color = gray!10,
  11. middle color = gray,
  12. shading = axis,
  13. opacity = 0.25]
  14. (0,0) circle (\R cm and 0.5cm);
  15. \fill[left color = gray!50!black,
  16. right color = gray!50!black,
  17. middle color = gray!50,
  18. shading = axis,
  19. opacity = 0.25]
  20. (\R,0) -- (\R,5.6*\R) arc (360:180:\R cm and 0.5cm)
  21. -- (-\R,0) arc (180:360:\R cm and 0.5cm);
  22. \fill[top color = gray!90!,
  23. bottom color = gray!2,
  24. middle color = gray!30,
  25. shading = axis,
  26. opacity = 0.25]
  27. (0,5.6*\R) circle (\R cm and 0.5cm);
  28. \draw (-\R,5.6*\R) -- (-\R,0) arc (180:360:\R cm and 0.5cm)
  29. -- (\R,5.6*\R) ++ (-\R,0) circle (\R cm and 0.5cm);
  30. \draw[densely dashed] (-\R,0) arc (180:0:\R cm and 0.5cm);
  31. \fill[thick, ball color=red!90] (0,\R) circle (\R);
  32. \fill[thick, ball color=orange!90] (0,2.8*\R) circle (\R);
  33. \fill[thick, ball color=blue!90] (0,4.6*\R) circle (\R);
  34. \end{tikzpicture}
  35. \end{document}


So it became:

cylinder-balls.png
cylinder-balls.png (55.6 KiB) Viewed 192 times


Stefan
Site admin

Tran Van Toan
Posts: 17
Joined: Sat Oct 28, 2017 1:18 am

Postby Tran Van Toan » Tue Nov 07, 2017 6:40 am

Thank you very much.


Return to “General”

Who is online

Users browsing this forum: No registered users and 11 guests