Guest User

Untitled

a guest
Dec 12th, 2015
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import Graphics.UI.GLFW
  2. import Graphics.Rendering.OpenGL
  3. import Control.Monad
  4.  
  5. import Data.IORef
  6. import System.Exit
  7. import Data.Function
  8. import Control.Concurrent
  9.  
  10. title = "Sierpinsky triangle"
  11.  
  12. width, height :: GLsizei
  13.  
  14. width = 700
  15. height = 600
  16.  
  17. w2, h2 :: GLfloat
  18.  
  19. w2 = (fromIntegral  width) / 2
  20. h2 = (fromIntegral  height)  / 2
  21.  
  22. dw2, dh2 :: GLdouble
  23.  
  24. dw2 = fromRational $ toRational w2
  25. dh2 = fromRational $ toRational h2
  26.  
  27. white = Color4 (1::GLfloat) 1 1 0
  28. black = Color4 (0::GLfloat) 0 0 1
  29. red = Color4 (1::GLfloat) 0 0 1
  30.  
  31. triangle ax ay bx by cx cy = renderPrimitive Triangles $ do
  32.   vertex2f ax ay
  33.   vertex2f bx by
  34.   vertex2f cx cy
  35.  
  36. clearCenters [] = return []
  37. clearCenters (x@(ax, ay, bx, by, cx, cy):xs) = do
  38.   let a1x = ax
  39.       a1y = ay
  40.       b1x = (ax + bx) / 2
  41.       b1y = (ay + by) / 2
  42.       c1x = (cx + ax) / 2
  43.       c1y = (cy + ay) / 2
  44.       a2x = b1x
  45.       a2y = b1y
  46.       b2x = bx
  47.       b2y = by
  48.       c2x = (cx + bx) / 2
  49.       c2y = (cy + by) / 2
  50.       a3x = c1x
  51.       a3y = c1y
  52.       b3x = c2x
  53.       b3y = c2y
  54.       c3x = cx
  55.       c3y = cy
  56.   triangle b1x b1y c1x c1y c2x c2y
  57.   cleared <- clearCenters xs
  58.   return $ if length xs > 5000 then (x:xs) else
  59.            (x : (a1x, a1y, b1x, b1y, c1x, c1y):(a2x, a2y, b2x, b2y, c2x, c2y):
  60.         (a3x, a3y, b3x, b3y, c3x, c3y):cleared)
  61.  
  62. vertex2f a b = vertex (Vertex3 a b 0)
  63.  
  64. main = do
  65.   initialize
  66.   openWindow (Size width height) [] Window
  67.   windowTitle $= title
  68.   clearColor $= Color4 1 1 1 1
  69.   windowCloseCallback $= exitWith ExitSuccess
  70.   windowSizeCallback  $= (\size -> viewport $= (Position 0 0, size))
  71.   ortho (-dw2-50) (dw2+50) (-dh2-50) (dh2+50) (-1) 1
  72.   let       ax = -w2
  73.             ay = -h2
  74.             bx = w2
  75.             by = -h2
  76.             cx = 0
  77.             cy = h2 * tan (pi / 3) - h2
  78.   triangles <- newIORef [(ax, ay, bx, by, cx, cy)]
  79.   let loop = do
  80.         clear [ColorBuffer]
  81.         color black
  82.         triangle ax ay bx by cx cy
  83.         color white
  84.         xs <- readIORef triangles
  85.         clearCenters xs >>= writeIORef triangles
  86.         swapBuffers
  87.         threadDelay 50000
  88.         loop
  89.  
  90.   loop
Advertisement
Add Comment
Please, Sign In to add comment