我试图建立一个程序,可以产生(x,y,z)10x10x10阵列(单位细胞)上的三维网格。示例:一个立方体位于点(0,10)x(0,10)x(0,10),而另一个立方体位于点(0,10)x(10,20)x(0,10)。到目前为止,我有一个函数,使其中4个,但我如何将它自动化,使几十个,甚至数百个?你知道吗
import math
import numpy as np
def cubeplot():
count = 0
count2 = 0
x = 11
y = 11
z = 11
c = 11
parameter = np.arange(0,11,1)
xx, yy, zz = np.meshgrid(parameter, parameter, parameter)
valuesrange = np.zeros((11, 11, 11))
parameter2 = np.arange(c, y+10,1)
xx2, yy2, zz2 = np.meshgrid(parameter2, parameter2, parameter2)
valuesrange2 = np.zeros((x+10, y+10 , z + 10))
parameter3 = np.arange(c + 10, y+20,1)
xx2, yy2, zz2 = np.meshgrid(parameter2, parameter2, parameter2)
valuesrange2 = np.zeros((x+20, y+20 , z +20))
print('POINT 1')
while (count < 1):
xint = np.random.randint(0,2)
yint = np.random.randint(0,2)
zint = np.random.randint(0,2)
if xint > 0:
xint = np.random.randint(10,c, 22)
x = 10
else:
xint = np.random.randint(0,1, 22)
x = 0
if yint >0:
yint = np.random.randint(10,c, 22)
y = 10
else:
yint = np.random.randint(0,1, 22)
y = 0
if zint > 0:
zint = np.random.randint(10,c, 22)
z = 10
else:
zint = np.random.randint(0,1, 22)
z = 0
count = count + 1
print('x = ' + str(x))
print('y = ' + str(y))
print('z = ' + str(z))
# print('Distance = ' + str(zint))
print('POINT 2')
while (count2 < 1):
xint = np.random.randint(0,2)
yint = np.random.randint(0,2)
zint = np.random.randint(0,2)
if xint > 0:
xint = np.random.randint(20,c + 10, 22)
x2 = 20
else:
xint = np.random.randint(10,11, 22)
x2 = 10
if yint >0:
yint = np.random.randint(10,c, 22)
y2 = 10
else:
yint = np.random.randint(0,1, 22)
y2 = 0
if zint > 0:
zint = np.random.randint(10,c, 22)
z2 = 10
else:
zint = np.random.randint(0,1, 22)
z2 = 0
count2 = count2 + 1
print('x = ' + str(x2))
print('y = ' + str(y2))
print('z = ' + str(z2))
distance = ((x2-x)**2 + (y2 - y)**2 + (z2 - z)**2)**.5
print ('POINT 1: x,y,z: ' + str(x) +',' + str(y) + ','+ str(z))
print ('POINT 2: x,y,z: ' + str(x2) +',' + str(y2)+ ',' + str(z2))
print('Distance = ' + str(distance))
print('POINT 3')
count3 = 0
while (count3 < 1):
xint = np.random.randint(0,2)
yint = np.random.randint(0,2)
zint = np.random.randint(0,2)
if xint > 0:
xint = np.random.randint(10,c, 22)
x3 = 10
else:
xint = np.random.randint(0,1, 22)
x3 = 0
if yint >0:
yint = np.random.randint(10,c, 22)
y3 = 10
else:
yint = np.random.randint(0,1, 22)
y3 = 0
if zint > 0:
zint = np.random.randint(20,c + 10, 22)
z3 = 20
else:
zint = np.random.randint(10,c, 22)
z3 = 10
count3 = count3 + 1
print('x = ' + str(x3))
print('y = ' + str(y3))
print('z = ' + str(z3))
print('POINT 4')
count4 = 0
while (count4 < 1):
xint = np.random.randint(0,2)
yint = np.random.randint(0,2)
zint = np.random.randint(0,2)
if xint > 0:
xint = np.random.randint(20,c+10, 22)
x4 = 20
else:
xint = np.random.randint(10,c, 22)
x4 = 10
if yint >0:
yint = np.random.randint(10,c, 22)
y4 = 10
else:
yint = np.random.randint(0,1, 22)
y4 = 0
if zint > 0:
zint = np.random.randint(20,c + 10, 22)
z4 = 20
else:
zint = np.random.randint(10,c, 22)
z4 = 10
count4 = count4 + 1
print('x = ' + str(x4))
print('y = ' + str(y4))
print('z = ' + str(z4))
print ('POINT 3: x,y,z: ' + str(x3) +',' + str(y3) + ','+ str(z3))
print ('POINT 4: x,y,z: ' + str(x4) +',' + str(y4)+ ',' + str(z4))
print('SET END')
print('')
print('')
print('')
runtime = int (input("How many times would you like to run the program?: "))
maincount = 0
print ('The program will run', runtime, 'times')
while (maincount < runtime):
cubeplot()
maincount = maincount + 1
numpy arrays比你想象的要强大一些。如果您希望三维网格的每个单元都有另一个三维网格,那么您需要的是一个6D网格。检查以下示例:
根据您的需要,还有其他填充网格的方法(可能更为优化),但是您几乎不需要使用指数化数字,除非您确实需要这样做。这就是你要找的(逻辑上)?你知道吗
编辑:因此,如果您真的只需要一个三维网格,您可以将单元格尺寸乘以网格尺寸,然后循环索引其中的一些内容。例如:
在这种情况下,
grid[0:cell[0], 0:cell[1], 0:cell[2]]
将等于cell
维度的网格,其内容是1
。你知道吗相关问题 更多 >
编程相关推荐