[Question]
  [
## Challenge
Given two integer values \$a \ge 2\$ and \$0 \le b < a\$, generate a \$(2a-1) \times (2a-1)\$ matrix consisting of the integers 0, 1, and 2 as follows:
1. Create a checkerboard of 0s and 1s of the given size, corners being 0.
2. If \$b > 0\$, overwrite the checkerboard with 2s in the X shape at the center, each leg being of length \$b\$ (not counting the center).
This pattern is directly modeled from Art Puzzle event grids (where \$a\$ is fixed at 3, \$b=0,1,2\$ represent easy/normal/hard grids, and the 0, 1, 2s in the output represent easy/normal/hard minipuzzles respectively). Blame it if you don't like the edge case of \$b=0\$ :P
Standard [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'") rules apply. The shortest code in bytes wins.
## Test cases
```
a = 2, b = 0
[[0, 1, 0],
 [1, 0, 1],
 [0, 1, 0]]
a = 2, b = 1
[[2, 1, 2],
 [1, 2, 1],
 [2, 1, 2]]
a = 3, b = 0
[[0, 1, 0, 1, 0],
 [1, 0, 1, 0, 1],
 [0, 1, 0, 1, 0],
 [1, 0, 1, 0, 1],
 [0, 1, 0, 1, 0]]
a = 3, b = 1
[[0, 1, 0, 1, 0],
 [1, 2, 1, 2, 1],
 [0, 1, 2, 1, 0],
 [1, 2, 1, 2, 1],
 [0, 1, 0, 1, 0]]
a = 3, b = 2
[[2, 1, 0, 1, 2],
 [1, 2, 1, 2, 1],
 [0, 1, 2, 1, 0],
 [1, 2, 1, 2, 1],
 [2, 1, 0, 1, 2]]
a = 4, b = 0
[[0, 1, 0, 1, 0, 1, 0],
 [1, 0, 1, 0, 1, 0, 1],
 [0, 1, 0, 1, 0, 1, 0],
 [1, 0, 1, 0, 1, 0, 1],
 [0, 1, 0, 1, 0, 1, 0],
 [1, 0, 1, 0, 1, 0, 1],
 [0, 1, 0, 1, 0, 1, 0]]
a = 4, b = 1
[[0, 1, 0, 1, 0, 1, 0],
 [1, 0, 1, 0, 1, 0, 1],
 [0, 1, 2, 1, 2, 1, 0],
 [1, 0, 1, 2, 1, 0, 1],
 [0, 1, 2, 1, 2, 1, 0],
 [1, 0, 1, 0, 1, 0, 1],
 [0, 1, 0, 1, 0, 1, 0]]
a = 4, b = 2
[[0, 1, 0, 1, 0, 1, 0],
 [1, 2, 1, 0, 1, 2, 1],
 [0, 1, 2, 1, 2, 1, 0],
 [1, 0, 1, 2, 1, 0, 1],
 [0, 1, 2, 1, 2, 1, 0],
 [1, 2, 1, 0, 1, 2, 1],
 [0, 1, 0, 1, 0, 1, 0]]
a = 4, b = 3
[[2, 1, 0, 1, 0, 1, 2],
 [1, 2, 1, 0, 1, 2, 1],
 [0, 1, 2, 1, 2, 1, 0],
 [1, 0, 1, 2, 1, 0, 1],
 [0, 1, 2, 1, 2, 1, 0],
 [1, 2, 1, 0, 1, 2, 1],
 [2, 1, 0, 1, 0, 1, 2]]
```
  
[Answer]
# [R](https://www.r-project.org/), ~~72~~ 68 bytes
```
function(a,b)`[<-`(diag(2*a-1)*0+0:1,cbind(c(x<--b:b,-x),x)+a,2*!!b)
```
[Try it online!](https://tio.run/##BcHBCoAgDADQX8nbphuo3cI/6eKmGF4MosC/t/ee1bbEq32jvP0eIKSYz8QZapcLohUOaL3zR6CifVQoMBOzHko8kSY6oWiNUVwNdgq4fg "R – Try It Online")
Thanks to Robin Ryder for -4 bytes.
[Answer]
# [Charcoal](https://github.com/somebody1234/Charcoal), ~~22~~ 21 bytes
```
ＵＯ±Ｎ01¶10‖Ｏ⌈¿ＩηＰX×2⊕η
```
[Try it online!](https://tio.run/##FcoxDoIwFADQ3VM0nX6TmgBxgtGJQUiMg4NLqR9o0n6a8uEMDp7Si1RdX56dTbKL8Tn3g19ogg4nwwgtxY27LQyYQCktZFE@qCykag5XHD1a7ndM3kSoP@/XT90o4GxWhlkpcdk8u5gcMdR3LW4u4Aqyklq0ZBMGJMbnf6om55Oo8nH3Xw "Charcoal – Try It Online") Link is to verbose version of code. Explanation:
```
ＵＯ±Ｎ01¶10
```
Draw a chequerboard of size `a` with `0`s on its major diagonals above and to the left of the current position.
```
‖Ｏ⌈
```
Reflect it to produce a chequerboard of size `2a-1`, leaving the current position in the middle of the chequerboard. (Note that while drawing the chequerboard below and right would save a byte, there is no single byte to reflect up and left, and Charcoal's up reflect is buggy and moves the current position incorrectly anyway.)
```
¿Ｉη
```
Test whether `b` is zero.
```
ＰX×2⊕η
```
If not then draw arms of `2`s of length `b+1` (including the centre).
[Answer]
# [Python 3.8 (pre-release)](https://docs.python.org/3.8/), 82, 80 bytes (@ovs)
```
lambda a,b:(R:=range(1-a,a))and[[1&x+y|(x*x==y*y<=b*b>0)*2for x in R]for y in R]
```
[Try it online!](https://tio.run/##TY/LCsIwEEX3/YrQhSQ1glUEKcaP6La6mNhWA82DNIuE2m@Pra3grO7cA4cZE9xLq@PZ2NiyW@xA8hoQUF7gsmAW1LPB@Q4oEAKqrqp847fhjX3mGQtZuDCe8eueZIdWW@SRUKi8zzEsMSat1RIZY4VySEijrVu3hQjXWKd11//gQ0suFDihVZ/MJk5hdv33eLnrROiBFAma5mvEbToAG@nA2ZiSpV8Bnj4iJH4A "Python 3.8 (pre-release) – Try It Online")
[Old version](https://tio.run/##ZY9LCsIwEED3PUVwIUmNYCuCFONFqouJNhpoPqRZJNSePba2guDs5j14w9jon0bvj9YlwS6pBcXvgIDyqq6LddjEFw55YCzm8cR4zs87kpfCOBSQ1MiBfjS42AIFcp1o/KMpE84oZK2T2iOprHF@2WYjfeO8MW33lTejuNTgpdFdNkU5hSn7y/F840BoSaoMjfMpYrHqgQ2052xYkZkvAo8/EZLe "Python 3.8 (pre-release) – Try It Online")
Nothing too fancy, still learning the ropes. Should run on older python3s, too.
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E/wiki/Commands), ~~25~~ 24 [bytes](https://github.com/Adriandmen/05AB1E/wiki/Codepage)
```
Dи2.ý01ŽNœSΛĀ+Y87S.Λ.º.∊
```
Can probably be shortened with a different approach, but I wanted to try and use the Canvas builtin for this challenge.
-1 byte thanks to *@ovs*.
[Try it online.](https://tio.run/##AS8A0P9vc2FiaWX//0TQuDIuw70wMcW9TsWTU86bxIArWTg3Uy7Omy7Cui7iiIr//zcKMw)
**Explanation:**
```
D            # Duplicate the first (implicit) input-integer `a`
 и           # Create a list with `a` amount of `a`s
  2.ý        # Intersperse it with 2: [a,2,a,2,a,2,...,a]
     01      # Push "01"
       ŽNœ   # Push compressed integer 6020
          S  # Convert it to a list of digits: [6,0,2,0]
           Λ # Use the Canvas builtin with these three options:
             #  Length: [a,2,a,2,a,2,...,a]
             #  Characters to draw: "01"
             #  Directions: [6,0,2,0]
Ā            # Check if the (implicit) second input-integer `b` is NOT 0
 +           # Add this check to the (implicit) second input-integer `b`
             # (b=0 will remain 0; b≥1 is now b+1)
  Y          # Push "2"
   87S       # Push list [8,7]
      .Λ     # Use the modifiable Canvas builtin with these three options:
             #  Length: b+1 (or 0 if b=0)
             #  Characters to draw: "2"
             #  Directions: [8,7]
.º           # Intersected mirror the result towards the left
  .∊         # Intersected mirror the result downwards
             # (after which the result is output implicitly)
```
[See this 05AB1E tip of mine (section *How to compress large integers?*)](https://codegolf.stackexchange.com/a/166851/52210) to understand why `ŽNœ` is `6020`.
The Canvas Builtin uses three arguments to draw a shape:
* Length of the lines we want to draw
* Character/string to draw
* The direction to draw in, where each digit represents a certain direction:
```
7   0   1
  ↖ ↑ ↗
6 ← X → 2
  ↙ ↓ ↘
5   4   3
```
Here a step-by-step explanation of how it draws for example inputs \$a=5,b=2\$ with these steps:
Preparation: `Dи2.ý` makes the list `[5,2,5,2,5,2,5,2,5]`.
`Dи2.ý01ŽNœSΛ` for this input will have the following Canvas arguments:
* Length: `[5,2,5,2,5,2,5,2,5]`
* Characters: `"01"`
* Directions: `[6,0,6,2]`, which translate to \$[←,↑,→,↑]\$
Step 1: Draw 5 characters (`"01010"`) in direction `6←`:
```
01010
```
Step 2: Draw 2-1 characters (`"1"`) in direction `0↑`:
```
1
01010
```
Step 3: Draw 5-1 characters (`"0101"`) in direction `2→`:
```
10101
01010
```
Step 4: Draw 2-1 characters (`"0"`) in direction `0↑`:
```
    0
10101
01010
```
Step 5: Draw 5-1 characters (`"1010"`) in direction `6←`:
```
01010
10101
01010
```
Step 6: Draw 2-1 characters (`"1"`) in direction `0↑`:
```
1
01010
10101
01010
```
Step 7: Draw 5-1 characters (`"0101"`) in direction `2→`:
```
10101
01010
10101
01010
```
Step 8: Draw 2-1 characters (`"0"`) in direction `0↑`:
```
    0
10101
01010
10101
01010
```
Step 9: Draw 5-1 characters (`"1010"`) in direction `6←`:
```
01010
10101
01010
10101
01010
```
We then use the Canvas builtin again with three new arguments: `Ā+Y87S.Λ`
* Length: \$b+1\$ (or \$0\$ if \$b=0\$, because with length `1`, direction `8` will draw a `"2"` at the position it was at before the reset unfortunately)
* Characters to draw: `"2"`
* Direction: `[8,7]`, where the `8` is a special direction that resets the starting position (to the bottom-left we started at) and `7` translates to \$↖\$
Draw \$3\$ (\$b=2\$) characters (`"222"`) in direction `7↖`:
```
01010
10101
01210
10121
01012
```
After which we mirror this multi-line string both horizontally and vertically, after which it is output implicitly as our result.
[See this 05AB1E tip of mine for an in-depth explanation of the Canvas builtin.](https://codegolf.stackexchange.com/a/175520/52210)
[Answer]
# [MATL](https://github.com/lmendo/MATL), ~~22~~ 21 bytes
```
_Zvt&+oy&=bitg+>&+>E+
```
Inputs are `a`, then `b`.
[**Try it online!**](https://tio.run/##y00syfn/Pz6qrERNO79SzTYpsyRd205N285V@/9/Yy5DAA)
### Explanation
This uses inputs `3`, `1` as an example. The stack contents are shown in each step. The stack is shown upside down, with the top (i.e. most recent) element down.
```
_Zvt % Implicit input: a. Symmetric range [a a-1 ... 2 1 2 ... a-1 a]. Duplicate
     % STACK: [3 2 1 2 3],
              [3 2 1 2 3]
&+   % Duplicate. Add to itself transposed, elementwise with broadcast
     % STACK  [3 2 1 2 3],
              [6 5 4 5 6
               5 4 3 4 5
               4 3 2 3 4
               5 4 3 4 5
               6 5 4 5 6]
o    % Modulo 2, elementwise. Gives checkered matrix
     % STACK: [3 2 1 2 3],
              [0 1 0 1 0
               1 0 1 0 1
               0 1 0 1 0
               1 0 1 0 1
               0 1 0 1 0
y    % Duplicate second-top element
     % STACK: [3 2 1 2 3],
              [0 1 0 1 0
               1 0 1 0 1
               0 1 0 1 0
               1 0 1 0 1
               0 1 0 1 0],
              [3 2 1 2 3]
&=   % Equal? to itself transposed, elementwise with broadcast. Gives "X" matrix
     % STACK: [3 2 1 2 3],
              [0 1 0 1 0
               1 0 1 0 1
               0 1 0 1 0
               1 0 1 0 1
               0 1 0 1 0],
              [1 0 0 0 1]
               0 1 0 1 0
               0 0 1 0 0
               0 1 0 1 0
               1 0 0 0 1]
b    % Bubble up third-top element
     % STACK: [0 1 0 1 0
               1 0 1 0 1
               0 1 0 1 0
               1 0 1 0 1
               0 1 0 1 0],
              [1 0 0 0 1]
               0 1 0 1 0
               0 0 1 0 0
               0 1 0 1 0
               1 0 0 0 1],
              [3 2 1 2 3]   
i    % Input: b
     % STACK: [0 1 0 1 0
               1 0 1 0 1
               0 1 0 1 0
               1 0 1 0 1
               0 1 0 1 0],
              [1 0 0 0 1]
               0 1 0 1 0
               0 0 1 0 0
               0 1 0 1 0
               1 0 0 0 1],
              [3 2 1 2 3],
               1
tg+  % Duplicate, convert to logical, add. This converts positive b into b+1
     % STACK: [0 1 0 1 0
               1 0 1 0 1
               0 1 0 1 0
               1 0 1 0 1
               0 1 0 1 0],
              [1 0 0 0 1]
               0 1 0 1 0
               0 0 1 0 0
               0 1 0 1 0
               1 0 0 0 1],
              [3 2 1 2 3],
               2
>    % Greater than?, elementwise
     % STACK: [0 1 0 1 0
               1 0 1 0 1
               0 1 0 1 0
               1 0 1 0 1
               0 1 0 1 0],
              [1 0 0 0 1]
               0 1 0 1 0
               0 0 1 0 0
               0 1 0 1 0
               1 0 0 0 1],
              [1 0 0 0 1]
&+   % Add to itself transposed, elementwise with broadcast
     % STACK: [0 1 0 1 0
               1 0 1 0 1
               0 1 0 1 0
               1 0 1 0 1
               0 1 0 1 0],
              [1 0 0 0 1]
               0 1 0 1 0
               0 0 1 0 0
               0 1 0 1 0
               1 0 0 0 1],
              [2 1 1 1 2
               1 0 0 0 1
               1 0 0 0 1
               1 0 0 0 1
               2 1 1 1 2]
>    % Greater than?, elementwise. Equivalent to "and not". This trims the "X" matrix
     % STACK: [0 1 0 1 0
               1 0 1 0 1
               0 1 0 1 0
               1 0 1 0 1
               0 1 0 1 0],
              [0 0 0 0 0]
               0 1 0 1 0
               0 0 1 0 0
               0 1 0 1 0
               0 0 0 0 0],
E+   % Multiply by 2, element-wise. Add, element-wise. Implicit display
     % STACK: [0 1 0 1 0
               1 2 1 2 1
               0 1 2 1 0
               1 2 1 2 1
               0 1 0 1 0]
```
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 19 bytes
```
^þ
ÇḂ+UṚŒḄŒBɓa‘{Ç¬Ḥ
```
[Try it online!](https://tio.run/##y0rNyan8/z/u8D6uw@0PdzRphz7cOevopIc7Wo5Ocjo5OfFRw4zqw@2H1jzcseT/4eX67ppZjxr3cXH9/x9tpKNgEKujAKINQbQxlG@M4BuBaBOouAlU3AQhbhwLAA "Jelly – Try It Online")
With some inspiration from hyper-neutrino's Vyxal solution.
```
^þ     Monadic helper link: 
 þ     Table the (range from 1 to the) argument with itself by
^      XOR.
  Ḃ    The parities of the cells form a checkerboard,
  ¬    and the diagonal consists of zeroes while the rest is truthy.
ÇḂ+UṚŒḄŒBɓa‘{Ç¬Ḥ    Dyadic main link:
             Ç¬     Generate an identity matrix of size
         ɓ          b
           ‘        + 1
          a {       or 0 if b is 0,
               Ḥ    then double it.
  +                 Add that to (the top left corner of)
ÇḂ                  an a by a checkerboard,
   UṚ               rotate the result 180 degrees,
     ŒḄŒB           and palindromize it in both dimensions.
```
# [Jelly](https://github.com/DennisMitchell/jelly), ~~25~~ ~~24~~ ~~23~~ ~~22~~ 21 bytes
```
^þ
ÇḂ+ŒḄŒBðÇUoḶ>¥*¬ḤU
```
[Try it online!](https://tio.run/##y0rNyan8/z/u8D6uw@0PdzRpH530cEfL0UlOhzccbg/Nf7hjm92hpVqH1jzcsST0/@Hl@u6aWY8a93Fx/f8fbaSjYBCrowCiDUG0MZRvjOAbgWgTqLgJVNwEIW4cCwA "Jelly – Try It Online")
That feels better. \$a\$ on the left, \$b\$ on the right.
```
^þ     Monadic helper link: 
 þ     Table the (range from 1 to the) argument with itself by
^      XOR.
  Ḃ    The parities of the cells form a checkerboard,
  ¬    and the diagonal consists of zeroes while the rest is truthy.
ÇḂ+ŒḄŒBðÇUoḶ>¥*¬ḤU    Dyadic main link:
        Ç             Generate an a by a matrix of zeroes on the diagonal and nonzeroes elsewhere.
         U            Mirror it,
          o           and replace zeroes with corresponding elements from:
           Ḷ ¥        for each 0 .. a-1,
            >         is it greater than b?
              *       Raise each to the bth power,
               ¬      logically negate,
                Ḥ     double,
                 U    and mirror back.
  +    ð              Add that to
ÇḂ                    an a by a checkerboard,
   ŒḄŒB               then palindromize in both dimensions.
```
[Answer]
# [J](http://jsoftware.com/), 40 bytes
```
((2|+/~)@]+2**@[*=/~@]*(>:]*=/~))|@i:@<:
```
[Try it online!](https://tio.run/##y/qvpKeepmBrpaCuoKNgoGAFxLp6Cs5BPm7/NTSMarT16zQdYrWNtLQcorVs9escYrU07KxiQUxNzRqHTCsHG6v/mlypyRn5QJ1pCkYQprqurq46hGkIFDXGEDUCipr8BwA "J – Try It Online")
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 18 bytes
```
_þµṂ>Ɠa~¤×¬Ḥ+ḂŒBŒḄ
```
[Try it online!](https://tio.run/##ATEAzv9qZWxsef//X8O@wrXhuYI@xpNhfsKkw5fCrOG4pCvhuILFkkLFkuG4hP/Dh0f/Mv81 "Jelly – Try It Online")
-1 from caird, who noted that I could get rid of a backtick by reading `b` from stdin.
```
                For inputs (a, b):
_þµ              Make a subtraction table over [1..a], and operate on that.
    Ṃ             Get the minimum row: this is always [1-a, 2-a ... 0].
     >Ɠa~¤        Which elements are greater than (b and ~b)?
                  For example for (5, 2): Ṃ    → [-4, -3, -2, -1,  0]
                                          Ṃ>-3 → [ 0,  0,  1,  1,  1]
          ×¬      Multiply this vector by the a×a identity matrix "¬table"
            Ḥ     and double the result (generating the 2s)
             +Ḃ   then add to an a×a checkerboard of 1s "table % 2":
                    0 0 0 0 0   0 1 0 1 0   0 1 0 1 0
                    0 0 0 0 0   1 0 1 0 1   1 0 1 0 1
                    0 0 2 0 0 + 0 1 0 1 0 → 0 1 2 1 0
                    0 0 0 2 0   1 0 1 0 1   1 0 1 2 1
                    0 0 0 0 2   0 1 0 1 0   0 1 0 1 2
ŒBŒḄ              Finally, palindromize both ways to make the full output.
```
[Answer]
# [Ruby](https://www.ruby-lang.org/), 70 bytes
```
->a,b{(r=1-a...a).map{|x|r.map{|y|x*x==y*y&&x*x<=b*b&&b>0?2:(x+y)%2}}}
```
[Try it online!](https://tio.run/##KypNqvyfZvtf1y5RJ6lao8jWUDdRT08vUVMvN7GguqaipgjCqKyp0Kqwta3UqlRTA7JsbJO0ktTUkuwM7I2sNCq0KzVVjWpra/8XlJYUK6RFG@kYxIL0aahZZeYVF6Qml2hygaS4oPKmBORNdAzxypvrGGPI/wcA "Ruby – Try It Online")
[Answer]
# Zsh, ~~127~~ ~~116~~ 112 bytes
```
R=({1..$[c=2*$1-1]})
for i ($R)(for j ($R)S+=$[(i==j)|(j+i==c+1)?$2==0?0:${$((i-$1))/\-/}>$2?0:2:(i+j)%2]
<<<$S)
```
[Attempt this Online!](https://ato.pxeger.com/run?1=LY1LDoJADIb3c4pZ1KQVUaawMITKHWAJLpRIgA3Gx8bHSdyQGA7FbRzARft9bZP-n-_jWnVDXLYXbTRrVKw9W0b5lv5EVoH1wN5tGwdfEUZRdBAwq1wfBbi_30p3O5wTwadZryErhJdgXLN_kxp_1xohIRy1mTR1BDKsRRp6YeNYKRxDMbCIF3shPAGxdsEQbXJ3894B2y2HWDsNLXivbD6kNOf2p6Jq_951M38)   ~~[116 bytes](https://tio.run/##RY29EoIwEAZ7nuKKcyYR@clBBRN5EKTQjBmSQhy0MubZY4jO2Oztt829HnPQjLvsquYFzhLFAS4SKSiJI@1RFGLq9bKCAeZEWaLy3G3b/vd9NbenBhyZkdLyN7N5FJULPiBJWQ91hw4ZMwUKzqtTUfkjUqzUMZNbvqOp3977sDHzmQaCOlFENsmbn1Nkm0oLAtL5piZ8AA)~~   ~~[127 bytes](https://tio.run/##TY69EoIwEAb7PMUV50xO5CeBipnIgyCFZmRICnHQBmOePYZoYbP37Vb3ekxh5OTYVU8znBWKA1wUyqAV9nKPIhcDG@cFDHAnigK1J7e5/fNVIecolaq6qkW3icgNUXnKS39EGaskYvfF3J4jYM@NUpbe3GZx6ExQt7bcZJZ2cvDpEx82Ms9GkFAlisg67fq3ZWSTSgMC0vmmOnwA)~~
I used bits of the [X without Y](https://codegolf.stackexchange.com/a/233518/15940) problem.
  
@pxeger saved 4 bytes!
**Description**
```
Using `a = $1` and `b = $2` as defined in the question...
R=({1..X})               define array R as a sequence from 1 to expression X
c=$2*$1-1                size of matrix
for i.. for j..          iterate rows, columns 
S+=$[...]                add a digit to S, per the logic in [...]
(i==j)|(j+i==c+1)?...    are we on a diagonal? if so, do ...
:(i+j)%2                 otherwise, fill the matrix with 1s and 0s
$2==0?0                  if b is 0, then default to 0 on the diagonal
:${$((i-$1))/\-/}>$2?0:2 if abs(i-a) > b then put 0 on the diagonal, otherwise 2
```
[Answer]
# [C (gcc)](https://gcc.gnu.org/), ~~118~~ \$\cdots\$ ~~106~~ 103 bytes
```
i;f(a,b){for(a*=2,i=--a*a;i--;)printf("%d%c",!b|b<abs(a/2-i/a)||i/a-i%a&&i/a+1+i%a-a?i%2:2,i%a?32:10);}
```
[Try it online!](https://tio.run/##ZY/BCsIwDIbvPkUcVFrXoFa82BVfxEtamRR0igoe3F7dmiJTh//hTxq@/G0D7kNIKdpakvbqUZ8ukqbO6OgQaUo2Ilp1vsTmVstC7EQo9Ni3viJ/lTQzGGek2pYdo6DJhJtyUXKLtInCrDlI0GZp1ou5sl3iGDhSbKSCxwhYfB/IPCVwYCyXysHKQllSjwwwD25u2SugDHn1Qb5wVv/iHCt2Ou9x3TaFBuKTsgM6f/5v2EdsGxyIQ37IbvT2Lj1DfaD9NeH9BQ "C (gcc) – Try It Online")
*Saved 3 bytes thanks to [ceilingcat](https://codegolf.stackexchange.com/users/52904/ceilingcat)!!!*
[Answer]
# [Vyxal](https://github.com/Vyxal/Vyxal), 24 bytes
```
:±+ʁ:±†vǔd$ʁ:v+∷+2(ṘømÞT
```
[Try It Online!](https://vyxapedia.hyper-neutrino.xyz/tio#WyIiLCI6wrEryoE6wrHigKB2x5RkJMqBOnYr4oi3KzIo4bmYw7htw55UIiwiKSB24bmE4oGLIiwiIiwiMlxuNCJd)
-3 bytes thanks to lyxal  
-1 byte thanks to Unrelated String
```
:±+ʁ:±†vǔd$ʁ:v+∷+2(ṘømÞT     Full Program; take in order b, a
:                            duplicate b
 ±                           sign
  +                          add (0 if b is 0, otherwise b+1)
   ʁ                         range (0..x-1)
    :                        duplicate
     ±                       sign ([0, 1, 1, ..., 1] of length 0 / b+1)
      †                      logical NOT - [1, 0, 0, ..., 0]
       vǔ                    vectorized rotate; identity matrix of size 0 / b+1
         d                   double; [2, 0, 0], [0, 2, 0], [0, 0, 2]
          $                  swap a to TOS
           ʁ                 range; 0..a-1
            :                duplicate
             v+              add each; addition table; [2, 3, 4], [3, 4, 5], [4, 5, 6]
               ∷             parity; checkerboard of size a with 0 in top-left
                +            add the two matrices together
                  2(....)    repeat twice
                    Ṙ        reverse (flip vertical)
                     øm      palindromize (reflect vertical; do not copy middle)
                       ÞT    transpose
```
[Answer]
# Excel, 97 bytes
```
=LET(x,SEQUENCE(A1*2-1)-A1,y,TRANSPOSE(x),IF(MOD(x+y,2),1,IF((x-y)*(x+y)+(B1=0)+(ABS(x)>B1),,2)))
```
[Link to Spreadsheet](https://1drv.ms/x/s!AkqhtBc2XEHJnQVfT5BM4NBPJs1G?e=8lgz25)
## Explanation
```
=LET(x,SEQUENCE(A1*2-1)-A1, # [-a..a] vertical
y,TRANSPOSE(x),             # [-a..a] horizontal
IF(MOD(x+y,2),1,            # if x+y mod 2 <> 0 then 1
  IF((x-y)*(x+y)+(B1=0)+(ABS(x)>B1),,2)))  
                   # else if (x<>y and x<>-y) or b = 0 or abs(x) > b then 0 else 2 
```
[Answer]
# [Julia](https://www.julialang.org/), ~~336~~ ~~271~~ ~~195~~ 187 Bytes
v4, and thanks for the help!
```
function x(a,b)
s=(2*a-1)
h = zeros(Int8,(s,s))
h[2:2:s^2].=1
if b!=0;h[a,a]=2end
for i in range(1,stop=min(b,a-1)) 
h[a+i,a+i]=2
h[a-i,a-i]=2
h[a-i,a+i]=2
h[a+i,a-i]=2
end
return(h)
end
```
old:
```
function cross(a,b)
    h = zeros(Int8,((2*a-1),(2*a-1)))
    h[2:2:(2*a-1)^2] .= 1
    c = (Int8((2*a-1)/2+0.5), Int8((2*a-1)/2+0.5))
    h[c[1],c[2]] = 2
    for i in range(1,stop=min(b,a-1))
        h[c[1]+i,c[2]+i] = 2
        h[c[1]-i,c[2]-i] = 2
        h[c[1]-i,c[2]+i] = 2
        h[c[1]+i,c[2]-i] = 2
    end
    return(h)
end
```
[Answer]
# JavaScript (ES6), ~~87~~ 84 bytes
Expects `(a)(b)`.
```
a=>b=>[...Array(a+--a)].map((_,y,A)=>A.map(_=>x+y&1|(x*x/b<=b&x*x--==y*y)*2,y-=x=a))
```
[Try it online!](https://tio.run/##NY7NboMwEAbveQqfwi7YTgPcmrXEoU9BrGhNID9KceREFZb67jRAc5pvZi975R9@NOFyf6reH9uxo5HJODK11roKgSNwphSj1d98BzjIKCskU816IDNkcb39hSEdNm5Hbv0aShHFNGKay6hoIEYcP@s6lx9Wihe2E4rFirflE8ollkss37GwdqU7H764OQPULIWzKMiIxvcPf2v1zZ@gA0ZwOP8VpmPQV3/pIREJ4v/c9wmKTMzE8Q8 "JavaScript (Node.js) – Try It Online")
### Note
We do `x*x/b<=b` rather than `x*x<=b*b` so that `x*x/b` evaluates to `NaN` when both \$x\$ and \$b\$ are \$0\$. This makes the condition fail as expected for this edge case without the need for an explicit test on \$b\$.
[Answer]
# TI-Basic, 80 bytes
```
Prompt A,B
identity(2A-1→[A]
For(I,1,2A-1
For(J,1,2A-1
remainder(I+J,2)+2max(abs({I,J}-A)≤B and B)max(I={J,2A-J→[A](I,J
End
End
```
+1 byte if not ran on a TI-84+/SE with the 2.53 MP OS or above by replacing `remainder(I+J,2)` with `2fPart(.5(I+J))`.
Output is stored in `[A]`.
[Answer]
# APL+WIN, 72 bytes
Prompts for a then b:
```
(s⊖(s←.5×b-a)⌽z↑(b≠1)×2×m∨⌽m←n∘.=(n←⍳b←(2×b←⎕)+1))+(z←a,(a←(2×⎕)-1))⍴0 1
```
Explanation:
```
(z←a,(a←(2×⎕)-1))⍴0 1 create the 0 1 checkerboard matrix defined by a
2×m∨⌽m←n∘.=(n←⍳b←(2×b←⎕)+1)) create the 2s diagonals defined by b
(b≠1)× filter the case where b=0
z↑ pad the diagonals matrix to the same size as the checkerboard matrix
s⊖(s←.5×b-a)⌽ centre the diagonals in the padded diagonals matrix. 
The two matrices are then summed together.
```
[Try it online! Thanks to Dyalog Classic](https://tio.run/##SyzI0U2pTMzJT9dNzkksLs5M/v@ob6qvz6O2CcZcQJanP5BlyPWooz3tv0bxo65pQKJtgp7p4elJuomaj3r2Vj1qm6iR9KhzgaHm4elGh6fnPupYARTOBarKe9QxQ89WIw/IfNS7OQlIaQAVgGigwZrahpqa2hpA7RMSdTQSoZIgCV2gxKPeLQYKhv@B1nL9T@My4jLgUldQ5wKxDKEsY7iYMVAUwjKBi5nA1ZkgyRpzcQEA "APL (Dyalog Classic) – Try It Online")
[Answer]
# [C (clang)](http://clang.llvm.org/), 92 bytes
```
z,i;f(a,b){for(i*=z=i=2*a-1;i;)printf("\n%d"+!!(i--%z),i%~z&&i%~-z||(i/z-~-a)/~b|!b?i%2:2);}
```
[Try it online!](https://tio.run/##VU9JDsIwDLznFWmkIIcmLD1wII34SC8OVZElCIjlki5fL4mQEMzBssbj8fhojmcMp3mOmmwHqL3qu@sdaOmiI1ct0WwtWXW7U3h2IJogW1EWBZAxMipNcoqLRaomDgPQOprJoFpPfij8gWS1r5Qd57TLL0iBg@pZsueQGXSVxXpnyxIV41/au431NSY6ZWGcf05zEB3IVstWrRKaYH4hdE5uk/rzQ@5ur@cjB/4TNkHk4cjYOL8B "C (clang) – Try It Online")
Function printing to stdout.  
Leading line can be removed at the cost of 1 Byte.
```
z : side length 
i : characters counter(backwards)
f(a,b){
for(i*=z=i=2*a-1;i;)    - initialize variables and itetate backwards until 0
printf("\n%d"+!!(i--%z) - prints:
         > a newline if i%z == 0
           > the number obtained by expression below at each iteration (decrements i)
i%~z&&i%~-z        - false on diagonals
||(i/z-~-a)/~b|!b  -  false on horizontal window
     * for example f(3,1)
    
  01110    11111   11111
  10101    00000   10101
  11011 || 00000 = 11011
  10101    00000   10101
  01110    11111   11111
?i%2:2    - draw board on 1's
                 2's on zeros 
```
[Answer]
# Matlab, 79 bytes
```
m=zeros(2*a-1);
m(2:2:end)=1;
for i=a-b:a+b
m(i,i)=2*(b>0);end
max(m,flipud(m))
```
Only make one diagonal of twos and generate the antidiagonal by maxing with the flipped matrix. The `2*(b>0)` was my shortest way to take care of the `b=0` edge case.
]