1

I would like to write a generalized function to create a checkerboard pattern of any size (width of each square and frequency of repetition).

How can I do so?

Edit 1

This is what I tried.

check = ConstantArray[0, {256, 256}];
Table[Table[check[[k, l]] = 255, {k, i, i + 8}, {l, j, j + 8}], {i, 1,
   256, 16}, {j, 1, 256, 16}]
J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
user36426
  • 3,335
  • 11
  • 29

5 Answers5

3
a = 2;
n = 4;
x0 = 0;
y0 = 0;
g = Graphics[
  Outer[{
    RGBColor[{1, 1, 1} Mod[Plus[##], 2]],
    Rectangle[{x0, y0} + a List[##], {x0 + a, y0 + a} + a List[##]]
    } &,
  Range[0, (n - 1)],
  Range[0, (n - 1)],
  1
  ],
  PlotRangePadding -> None
 ]

enter image description here

You can apply Rasterize to obtain an image. Or you can use something like

Image@ArrayResample[
  Outer[
   N@Mod[Plus[##], 2] &,
   Range[0, n - 1],
   Range[0, n - 1]
   ],
  {400, 400}, "Bin", Antialiasing -> False, Resampling -> "Constant"]
Henrik Schumacher
  • 106,770
  • 7
  • 179
  • 309
  • Thanks for your answer. However, this is a Graphics object and I want an image. Secondly, there is a white border around the texture, which I don't want. – user36426 Apr 05 '18 at 11:32
  • 2
    @Majis, However you do not have requested an image in your statement. As a starting point is useful. I am sure you will be able to create an image from the graphic object... – José Antonio Díaz Navas Apr 05 '18 at 11:54
3

Specify n and m (number of blocks desired) and the overall ImagSize. This creates two sequences of +/-1's and then plots the Outer product of the two:

n = 5; m = 7;
Image[Outer[Times, (-1)^Range[n], (-1)^Range[m]], ImageSize -> 500]

enter image description here

bill s
  • 68,936
  • 4
  • 101
  • 191
2

I needed to to this as part of an upcoming project. The context was in the visualization of complex mappings, which is why the following code uses complex numbers:

(* adapted from https://mathematica.stackexchange.com/a/7359 *)
complexGrid = Compile[{{xmin, _Real}, {xmax, _Real}, {xn, _Integer},
                       {ymin, _Real}, {ymax, _Real}, {yn, _Integer}},
                      Block[{rx, ry},
                            rx = xmin + (xmax - xmin) Range[0, xn - 1]/(xn - 1);
                            ry = ymin + (ymax - ymin) Range[yn - 1, 0, -1]/(yn - 1);
                            Outer[Plus, I ry, rx]]];

complex2bw = Compile[{{Z, _Complex}, {sp, _Real}},
                     Mod[Round[Mod[Im[Z]/(2 sp), 1]] + Round[Mod[Re[Z]/(2 sp), 1]], 2], 
                     RuntimeAttributes -> {Listable}];

and thus,

Image[complex2bw[complexGrid[-4, 4, 500, -2, 2, 250], 1/2]]

checkerboard

yields a $500\times 200$ checkerboard image.

Change the spacing parameter:

Image[complex2bw[complexGrid[-4, 4, 500, -2, 2, 250], 2]]

checkerboard again

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
1

This is a way to design the checkerboard pattern I need.

Manipulate[
 check = ConstantArray[0, {nrow, ncol}];
 Table[check[[i, j]] = Mod[i + j, 2], {i, 1, nrow, 1}, {j, 1, ncol, 
   1}];
 ImageResize[Image[check], {height, width}], {nrow, 1, height, 
  1}, {ncol, 1, width, 1}, {height, 1, 512, 1}, {width, 1, 512, 1}]
user36426
  • 3,335
  • 11
  • 29
1

You can specify the size of each cell using ImageSize-> 1 -> s:

board = Image[Raster[(-1)^Array[+##&, {#, #2}]], ImageSize-> 1 -> #3] &;

The overall size of board[r, c, s] with r rows, c columns (r c cells each of size s) is {c s, r s}:

board[7, 11, 20]// Rasterize[#, "RasterSize"]&

{220, 140}

board[7, 11, 20]

enter image description here

board[7, 11, 50]// Rasterize[#, "RasterSize"]&

{550, 350}

board[7, 11, 50]

enter image description here

Note: See this answer by rm-rf regarding the usage ImageSize -> 1 -> s.

kglr
  • 394,356
  • 18
  • 477
  • 896