在球体表面创建一组随机点

发布于 2025-01-09 13:59:59 字数 1475 浏览 0 评论 0原文

我编写了一个代码来在同心球表面上生成一组随机点,在每个球上我会根据我想要的同心球表面的数量生成一定数量的点,如下面编写的代码所示,您可以尝试运行它,也许可以更好地理解手头的问题:

class point:
    def __init__(self, x, y, z):
        self.x = x
        self.y = y
        self.z = z

    R_list=(0.001 , 0.002, 0.003 , 0.004 ,0.005)
    num_points = 1000/len(R_list)
    LD1 = 0.05
    LD23 = 0.025
    obj_points = []
    condition = True
    for R in R_list:
        while len(rpoints) < num_points:
           theta = np.random.uniform(-math.pi, math.pi)
           phi = np.random.uniform(-math.pi, math.pi)
           pi = R * math.cos(theta) * math.sin(phi) + (LD1 / 2), R * math.sin(theta) * math.sin(phi) + (
                    LD23 / 2), R * math.cos(phi) + (LD23 / 2)
           obj_points.append(point(pi[0], pi[1], pi[2]))
    
      # plott array
    lt = len(obj_points)
    Vis_arr = np.zeros((lt, 3))
    n = 0
    for n in range(0, lt-1 ):
        Vis_arr[n][0] = obj_points[n].x
        Vis_arr[n][1] = obj_points[n].y
        Vis_arr[n][2] = obj_points[n].z
    
    # scattering of points
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    # scattering of points
    ax.scatter3D(Vis_arr[:, 0], Vis_arr[:, 1], Vis_arr[:, 2], marker="o")
    # graph output fully no evolution
    plt.show()

但是当我运行代码时,一个随机点突然出现: 输入图片此处描述

我怀疑这是创建点数组时的错误,但我无法检测到问题的确切原因。

i wrote a code to generate a set of random points on the surface of concentric spheres,where on each sphere I would generate a certain number of points depending on how many concentric sphere surfaces I want as seen in the code written below,you can try to run it and maybe get a better understanding of the issue at hand:

class point:
    def __init__(self, x, y, z):
        self.x = x
        self.y = y
        self.z = z

    R_list=(0.001 , 0.002, 0.003 , 0.004 ,0.005)
    num_points = 1000/len(R_list)
    LD1 = 0.05
    LD23 = 0.025
    obj_points = []
    condition = True
    for R in R_list:
        while len(rpoints) < num_points:
           theta = np.random.uniform(-math.pi, math.pi)
           phi = np.random.uniform(-math.pi, math.pi)
           pi = R * math.cos(theta) * math.sin(phi) + (LD1 / 2), R * math.sin(theta) * math.sin(phi) + (
                    LD23 / 2), R * math.cos(phi) + (LD23 / 2)
           obj_points.append(point(pi[0], pi[1], pi[2]))
    
      # plott array
    lt = len(obj_points)
    Vis_arr = np.zeros((lt, 3))
    n = 0
    for n in range(0, lt-1 ):
        Vis_arr[n][0] = obj_points[n].x
        Vis_arr[n][1] = obj_points[n].y
        Vis_arr[n][2] = obj_points[n].z
    
    # scattering of points
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    # scattering of points
    ax.scatter3D(Vis_arr[:, 0], Vis_arr[:, 1], Vis_arr[:, 2], marker="o")
    # graph output fully no evolution
    plt.show()

but when I run the code a random point appears out of nowhere:
enter image description here

I suspect that this is an error in creating the arrays of points but I cant detect the exact reason of the problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小兔几 2025-01-16 13:59:59

您的代码中有两个错误:

while len(rpoints) < num_points:

应该是:

while len(obj_points) < num_points:

for n in range(0, lt-1 ):

应该是:

for n in range(0, lt ):

You have two errors in your code:

while len(rpoints) < num_points:

should be:

while len(obj_points) < num_points:

and

for n in range(0, lt-1 ):

should be:

for n in range(0, lt ):
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文