Guest User

Untitled

a guest
Jul 18th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. ; The New Year in Snowflakes
  2. ; A Clojure doodle by Chouser
  3. (import '(java.awt Color Graphics Frame RenderingHints))
  4.  
  5. (defonce draw-agent (agent nil))
  6. (defonce halt (atom false))
  7.  
  8. (defmacro ui-thread [& body]
  9. `(javax.swing.SwingUtilities/invokeAndWait (fn [] ~@body)))
  10.  
  11. (def face {
  12. \0 [[ 0 69] [ 40 0] [120 0] [160 69] [160 138]
  13. [160 207] [120 276] [ 40 276] [ 0 207] [ 0 138]]
  14. \1 [[ 11 40] [ 80 0] [ 80 92] [ 80 184] [ 0 276] [80 276] [160 276]]
  15. \2 [[ 0 69] [ 40 0] [120 0] [160 69] [120 138] [80 207]
  16. [ 30 276] [120 276] [200 276]]})
  17.  
  18. (defn draw-snow [_ g size]
  19. (ui-thread
  20. (doto g
  21. ; clear window
  22. (.setColor Color/BLACK)
  23. (.fillRect 0 0 (.width size) (.height size))
  24.  
  25. ; draw white snowflakes
  26. (.setColor Color/WHITE)
  27. (.setRenderingHint RenderingHints/KEY_ANTIALIASING
  28. RenderingHints/VALUE_ANTIALIAS_ON)
  29. (.setRenderingHint RenderingHints/KEY_RENDERING
  30. RenderingHints/VALUE_RENDER_QUALITY)))
  31. (let [t (.getTransform g)]
  32. (doseq [[digit col] (map vector "2011" (reductions + [0 290 270 270]))
  33. [x y] (face digit)
  34. :let [x (+ 50 x col)
  35. y (+ 80 y)
  36. a4 (* 2 Math/PI (rand))] ; rotate whole snowflake some
  37. :while (not @halt)]
  38. (ui-thread
  39. (dotimes [i (+ 5 (rand 15))]
  40. (let [s (* 20 (rand)) ; length of line
  41. dx (* 30 (rand)) ; distance of line from center of flake
  42. a1 (* 2 Math/PI (rand)) ; angle of line around center
  43. a2 (- (* Math/PI (rand)) (/ Math/PI 2))] ; angle from "spoke"
  44. (doseq [a3 (range 0 6 (/ Math/PI 3)), flip [1 -1]]
  45. (doto g
  46. (.setTransform t)
  47. (.translate x y)
  48. (.rotate (+ a4 a3))
  49. (.scale flip 1)
  50. (.rotate a1)
  51. (.translate dx 0.0)
  52. (.rotate a2)
  53. (.scale s 1.0)
  54. (.fillRect 0 0 1 1)))))))))
  55.  
  56. (defn paint [g size]
  57. (future
  58. (reset! halt true)
  59. (await draw-agent) ; doesn't guarantee much
  60. (reset! halt false)
  61. (send-off draw-agent draw-snow g size)))
  62.  
  63.  
  64. (defonce frame
  65. (doto
  66. (proxy [Frame] ["Happy New Year"]
  67. (paint [_] (paint (.getGraphics this) (.getSize this))))
  68. (.setSize 600 700)
  69. (.setVisible true)))
Add Comment
Please, Sign In to add comment