(This is my 200th post! =)
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)
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. 🙂
Yep, I fixed it up, no worries. =) You can just surround code with <pre><code> … </code></pre>.
The one on the left is just counting in binary. Is the one on the right a Gray code?
Right you are!
I made something pretty using Gray code:
Cool! Can you explain how you made it?
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
(I forgot that I had already posted bigger versions on G+.)
Ah, I see. Neat!
Comments are closed.
Enter your email address to follow this blog and receive notifications of new posts by email.
Join 590 other followers
Brent's blogging goal