python - 在球面上均匀分布n个点

标签 python algorithm math geometry uniform

我需要一种算法,它可以为我在一个球体周围提供 N 个点(可能小于 20 个)的位置,并将它们模糊地分散开来。不需要“完美”,但我只需要它,所以它们都不会聚集在一起。

  • This question提供了很好的代码,但我找不到制作这种统一的方法,因为这似乎是 100% 随机的。
  • This blog post推荐有两种方法允许输入球体上的点数,但 Saff and Kuijlaars算法完全是我可以转录的伪代码,code example我发现包含“node [k]”,我看不到它的解释并破坏了这种可能性。第二个博客示例是黄金分割螺旋,它给了我奇怪的、成束的结果,没有明确的方法来定义一个恒定的半径。
  • This algorithm来自 this question似乎它可以工作,但我无法将该页面上的内容拼凑成伪代码或任何东西。

我遇到的其他一些问题主题谈到了随机均匀分布,这增加了我不关心的复杂程度。很抱歉,这是一个如此愚蠢的问题,但我想表明我真的很努力,但仍然没有完成。

所以,我正在寻找的是简单的伪代码,用于将 N 个点均匀分布在一个单位球体周围,以球坐标或笛卡尔坐标返回。如果它甚至可以稍微随机分布(想象一下围绕恒星的行星,分布均匀,但有余地),那就更好了。

最佳答案

The Fibonacci sphere algorithm非常适合这个。它速度很快,并且给出的结果一目了然很容易欺骗人眼。 You can see an example done with processing随着点的增加,这将随着时间的推移显示结果。 Here's another great interactive example由@gman 制作。这是一个简单的python实现。

import math


def fibonacci_sphere(samples=1000):

    points = []
    phi = math.pi * (3. - math.sqrt(5.))  # golden angle in radians

    for i in range(samples):
        y = 1 - (i / float(samples - 1)) * 2  # y goes from 1 to -1
        radius = math.sqrt(1 - y * y)  # radius at y

        theta = phi * i  # golden angle increment

        x = math.cos(theta) * radius
        z = math.sin(theta) * radius

        points.append((x, y, z))

    return points

1000 个样本为您提供:

enter image description here

关于python - 在球面上均匀分布n个点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9600801/

相关文章:

c++ - UTF-8 表示形式和 unicode 表示形式之间的字符串转换

Python 递归回溯数独求解器

algorithm - PCA:查找协方差矩阵的特征值:求解 N 次多项式

找到具有首选组的组和对象的最佳分配的算法

python - "The system cannot find the file specified"在 python 中调用 subprocess.Popen 时

python - 在没有模板系统的情况下安装 Pyramid(Mako 和 Chameleon)

c++ - 没有累积缓冲区的 OpenGL 运动模糊

c# - 为什么 -2 % 360 在 C# 中给出 -2 而不是 358

Python ElementTree XML 解析与多个答案

python - 如何使用 Bottle 获取请求正文?