Skip to content

Instantly share code, notes, and snippets.

@vorce
Created April 6, 2014 10:53
Show Gist options
  • Save vorce/10004442 to your computer and use it in GitHub Desktop.
Save vorce/10004442 to your computer and use it in GitHub Desktop.
Example usage of raev
(ns symmetri.core
(:use quil.core
raev.core))
(def mid-x 400)
(def mid-y 300)
(defrecord Point [x y z])
(defrecord Sym [s1 s2 col])
(defrecord Colors [r g b a])
(defrecord DrawInfo [lines ellipses draw-colors line-thickness ellipse-size])
(defn new-colors [rgb]
(Colors. (first rgb) (second rgb) (last rgb) 0))
(defn hex-to-rgb
"Convert hex strings to rgb list. Ex: 'FF0000' -> (255 0 0)"
[hex]
(let [ hex-pairs (partition 2 hex) ]
(for [p hex-pairs]
(java.lang.Integer/parseInt (apply str p) 16))))
(defn draw-colors []
(raev.core/get-palettes-with {raev.core/PARAM_KEYWORDS "space"}))
(def draw-chance [true true true true false])
(defn new-draw-info []
(DrawInfo. (rand-nth draw-chance) (rand-nth draw-chance) (rand-nth (draw-colors)) (+ 1 (rand-int 4)) (+ 5 (rand-int 15))))
(defn symlists [y]
(let [ x1 (rand-int mid-x)
dist (- mid-x x1)
x2 (+ mid-x dist)
p1 (Point. x1 y 0)
p2 (Point. x2 y 0) ]
[p1 p2]))
(defn add-sym-to-metri [metri]
(let [ y (- (:y (last (:s1 metri))) (+ (rand-int 25) 25))
points (symlists y)
s1p (first points)
s2p (last points) ]
(Sym. (conj (:s1 metri) s1p) (conj (:s2 metri) s2p) (:col metri))))
(defn build-metri [metri nsyms]
(if (>= (count (:s1 metri)) nsyms)
metri
(recur (add-sym-to-metri metri) (dec nsyms))))
(defn add-mid-sym [metri]
(let [ y (+ (rand-int 450) 150)
mid-sym (Point. mid-x y 0) ]
(Sym. (conj (:s1 metri) mid-sym) (:s2 metri) (:col metri))))
(defn starters [] (symlists (- 600 (rand-int 150))))
(defn start-metri []
(let [ st (starters) ]
(Sym. [(first st)] [(last st)] 255)))
(defn draw-vertex [x y col line-size]
(no-fill)
(stroke (first col) (second col) (last col))
(stroke-join :round)
(stroke-weight line-size)
(vertex x y))
(defn draw-ellipse [x y width height col]
;;(no-fill)
(no-stroke)
;;(stroke-weight 1)
(fill (first col) (second col) (last col))
(ellipse x y width height))
(defn draw-lines [metri draw-info]
(if (:lines draw-info)
(let [ line-col (hex-to-rgb (first (:colors (:draw-colors draw-info))))]
(begin-shape)
(doseq [ s (concat (:s1 metri) (reverse (:s2 metri))) ]
(draw-vertex (:x s) (:y s) line-col (:line-thickness draw-info)))
(end-shape))))
(defn draw-dots [metri draw-info]
(if (:ellipses draw-info)
(let [c (hex-to-rgb (second (:colors (:draw-colors draw-info)))) ]
(doseq [ s (concat (:s1 metri) (reverse (:s2 metri))) ]
(draw-ellipse (:x s) (:y s) (:ellipse-size draw-info) (:ellipse-size draw-info) c)))))
(defn draw-metri [metri draw-info]
(draw-lines metri draw-info)
(draw-dots metri draw-info))
(defn save-screenshot []
(println "Saving screenshot")
(save-frame "symmetri-####.png"))
(defn setup []
(smooth))
(defn draw []
(let [draw-info (new-draw-info)
bg-col (hex-to-rgb (last (:colors (:draw-colors draw-info)))) ]
(println "Using palette:" (:draw-colors draw-info))
(background (first bg-col) (second bg-col) (last bg-col))
(draw-metri (add-mid-sym (build-metri (start-metri) (+ (rand-int 10) 5))) draw-info)))
(defn mouse-click []
(if (= (mouse-button) :right)
(save-screenshot)
(draw)))
(defsketch symmetri
:title "Symmetri"
:setup setup
;; :draw draw
:size [800 600]
:mouse-pressed mouse-click)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment