如何在圆柱体内画一些球体?

如何在圆柱体内画一些球体?

我尝试画三个球体内接一个圆柱体。我的代码

\documentclass[border=5]{standalone}
\usepackage{tikz}
\usepackage{tkz-euclide}
\usetkzobj{all}
\usetikzlibrary{shadings}
\begin{document}
\begin{tikzpicture}
\def\R{1}
  \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} 

如果我想画一些球体:5、6、......,我该如何修复我的代码?

在此处输入图片描述

答案1

这里有一个使用图层的技巧。首先绘制球,球的数量由 参数中的颜色数量决定\foreach。为每个球的顶部定义一个坐标,由于它每次都有相同的名称,并且球是从下到上绘制的,因此可用于获取圆柱体的高度。然后在背景库定义的图层上绘制圆柱体的底部和后部,background最后绘制前部。等等:

\documentclass[tikz,border=5]{standalone}
\usetikzlibrary{backgrounds}
\def\R{1}
\begin{document}
\begin{tikzpicture}
\foreach \c [count=\y] in {red, yellow, pink, green, orange, purple, blue}
  \shade [shading=ball, ball color=\c!90, opacity=.5] 
    (0,\y*\R-\R/2) circle [radius=\R/2] (0,\y*\R) coordinate (top-ball);
\begin{scope}[x=(0:\R/2), y=(90:\R/4)]
\begin{pgfonlayer}{background}
\fill [gray!50] (0,0) ellipse [x radius=1, y radius=1];
\draw [dashed] (-1,0) arc (180:0:1);
\fill [gray!25] (-1, 0) arc (180:0:1) -- (top-ball -| 1, 0) arc (0:180:1) -- cycle;
\end{pgfonlayer}
\draw (top-ball -| 1, 0) arc (0:180:1);
\shade [draw, left color=gray, right color=gray, middle color=gray!50,
  fill opacity=0.25]
  (-1, 0) arc (180:360:1) -- (top-ball -| 1, 0) arc (360:180:1) -- cycle;
\end{scope}
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

我使用 Asymptote 添加了一个解决方案。代码是自动的,因为您可以选择球的数量以及它们的颜色(应使用无透明度opacity(1))。覆盖圆柱体具有所需的不透明度。

在此处输入图片描述

unitsize(1cm);
import solids;

currentprojection=orthographic(1,0,.2,zoom=.95);
//currentlight=light(lightblue);

revolution s=sphere(O,1);
surface ball=surface(s);

pen[] m={red,blue,green,purple};
m.cyclic=true;
int n=7;     // the number of the balls
for(int i=0; i<n; ++i)
draw(shift(0,0,2*i)*ball,m[i]+opacity(1));

real h=2*n;    // the height of the covering cylinder
revolution c=cylinder(O,1,h,Z);
draw(shift(0,0,-1)*surface(c),yellow+opacity(.3));

答案3

以下是一种更通用的方法的尝试元帖子. 将变量设置N为您想要“管”中的球的数量。

在此处输入图片描述

prologues := 3;
outputtemplate := "%j%c.eps";

vardef ball(expr r, light_angle, base_shade, white_shade, rim_shade) = 
    save c; path c, c'; 
    c = fullcircle scaled 2r;
    c' = fullcircle scaled 2 shifted 1/2(r,0) rotated light_angle;
    image(
        for s=0 step 1/64 until 1:
            fill interpath(s, c, c') withcolor s[base_shade, white_shade];
        endfor
        draw c withcolor rim_shade;
    )
enddef;

beginfig(1);

    color shade[];
    shade1 = (0.49804,0.78824,0.49804);
    shade2 = (0.7451,0.68235,0.83137);
    shade3 = (0.99216,0.75294,0.52549);
    shade4 = (1,1,0.6);
    shade5 = (0.21961,0.42353,0.6902);

    color glass[];
    glass1 = (0.588, 0.588, 0.447) * 1.2;
    glass2 = (0.644, 0.644, 0.620);
    glass3 = (0.844, 0.844, 0.820);

    numeric r, N;
    r = 42;
    N = 5;

    path base, vent;
    base = fullcircle xscaled 2r yscaled 2/3r;
    vent = base shifted (0,2N*r);

    fill base withcolor glass1;
    fill subpath (0,4) of base -- subpath (4,8) of vent -- cycle withcolor glass2;
    fill vent withcolor glass3;

    draw subpath (0,4) of base dashed withdots scaled 1/2;
    draw subpath (4,8) of base;
    for i=1 upto N:
        draw ball(r, 130+5i, 2/3 shade[(i-1) mod 5 + 1] , (1,1,7/8), 1/2 white) shifted (0,2i*r-r);
    endfor

    draw subpath (0,4) of base dashed withdots scaled 1/2 withpen pencircle scaled 1/8;
    draw point 0 of base -- point 0 of vent;
    draw point 4 of base -- point 4 of vent;
    draw vent;

endfig;
end.

请注意,我这里只做了假透明。你可以在Metapost中实现透明度但要正确地做到这一点却相当复杂。

答案4

您必须6*R通过更改所有10*R内容。我添加了两个球,并将这些修改放入您的代码中,我还更改了一些颜色,以便更好地直观地显示内容:

\documentclass[border=5]{standalone}
\usepackage{tikz}
\usepackage{tkz-euclide}
\usetkzobj{all}
\usetikzlibrary{shadings}
\begin{document}
\begin{tikzpicture}
\def\R{1}
  \fill[top color    = gray!50!black,
        bottom color = gray!10,
        middle color = red,
        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 = yellow,
        shading      = axis,
        opacity      = 0.25]
    (\R,0) -- (\R,10*\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,10*\R) circle (\R cm and 0.5cm);
  \draw (-\R,10*\R) -- (-\R,0) arc (180:360:\R cm and 0.5cm)
               -- (\R,10*\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);
\fill[thick, ball color=green!90, opacity = 0.5] (0,7*\R) circle (\R);
\fill[thick, ball color=yellow!90, opacity = 0.5] (0,9*\R) circle (\R);
 \end{tikzpicture}
\end{document} 

圆柱体

您可以在以下行中更改圆柱体的高度(\R,0) -- (\R,10*\R)

相关内容