## Post without words #3

(This is my 200th post! =)

This entry was posted in counting, pattern, pictures, posts without words, recursion. Bookmark the permalink.

### 11 Responses to Post without words #3

1. Mister Globules says:
``````
import Data.Bits
import Numeric
import Text.Printf

toGC n = (n `shiftR` 1) `xor` n

base2 n = showIntAtBase 2 ("01" !!) n ""

printGC w n = do
let n' = toGC n
printf "%0*s %0*s\n" w (base2 n) w (base2 n')

main = forM_ [0..31 :: Int] \$ printGC (5 :: Int)
``````
• Brent says:

=D

• Mister Globules says:

I indented the code by four spaces, as I thought you were using Markdown, but it looked terrible. Did you fix it up? If so, thanks!

Also, I like the seasonal colours in the diagram.

• Brent says:

Yep, I fixed it up, no worries. =) You can just surround code with <pre><code> … </code></pre>.

2. Lucas Brown says:

The one on the left is just counting in binary. Is the one on the right a Gray code?

• Brent says:

Right you are!

• Brent says:

Cool! Can you explain how you made it?

• Yes.

• For each pixel, put its x,y coordinates in Gray code; deal out the bits to the color-channels, in one of a large number of possible ways; convert the result from Gray code. Gray code is not essential to the concept, but makes it prettier.

Thus for example the (Gray) coordinates’ bits might be
X = G7 B7 R6 G5 B5 R4 G3 B3 R2 G1 B1 R0
Y = R7 G6 B6 R5 G4 B4 R3 G2 B2 R1 G0 B0
or in other words the (Gray) color bits are
R = Y11 X9 Y8 X6 Y5 X3 Y2 X0
G = X11 Y10 X8 Y7 X5 Y4 X2 Y1
B = X10 Y9 X7 Y6 X4 Y3 X1 Y0