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:
- the unit cell
- then, duplicate it 15 times to build a stack.
- 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. |
+++ IJ snippet +++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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(); |
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:
- Duplicate once the unit cell and create a 2-slices stack.
- Scale this stack by setting the Z-scale to 8 (or Depth to 16). Your 16-slices stack is now created.
+++ IJ snippet +++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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"); |
Hope that helps.
No comments:
Post a Comment