如何在球体上随机散布点

Str*_*ent 5 python random sample julia


using PyPlot
n = 50
u = range(0,stop=2*?,length=n);
v = range(0,stop=?,length=n);

x = cos.(u) * sin.(v)';
y = sin.(u) * sin.(v)';
z = ones(n) * cos.(v)';

scatter3D(vec(x),vec(y),vec(z);c="red",s=1)
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

不过,如果我乘vec(x)vec(y)vec(z)rand()
我仍然得到同样的情节,唯一的区别是,轴变化或者换句话说,该领域被“压扁”。


using PyPlot
n = 50
u = range(0,stop=2*?,length=n);
v = range(0,stop=?,length=n);

x = cos.(u) * sin.(v)';
y = sin.(u) * sin.(v)';
z = ones(n) * cos.(v)';

scatter3D(rand()*vec(x),rand()*vec(y),rand()*vec(z);c="red",s=1)

Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

Ste*_*ski 7

最简单的方法似乎是对每个维度进行高斯采样,然后如本答案中所述对结果向量的长度进行归一化。获得零长度向量的可能性很小,可以通过拒绝采样来处理。把它们放在一起,你会这样做:

points = map(1:n) do _
   while true
       x = randn()
       y = randn()
       z = randn()
       l = hypot(x, y, z)
       l ? 0 && return (x, y, z) ./ l
    end
end
Run Code Online (Sandbox Code Playgroud)

这给出了一个由 3 元组组成的向量,每个元组代表点的 x、y 和 z 坐标,您可以像以前一样绘制它们。可以使用推导式提取单独的坐标向量:

xs = [p[1] for p in points]
ys = [p[2] for p in points]
zs = [p[3] for p in points]
Run Code Online (Sandbox Code Playgroud)

这种方法可以很容易地推广到任意数量的维度。