Friday, September 2, 2011

Drawing a chessboard v2.1

This version of my series Drawing a chessboard is a minor update of the version 2.0 and is only different because it contains one loop to scan the image.


In this version, I create an 8x8 image with a black background and assign the white color by scanning this image in ONE loop. Indeed, there is 64 cells in the chessboard and they can be numbered from 0 (the top left cell) to 63 (the bottom right). The question is now: How can we extract the XY-coordinates from these indexes?
In the following figure, on the left, the chessboard contains the cell indexes. For example, the cell # 13 has XY-coordinates (5,1).

Now, the formula to calculate the cell index from the XY-coordinates is given above and corresponds to the Y-coordinate multiplied by the image width plus the X-coordinate.Ex: 13 = 1 * 8 + 5
And to extract the coordinates,
- X is calculated from the remainder after division by the image width.
- Y is the result of the integer division of the cell index by the image width.

Here is the final code...
+++ IJ snippet: chessboard_v2_1_imagej.ijm +++
+++ End of IJ snippet +++

No comments:

Post a Comment