如何使用 tikz 在特定空间中生成随机球?

如何使用 tikz 在特定空间中生成随机球?

我创建了一个 tikz 图片,如下所示:

\documentclass{article}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}[scale=1.5]
\draw[blue,thin,->,dashed] (0,-1.5) -- (0,1.5)node[blue,right]{\scriptsize $y_{RP}$};
\draw[blue,thin,->,dashed] (-2.5,0) -- (2.5,0)node[blue,below]{\scriptsize $x_{RP}$};
\draw[blue,dashed] (-0.6,0) circle (1);
\draw[blue,dashed] (0.6,0) circle (1);
\fill[inner color=black,outer color=green,dashed] (0.1,0.1) circle (0.05);
\draw[red,rotate=10] (0,0) ellipse (0.3cm and 0.6cm);
\draw[red,->,rotate=10] (0,0) -- (2.5,0)node[above,rotate=10,red]{\scriptsize $x_{PP}$};
\draw[red,->,rotate=10] (0,0) -- (0,1.5)node[left,rotate=10,red]{\scriptsize $y_{PP}$};
\end{tikzpicture} 
\end{document}

示意图重现: 图片

我想将一些(我不知道确切的数字,但可能是 10-15)个球放在随机位置以填充红色椭圆。我知道我可以用 和\usetikzlibrary{shapes}来制作一个球\shade [ball color=green] (0.1,0.1) circle [radius=0.05cm];,但我在这里只放了一个圆圈。

我们可以用 tikz 来实现吗?我们该怎么做?谢谢,

答案1

这实际上只是一个问题,即您需要做什么才能将一个点随机地放置在椭圆内。如果您使用参数方程,那么您应该选择一个随机角度和沿两个轴的随机长度,然后您就会得到一个随机点。

编辑

事实上,由于只有两个坐标,因此最好随机选择极坐标,然后重新缩放为椭圆。

在此处输入图片描述

以下是代码:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{math, shapes}

\newcommand\RandomPointInEllipse[3]{% \RandomPointInEllipse[a][b][rotation]
  % parametrically, the ellipse is (a\cos u, b\sin u), so randomly
  % choose polar coordinates (r,u) and then rescale to ellipse
  \pgfmathsetmacro\uRnd{random(0,360)}
  \pgfmathsetmacro\rRnd{random}
  \pgfmathsetmacro\aRnd{#1*\rRnd*cos(\uRnd)}
  \pgfmathsetmacro\bRnd{#2*\rRnd*sin(\uRnd)}
  \shade [ball color=green, rotate=#3] (\aRnd,\bRnd) circle [radius=0.05cm];
}
\begin{document}
  \begin{tikzpicture}[scale=1.5]
    \draw[blue,thin,->,dashed] (0,-1.5) -- (0,1.5)node[blue,right]{\scriptsize $y_{RP}$};
    \draw[blue,thin,->,dashed] (-2.5,0) -- (2.5,0)node[blue,below]{\scriptsize $x_{RP}$};
    \draw[blue,dashed] (-0.6,0) circle (1);
    \draw[blue,dashed] (0.6,0) circle (1);
    \fill[inner color=black,outer color=green,dashed] (0.1,0.1) circle (0.05);
    \draw[red,rotate=10] (0,0) ellipse (0.3cm and 0.6cm);
    \draw[red,->,rotate=10] (0,0) -- (2.5,0)node[above,rotate=10,red]{\scriptsize $x_{PP}$};
    \draw[red,->,rotate=10] (0,0) -- (0,1.5)node[left,rotate=10,red]{\scriptsize $y_{PP}$};
    \foreach \ball in {1,...,10} { \RandomPointInEllipse{0.3}{0.6}{10}}
  \end{tikzpicture}
\end{document}

答案2

类似这样的事情可能会有帮助......

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes,calc}
\begin{document}
\begin{tikzpicture}[
    ]
        \begin{scope}[rotate=10]
              \draw[red] (0,0) ellipse (0.3 and 0.6);
              \foreach \x in {1,...,10} {
                  \shade [ball color=green] (0.25*rand, 0.55*rand) circle [radius=0.05];
               }
        \end{scope}
\end{tikzpicture}
\end{document}

x...棘手的部分是绝对值的坐标和tikz 的混合y

在此处输入图片描述

答案3

使用clip

\documentclass[tikz, margin=3mm]{standalone}

\begin{document}
    \begin{tikzpicture}[scale=1.5,
        N/.style   = {rotate=#1, font=\scriptsize},
        N/.default = 0,
        L/.style   = {blue, dashed, ->}
                    ]
\draw[L,thin]   ( 0.0,-1.5) -- (0,1.5) node[N,right] {$y_{RP}$};
\draw[L,thin]   (-2.5, 0.0) -- (2.5,0) node[N,below] {$x_{RP}$};
\draw[blue]     (-0.6, 0.0) circle (1);
\draw[blue]     ( 0.6, 0.0) circle (1);
\draw[red,->,rotate=10] (0,0) -- (2.5,0) node[N=10,above] {$x_{PP}$};
\draw[red,->,rotate=10] (0,0) -- (0,1.5) node[N=10,left]  {$y_{PP}$};
%
\begin{scope}[red,rotate=10]
\draw[clip] (0,0) ellipse (0.3cm and 0.6cm);
\foreach \i in {1,...,30}
\fill[ball color=green] (rand*360:rand/2) circle (0.25mm);
\end{scope}
    \end{tikzpicture}
\end{document}

在此处输入图片描述

答案4

元帖子使用transformation让椭圆保持在两个圆的重叠范围内,并将球整齐地放置在里面。 椭圆形中的球

\RequirePackage{luatex85}
\documentclass[border=5mm]{standalone}
\usepackage{luamplib}
\begin{document}
\mplibtextextlabel{enable}
\begin{mplibcode}
beginfig(1);

vardef ball(expr r, light_angle, base_shade, white_shade, rim_shade) = 
    save c; path c, c'; 
    c = fullcircle scaled 2r;
    c' = fullcircle scaled 1/32 r 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);

    numeric u; u = 42;

    path xx, xx', yy, yy', C, D, E;

    xx = (left -- right) scaled 4u;
    yy = (down -- up) scaled 3u;
    xx' = subpath (1/2, 1) of xx rotated 12;
    yy' = subpath (1/2, 1) of yy rotated 12;

    numeric d; d = .8u;
    C = fullcircle scaled 4u shifted (d,0);
    D = fullcircle scaled 4u shifted (-d,0);

    z0 = xx' intersectionpoint D;
    z1 = yy' intersectionpoint C;

    transform T; 
    origin transformed T = origin;
    right transformed T = 3/4 z0;
    up transformed T = 3/4 z1;
    E = fullcircle scaled 2 transformed T;

    drawoptions(dashed withdots scaled 1/2 withcolor 2/3 blue);
    drawarrow xx; drawarrow yy; draw C; draw D;
    label.rt("$x_{RP}$", point 1 of xx);
    label.urt("$y_{RP}$", point 1 of yy);

    drawoptions(withcolor 3/4 red);
    drawarrow xx'; drawarrow yy'; draw E;
    label.urt("$x_{PP}$", point 1 of xx');
    label.ulft("$y_{PP}$", point 1 of yy');

    drawoptions();

    picture b; b = ball(2.4, 135, 3/4 green, red+green, 3/4 green);

    for i=1 upto 20:
        draw b shifted (uniformdeviate 7/8 * dir uniformdeviate 360 transformed T);
    endfor

endfig;
\end{mplibcode}
\end{document}

这是包裹在luamplib因此编译lualatex,或适应普通 MP 或gmp

相关内容