我怎样才能找到下面问题的最佳解决方案?

2024-09-26 22:54:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我创建了一个名为grid的3D列表,并创建了另一个与上一个名为elevation with zero elements的列表大小相同的列表(elevation在这里表示为每个元素的高程=1)。我给了栅格地图中的每个元素一个随机数,它代表一个高程

我给了一个无人机高度=60

如果这些随机数大于60。。。把它们画成黑色 如果这些随机数在50-60之间。。。把它们画成红色 如果这些随机数小于50。。。把它们画成紫色

现在,我试着用1来绘制网格和元素,1取随机数(显示它们的高度)。我能得到什么帮助吗?我怎样才能完成它

x represents a map with 
# 0 = navigable space
# 1 = occupied space
y the elvation of every 1 in x

我在下面写了这个代码,但它没有给我想要的。我想显示,这些随机数在我的绘图中的高度是三维的

我在下面尝试:

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import style
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

import random
import numpy as np

grid = [[[0, 1, 0, 0, 0, 0],
        [0, 1, 0, 0, 0, 0],
        [0, 1, 0, 0, 0, 0],
        [0, 1, 0, 0, 0, 0],
        [0, 0, 0, 0, 1, 0]],
        [[0, 1, 0, 0, 0, 0],
        [0, 1, 0, 0, 0, 0],
        [0, 1, 0, 0, 0, 0],
        [0, 1, 0, 0, 0, 0],
        [0, 0, 0, 0, 1, 0]],
        [[0, 1, 0, 0, 0, 0],
        [0, 1, 0, 0, 0, 0],
        [0, 1, 0, 0, 0, 0],
        [0, 1, 0, 0, 0, 0],
        [0, 0, 0, 0, 1, 0]]]
risk = [i for i in range(50,60)]
init = [0,0,0]
goal = [1,4,5]
x= init[0]
y= init[1]
z= init[2]

x1= goal[0]
y1= goal[1]
z1= goal[2]
drone_height = 60
dxi=np.ones(1)
dyj=np.ones(1)
dzk=np.ones(1)
elevation = [[[0 for row in range(len(grid[0][0]))] for col in range(len(grid[0]))] for a in range(len(grid))]
for i in range(len(grid)):    
    for j in range(len(grid[0])):
        for k in range(len(grid[0][0])):

            if grid[i][j][k] == 1:
                elevation[i][j][k] = random.randint(1,100)
                if elevation[i][j][k] >= drone_height:
                    ax.bar3d(i, j, k, dxi, dyj,  dzk, color='black')
                elif elevation[i][j][k] in risk:
                    ax.bar3d(i, j, k, dxi, dyj,  dzk, color='red')
                else:
                    ax.bar3d(i, j, k, dxi, dyj,  dzk, color='purple')
            else:
                elevation[i][j][k] = 0

ax.scatter(x,y,z, color='g', marker='o')
ax.scatter(x1,y1,z1, color='b', marker='o')
print(elevation)
plt.show()

Tags: inimportforleninitnprangeax

热门问题