Skip to main content
 首页 » 编程设计

python之在 python 中将网格变成棋盘格图案

2025年04月02日14duanxz

我已经成功地创建了一个网格,但现在我正在尝试将我的网格变成棋盘图案,最好使用 floodfill 命令的变体。

我如何确保程序识别哪些方 block 是偶数,哪些是奇数?

目前 IDE 设置为 m[i][j]= 1 给出蓝色,而 m[i][j]= 0 给出红色,我很高兴保留,所以我不需要定义颜色。谢谢。

我目前的代码:

from pylab import * 
from numpy import * 
from math import * 
 
m=zeros((100,100)) 
for i in range(100): 
    for j in range(100): 
        if (math.floor(i) % 10) != 0: 
            if (math.floor(j) % 10) != 0: 
                m[i][j]= 1 
            else: 
                m[i][j]= 0 
 
imshow(m) 
show() 

代码输出:

请您参考如下方法:

import numpy as np 
def make_checkerboard(n_rows, n_columns, square_size): 
 
    n_rows_, n_columns_ = int(n_rows/square_size + 1), int(n_columns/square_size + 1) 
    rows_grid, columns_grid = np.meshgrid(range(n_rows_), range(n_columns_), indexing='ij') 
    high_res_checkerboard = np.mod(rows_grid, 2) + np.mod(columns_grid, 2) == 1 
    square = np.ones((square_size,square_size)) 
    checkerboard = np.kron(high_res_checkerboard, square)[:n_rows,:n_columns] 
 
    return checkerboard 
 
 
square_size = 5 
n_rows = 14 
n_columns = 67 
checkerboard = make_checkerboard(n_rows, n_columns, square_size)