Thursday, October 27, 2011

Drawing a chessboard using boolean operators



A small use of boolean operators to draw my favorite chessboard and also a good example to use other functions like geometrical transforms.


This algorithm is based on two interleaved perpendicular stripes images as shown in Fig. 1. The first part of the algorithm consists of creating the horizontal stripes (Fig. 1A) and then, to combine this image with its 90° rotated (Fig.1B) clone to yield the famous chessboard.

Fig.1: Drawing a chessboard from stripes.
1- Creating the horizontal stripes.
newImage("stripes-1", "8-bit Black", 1, 8, 1);
for (i=0;i<8;i+=2)
{
  setPixel(0,i,255);
}
run("Size...", "width=256 height=256 interpolation=None");

The above script creates a 1x8 black image and even pixels are set to 255 (white). Then, this small image is rescaled to 256x256 leading to the stripes of Fig. 1A.
2- Creating the perpendicular stripes
run("Duplicate...", "title=stripes-2");
run("Rotate 90 Degrees Right");

In these two lines of code, the image with vertical stripes of Fig. 1B is obtained by duplicating stripes-1 (the horizontal stripes) and by applying a 90° rotation.
3- Combining both images
Now, we have to combine these two images. They are binary images [see post] and thus, we have three kind of operators: AND, OR, and XOR [see post]. Depending of the pixel values of stripes-1, stripes-2, and the expected chessboard, we can fill the following array:

strip-1 strip-2 chessboard
TRUE    TRUE    FALSE (found in top right corner)
TRUE    FALSE   TRUE   (found in top left corner)
FALSE   TRUE    FALSE (found in bottom right corner)
FALSE   FALSE   FALSE (found in bottom left corner)

This array is characteristic of a XOR operator. Finally, just add these two following lines to complete our script...

imageCalculator("XOR create", "stripes-1","stripes-2");
rename("chessboard");

No comments:

Post a Comment