Thursday, November 10, 2011

Drawing a chessboard from a 2x2 unit cell



In my series Drawing a chessboard, a simple way to build the chessboard from a 2x2 unit cell. 



As shown in Fig. 1, a chessboard can be described as a 4x4 array of unit cells composed of 2x2 black and white cells. Thus, this algorithm computes:
  1. the unit cell  
  2. then, duplicate it 15 times to build a stack.
  3. finally, create a montage of 4 rows and 4 columns.
Fig.1: A chessboard is the repetition of a 2x2 unit cell as shown in the top left corner.
Here is the script...

+++ IJ snippet +++
//
// Chessboard from a 2x2 unit cell
// Jean-Christophe Taveau
// http://crazybiocomputing.blogspot.com
//
// 1- Unit cell
newImage("unitCell", "8-bit White", 2, 2, 1);
setPixel(0,1,0);
setPixel(1,0,0);
run("Size...", "width=64 height=64 constrain average interpolation=None");
// 2- Stack creation
for (i=1;i<16;i++)
{
run("Duplicate...", "title=unitCell-"+i);
}
run("Images to Stack", "method=[Copy (center)] name=chess title=unitCell use");
// 3- Montage
run("Make Montage...", "columns=4 rows=4 scale=1 first=1 last=16 increment=1 border=0 font=12");
exit();
view raw gistfile1.js hosted with ❤ by GitHub
+++ End of IJ snippet +++

Duplicating a great number of the same 2x2 unit cell can be problematic because a lot of windows are created and your OS may have some trouble to manage all these windows. This can be improved by using the Scale operator. Indeed, when detecting a stack, this operation offers the possibility to modify the Z-scale. Thus, our algorithm becomes:
  1. Duplicate once the unit cell and create a 2-slices stack.
  2. Scale this stack by setting the Z-scale to 8 (or Depth to 16). Your 16-slices stack is now created.

+++ IJ snippet +++
//
// Chessboard from a 2x2 unit cell
// Jean-Christophe Taveau
// http://crazybiocomputing.blogspot.com
//
// 2- Stack creation
run("Duplicate...", "title=unitCell-1");
run("Images to Stack", "method=[Copy (center)] name=chess title=unitCell use");
run("Scale...", "x=- y=- z=- width=64 height=64 depth=16 interpolation=None create");
view raw gistfile1.js hosted with ❤ by GitHub
+++ End of IJ snippet +++

Hope that helps.

No comments:

Post a Comment