Python 球面上的点

Python 球面上的点,python,numpy,geometry,Python,Numpy,Geometry,我是Python新手,我有一个半径为(R)的球体,中心位于(x0,y0,z0)。现在,我需要找到球体表面或球体内部的点,例如满足((x1-x0)**2+(y1-y0)**2+(z1-x0)**82)**1/2的点(x1,y1,z1),总结评论中讨论的内容,以及其他一些点: 不需要过滤这些点,因为u得到的错误是什么?具体一点:!你的具体问题是什么?亲爱的@KlausD。它打印所有坐标,而不是只打印球体内部或表面的坐标。正如我在正文中所说的,我想得到一个点坐标的numpy数组,这些点要么在球体内部

我是Python新手,我有一个半径为(R)的球体,中心位于(x0,y0,z0)。现在,我需要找到球体表面或球体内部的点,例如满足((x1-x0)**2+(y1-y0)**2+(z1-x0)**82)**1/2的点(x1,y1,z1),总结评论中讨论的内容,以及其他一些点:


  • 不需要过滤这些点,因为
    u得到的错误是什么?具体一点:!你的具体问题是什么?亲爱的@KlausD。它打印所有坐标,而不是只打印球体内部或表面的坐标。正如我在正文中所说的,我想得到一个点坐标的numpy数组,这些点要么在球体内部,要么在球体表面。例如,如果10个点(n=10)中只有3个满足该标准,则程序应给出这3个点坐标的3*3矩阵。[x1,y1,z1],[x2,y2,z2]…]亲爱的@DOOM我已经在上面解释了一点,问题是行
    u=np.random.uniform(0,1,size=(n,)
    r=r*np.cbrt(u)
    <代码>u
    import numpy as np
    import math
    
    def create_points_around_atom(number,atom_coordinates):
        n= number
        x0 = atom_coordinates[0]
        y0 = atom_coordinates[1]
        z0 = atom_coordinates[2]
        R = 1.2
        for i in range(n):
            phi = np.random.uniform(0,2*np.pi,size=(n,))
            costheta = np.random.uniform(-1,1,size=(n,))
            u = np.random.uniform(0,1,size=(n,))
            theta = np.arccos(costheta)
            r = R * np.cbrt(u)
            x1 = r*np.sin(theta)*np.cos(phi) 
            y1 = r*np.sin(theta)*np.sin(phi)
            z1 = r*np.cos(theta)
            dist  = np.sqrt((x1-x0)**2+(y1-y0)**2+(z1-z0)**2)
            distance = list(dist)
            point_on_inside_sphere = []
            for j in distance:
                if j <= R:
                    point_on_inside_sphere.append(j)
                    print('j:',j,'\tR:',R)
                    print('The list is:', point_on_inside_sphere)
                    print(len(point_on_inside_sphere))
                    kk =0
                    for kk in range(len(point_on_inside_sphere)):
                        for jj in point_on_inside_sphere:
                            xx = np.sqrt(jj**2-y1**2-z1**2)
                            yy = np.sqrt(jj**2-x1**2-z1**2)
                            zz = np.sqrt(jj**2-y1**2-x1**2)
                        print("x:", xx, "y:", yy,"z:", zz)
                    kk +=1 
    
    # pass radius as an argument
    def create_points_around_atom(number, center, radius):
    
        # generate the random quantities
        phi         = np.random.uniform( 0, 2*np.pi, size=(number,))
        theta_cos   = np.random.uniform(-1,       1, size=(number,))
        u           = np.random.uniform( 0,       1, size=(number,))
    
        # calculate sin(theta) from cos(theta)
        theta_sin   = np.sqrt(1 - theta_cos**2)
        r           = radius * np.cbrt(u)
    
        # use list comprehension to generate the coordinate array without a loop
        # don't forget to offset by the atom's position (center)
        return np.array([
            np.array([
                center[0] + r[i] * theta_sin[i] * np.cos(phi[i]),
                center[1] + r[i] * theta_sin[i] * np.sin(phi[i]),
                center[2] + r[i] * theta_cos[i]
            ]) for i in range(number)
        ])