Guest User

Untitled

a guest
Feb 21st, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. import Graphics.UI.Gtk hiding (fill)
  2. import Graphics.Rendering.Cairo
  3. import Data.Time.Clock.POSIX
  4. import Time
  5.  
  6. frac = snd . properFraction
  7.  
  8. modf a b = frac (a / b) * b
  9.  
  10. normalizeAngle a | a < 0 = 2*pi + (a `modf` (2*pi))
  11. normalizeAngle a = a `modf` (2*pi)
  12.  
  13. floorf = fromInteger . fst . properFraction
  14.  
  15. angularDistance a b =
  16. f (na - nb)
  17. where na = normalizeAngle a
  18. nb = normalizeAngle b
  19. f a | a > pi = a - 2*pi
  20. f a | a < -pi = a + 2*pi
  21. f a = a
  22.  
  23. cylinderProjection r (x, y) =
  24. (mx * 200/z, my * 200/z)
  25. where mx = r * sin (x/r)
  26. my = y
  27. z = 400 + r + r * cos (x/r)
  28.  
  29. scaleP f (x,y) = (x*f, y*f)
  30. translateP u v (x,y) = (x+u, y+v)
  31. rotateP a (x,y) = (cos a * x - sin a * y, sin a * x + cos a * y)
  32.  
  33. gon n =
  34. map nrot [0..n-1]
  35. where nrot i = let a = 2*pi*i/n in
  36. (cos a, sin a)
  37. hexagon = gon 6
  38.  
  39. drawHexagon col rot r rows i = do
  40. let y = if (floor i) `mod` 2 == 0
  41. then 0
  42. else 1.732
  43. let transform = cylinderProjection r . scaleP (2*pi*r/rows) . translateP (rows*rot/(2*pi) + i) (y+col*1.732*2) . rotateP (pi/2)
  44. let hex = map transform hexagon
  45. save
  46. newPath
  47. uncurry moveTo $ head hex
  48. mapM_ (uncurry lineTo) $ tail hex
  49. closePath
  50. stroke
  51. setSourceRGBA 0.8 0 1 1
  52. let nex = map (transform . translateP (-0.4) 0.333 . scaleP 0.2) hexagon
  53. newPath
  54. uncurry moveTo $ head nex
  55. mapM_ (uncurry lineTo) $ tail nex
  56. closePath
  57. fill
  58. restore
  59.  
  60. drawHexagons col rot r rows i = do
  61. drawHexagon col rot r rows (i*2)
  62. drawHexagon col rot r rows (i*2+1)
  63.  
  64. exposeHandler widget e = do
  65. drawWin <- widgetGetDrawWindow widget
  66. (wi,hi) <- widgetGetSize widget
  67. let (w,h) = (realToFrac wi, realToFrac hi)
  68. t <- getPOSIXTime
  69. let rot = normalizeAngle ((realToFrac t) / 5)
  70. let rows = 50
  71. let columns = 25
  72. let radius = 150
  73. let hexagonRadius = 2*pi*radius / rows
  74. renderWithDrawable drawWin $ do
  75. save
  76. setSourceRGBA 1 1 1 1
  77. paint
  78. setSourceRGBA 0 0 0 1
  79. translate (w/2) (h/2) -- (-3*hexagonRadius)
  80. setLineWidth 0.5
  81. mapM_ (\i -> do
  82. mapM_ (drawHexagons (i-columns/2) (rot) radius rows) [i*2..i*2+rows/6-4])
  83. [0..columns-1]
  84. scale 0.5 0.5
  85. setLineWidth 0.5
  86. mapM_ (\i -> do
  87. mapM_ (drawHexagons (i-columns) (-rot*4) radius rows) [i*2..i*2+rows/6-4])
  88. [0..columns*2-1]
  89. restore
  90. widgetQueueDraw widget
  91. return True
  92.  
  93. main = do
  94. initGUI
  95. window <- windowNew
  96. da <- drawingAreaNew
  97. set window [ containerChild := da ]
  98. windowSetDefaultSize window 410 450
  99. onExpose da (exposeHandler da)
  100. onDestroy window mainQuit
  101. widgetShowAll window
  102. mainGUI
Add Comment
Please, Sign In to add comment