【问题标题】:Turn grid into a checkerboard pattern in python?在python中将网格变成棋盘图案?
【发布时间】:2016-10-31 05:28:47
【问题描述】:

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

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

目前 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()

代码输出:

【问题讨论】:

    标签: python python-2.7 python-3.x numpy matplotlib


    【解决方案1】:
    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)
    

    【讨论】:

      【解决方案2】:

      只需对代码进行少量修改,它就会如下所示:

      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
              if (int(i / 10) + int(j / 10)) % 2:  # the only two extra lines. 
                  m[i][j] = 0                      #
      imshow(m)
      show()
      

      或者只是这个(假设你真的需要 100x100)来摆脱“边界线”:

      m=zeros((100,100))
      for i in range(100):
          for j in range(100):
              m[i][j] = (int(i / 10) + int(j / 10)) % 2
      

      干杯。

      【讨论】:

        【解决方案3】:

        您可以使用 NumPy 创建一个棋盘格样式数组,然后使用 scipy's imresize 调整它的大小以使其等于您想要的画布区域。

        因此,步骤将是:

        1) 创建一个形状为(10,10) 的NumPy 数组,对应于10 x 10 大小的棋盘图案。为此,请从 zeros 数组开始,并使用 ones 填充交替的行和列:

        arr = np.zeros((10,10),dtype=int)
        arr[::2,::2] = 1
        arr[1::2,1::2] = 1
        

        2) 调整数组10x 的大小以得到(100,100) 像素大小的输出图像:

        from scipy.misc import imresize # Importing required function
        
        out = imresize(arr,10*np.array(arr.shape),interp='nearest')/255
        

        输出:

        【讨论】:

          【解决方案4】:

          我会创建一个线性数组,每隔一秒填充一次值并重新整形。 在您的情况下(偶数列),预先添加一列并在重塑后将其删除:

          import numpy as np
          
          rows = 100
          cols = 100 + 1  # product of rows*cols must be odd, we fix it later
          m = np.zeros((rows*cols, 1))  # create array
          m[::2] = 1  # fill every second
          m = np.reshape(m, (rows, cols))  # reshape array to matrix
          m = m[:, :-1]  # cut additional column
          

          【讨论】:

            【解决方案5】:

            使用取模运算:

            m[i][j] = (i+j) % 2
            

            【讨论】:

              【解决方案6】:

              您可以检查两个索引(行和列)的总和,如果它是奇数则用第一种颜色着色,否则用第二种颜色着色。比如:

              for i in range(nrows):
                  for j in range(ncols):
                      m[i][j] = 0 if (i+j)%2 else 1
              

              【讨论】:

              • 非常简单。
              猜你喜欢
              • 1970-01-01
              • 2016-05-23
              • 2022-11-04
              • 1970-01-01
              • 1970-01-01
              • 2021-06-10
              • 1970-01-01
              • 2021-07-07
              • 2019-10-10
              相关资源
              最近更新 更多