【发布时间】:2015-04-01 19:10:12
【问题描述】:
上面的代码当前生成的是一个立方体,而不是一个球体。我该如何纠正?
x = list(range(-5,5))
y = list(range(-5,5))
z = list(range(-5,5))
r = [1,2,3,4,5,6,7,10]
xcoords = []
ycoords = []
zcoords = []
"""functions used"""
def square(number):
return number**2
"""determines which values satisfies the equation for a sphere"""
for itemx in x:
for itemy in y:
for itemz in z:
for itemr in r:
if abs(itemx) == abs(itemy) and abs(itemy) == abs(itemz) and square(itemx) + square(itemy) +square(itemz) == itemr:
xcoords.append(itemx)
ycoords.append(itemy)
zcoords.append(itemz)
"""determines the number of atoms in the system"""
natoms = len(xcoords)
"""writes coords onto txt file"""
out = open("sphere.xyz", "a")
print (natoms)
out.write("%s \n \n" %(natoms))
out.close()
for item in zip(xcoords, ycoords,zcoords):
out = open("sphere.xyz", "a")
print (item)
out.write( "Ar" " " " " "%2.5s %2.5s %2.5s \n" %(item))
out.close()
我考虑过使用球坐标来定义球体的参数,但我不知道如何使用 python 进行设置。
【问题讨论】:
-
你能给我看一个'球点'的例子吗?其中所有坐标 (x, y, z) 和 r 都是你定义的范围内的整数?
-
这让我想起了我对Drawing Sphere in OpenGL without using gluSphere()? 的一个旧答案。也许对你有用。
-
我认为最大的问题是您明显假设您会在特定网格上找到坐标的球体上的点。给定网格上的质心,半径是网格间距的倍数,绝大多数球体将只有 8 个这样的点——半径上的每个方向都有一个点,与通过质心的每个轴对齐——网格对齐的边界立方体的面...
标签: python computational-geometry