
The program makes for an interesting screensaver, but does it follow any pattern, or illustrate any special properties of √2 ? Though I'm no mathematician, from what I've read this seems unlikely. √2 is suspected (but not proved) to be a normal number. This would mean the digits of its expansion (respective to some number base) follow a uniform distribution; no digit would be more likely to appear than any other.
Nonetheless, I'd be curious to see the results of a really long run!
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
import Control.Arrow ((***)) | |
import Control.Monad (join, liftM2, liftM3, when) | |
import Data.Bits (shift) | |
import Data.Maybe (fromJust) | |
import Graphics.UI.SDL as SDL | |
(xres, yres) = (1600, 900) | |
main = withInit [InitVideo] $ do | |
w <- setVideoMode xres yres 32 [NoFrame] | |
s <- createRGBSurface [SWSurface] 1 1 32 0 0 0 0 | |
enableEvent SDLMouseMotion False | |
setCaption "Sqrt 2 Curve" "Sqrt 2 Curve" | |
run w s (xres `div` 2, yres `div` 2) dirs rgbs [1..] | |
run w s p (f:fs) (c:cs) (n:ns) = do | |
drawCell w s p c | |
when (n `mod` 47 == 0) $ SDL.flip w | |
e <- pollEvent | |
case e of | |
KeyUp (Keysym SDLK_ESCAPE _ _) -> save >> print n | |
_ -> run w s (f p) fs cs ns | |
where | |
save = saveBMP w "out.bmp" | |
drawCell w s (x,y) c = draw x y | |
where | |
rect x y = Just $ Rect x y 1 1 | |
draw x y = do fillRect s (rect 0 0) (Pixel c) | |
blitSurface s (rect 0 0) w $ rect x y | |
sqrt2 = let ns = f 2 0 in concatMap (show . (ns !!)) [0..] | |
where | |
f x r = d : f (100 * (x - (20 * r + d) * d)) (10 * r + d) | |
where | |
d = head (dropWhile p [0..]) - 1 | |
p n = (20 * r + n) * n < x | |
dirs = map f . expand $ map numPad sqrt2 | |
where | |
f (x,y) = (x+) *** (+y) | |
rgbs = expand . cycle . map f $ liftM3 (,,) ns ns ns | |
where | |
ns = [71, 77.. 255] | |
f (r,g,b) = shift r 16 + shift g 8 + b | |
numPad = fromJust . (`lookup` pad) | |
where | |
pad = zip "0528639417" $ head ps : ps | |
ps = (join $ liftM2 (,)) [0, 1, -1] | |
expand (x:xs) = replicate 8 x ++ expand xs |
No comments:
Post a Comment