简体   繁体   English

如何使用 1 形成“菱形”形状的 2D 二进制数组

[英]How to create a 2D binary array with 1's forming a "diamond" shape

I need to create a function that results in a diamond shape when given a side length.我需要创建一个函数,当给定边长时会产生菱形。 The diamond array should consist of values of 0 and 1.菱形数组应由 0 和 1 值组成。

So far I figured out how to make the diamond, I don't know how to program a function for different side lengths到目前为止我想出了如何制作钻石,我不知道如何为不同的边长编写函数

So far I have this solution for a side length of 3:到目前为止,我有一个边长为 3 的解决方案:

import numpy as np

#line1
a=np.zeros(3+2)
a[3-1]=1

#line2
b=np.zeros(3+2)
b[3-2]=1
b[3]=1

#line3
c=np.zeros(3+2)
c[3-3]=1
c[3+1]=1

print(np.concatenate((a,b,c,b,a),axis=1).reshape(5,5))

How could I write a function for different lengths?我怎么能写一个不同长度的函数?
Also, if given a length of 1 it should return [[1]]此外,如果给定长度为 1,则应返回[[1]]

You can do this using an intersection of horizontal and vertical patterns:您可以使用水平和垂直模式的交集来做到这一点:

import numpy as np

N       = 5
H       = abs(np.arange(1-N,N+1,2))//2
V       = H[0] - H[:,None]
diamond = (H==V)*1

print(diamond)

[[0 0 1 0 0]
 [0 1 0 1 0]
 [1 0 0 0 1]
 [0 1 0 1 0]
 [0 0 1 0 0]]

Visually, this corresponds to intersecting number equalities between rows and columns:从视觉上看,这对应于行和列之间的相交数相等:

for N=7:对于 N=7:

     [3, 2, 1, 0, 1, 2, 3]
   0  .  .  .  x  .  .  .         
   1  .  .  x  .  x  .  .
   2  .  x  .  .  .  x  .     
   3  x  .  .  .  .  .  x
   2  .  x  .  .  .  x  .
   1  .  .  x  .  x  .  .
   0  .  .  .  x  .  .  .

for N=8:对于 N=8:

     [3, 2, 1, 0, 0, 1, 2, 3]
   0  .  .  .  x  x  .  .  .         
   1  .  .  x  .  .  x  .  .         
   2  .  x  .  .  .  .  x  .         
   3  x  .  .  .  .  .  .  x         
   3  x  .  .  .  .  .  .  x         
   2  .  x  .  .  .  .  x  .         
   1  .  .  x  .  .  x  .  .         
   0  .  .  .  x  x  .  .  .         

If you want the diamond to be filled, use diamond = (H<=V)*1如果要填充diamond = (H<=V)*1 ,请使用diamond = (H<=V)*1

I took the longer way so I could extend the function to handle other geometries我花了更长的时间,所以我可以扩展函数来处理其他几何图形

import numpy as np

def diamondarray(dimension=1):

    #// initialize 2d array
    a=np.zeros((dimension,dimension))

    #// find the middle of the array
    midpoint=(dimension-1)/2

    #// initialize an offset
    offset=-1
    offsetstep=1

    #// loop through rows and columns
    for row in range(dimension):
        if dimension%2 == 0 and row == np.ceil(midpoint):
            #// repeat offset for second midpoint row
            offset=offset
        else:
            if row <= np.ceil(midpoint):
                #// increase offset for each row for top
                offset=offset+offsetstep
            else:
                #// decrease offset for each row for bottom
                offset=offset-offsetstep

        for col in range(dimension):
            #// set value to one
            if dimension%2 == 0:
                if col <= np.floor(midpoint):
                    if col == np.floor(midpoint)-offset:
                        a[row,col]=fill
                if col >= np.ceil(midpoint):
                    if col == int(midpoint)+offset+1:
                        a[row,col]=fill
            else:
                if col == midpoint+offset or col == midpoint-offset:
                    pass
                    a[row,col]=fill
    return a

For N=5:对于 N=5:

print(diamondarray(5))打印(钻石阵列(5))

[[0. 0. 1. 0. 0.]
 [0. 1. 0. 1. 0.]
 [1. 0. 0. 0. 1.]
 [0. 1. 0. 1. 0.]
 [0. 0. 1. 0. 0.]]

For N=8:对于 N=8:

print(diamondarray(8))打印(钻石阵列(8))

[[0. 0. 0. 1. 1. 0. 0. 0.]
 [0. 0. 1. 0. 0. 1. 0. 0.]
 [0. 1. 0. 0. 0. 0. 1. 0.]
 [1. 0. 0. 0. 0. 0. 0. 1.]
 [1. 0. 0. 0. 0. 0. 0. 1.]
 [0. 1. 0. 0. 0. 0. 1. 0.]
 [0. 0. 1. 0. 0. 1. 0. 0.]
 [0. 0. 0. 1. 1. 0. 0. 0.]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM