python-3.x - 删除边界框点之间的网格点
问题描述
我正在寻找一种有效的方法来删除块边界框内的网格点(代码中的块 1 和 2)。我的代码是:
x_max, x_min, y_max, y_min = 156.0, 141.0, 96.0, 80.0
offset = 5
stepSize = 0.2
x = np.arange(x_min-offset, x_max+offset, stepSize)
y = np.arange(y_min-offset, y_max+offset, stepSize)
xv, yv = np.meshgrid(x, y)
#bounding box (and pints inside) that I want to remove for mesh
block1 = [(139.78, 86.4), (142.6, 86.4), (142.6, 88.0), (139.78, 88.0)]
block2 = [(154.8, 87.2), (157.6, 87.2), (157.6, 88.8), (154.8, 88.8)]
根据答案之一,如果我只有一个要从网格中删除的块,我可以生成所需的结果。如果我有多个块,那么它将无法工作。从网格中删除多个块的优化方法可能是什么。最终的图应该是这样的: Mesh
编辑:改进的问题和编辑的代码。
解决方案
我想我是根据@hpaulj 的解释弄清楚的(我也不能对他的建议投赞成票,可能是因为得分低)。我可以在 allBlocks 数组中附加块,然后在 allBlocks 上运行一个循环,同时禁用网格中的点。这是我的解决方案:
x_new = np.copy(xv)
y_new = np.copy(yv)
ori_x = xv[0][0]
ori_y = yv[0][0]
for block in allBlocks:
block_xmin = np.min((block[0][0], block[1][0]))
block_xmax = np.max((block[0][0], block[1][0]))
block_ymin = np.min((block[0][1], block[1][1]))
block_ymax = np.max((block[0][1], block[3][1]))
rx_min, rx_max = int((block_xmin-ori_x)/stepSize), int((block_xmax-ori_x)/stepSize)
ry_min, ry_max = int((block_ymin-ori_y)/stepSize), int((block_ymax-ori_y)/stepSize)
for i in range(rx_min,rx_max+1):
for j in range(ry_min,ry_max+1):
x_new[j][i] = np.nan
for i in range(ry_min,ry_max+1):
for j in range(rx_min,rx_max+1):
y_new[i][j] = np.nan
推荐阅读
- typescript - TypeScript 从泛型类型创建对象
- html - 将 Bootstrap 侧边栏扩展到网页末尾
- perl - 在匹配行之后打印第 n 行
- opencl - 在 Ubuntu 16.04 上安装英特尔 OpenCL SDK
- python - Flask Web 应用程序上的 Ajax 错误
- sql - 转置 SQL 查询结果
- ffmpeg - ffmpeg,缩放和硬编码的潜艇
- python - Allen Bradley L16ER 通过 Python 进行通信
- sql-server - 将数据库从 SQL Server 2008 复制到 SQL Server 2016
- types - OCaml 识别类型