Skip to content

Instantly share code, notes, and snippets.

Created Apr 6, 2014
What would you like to do?
Example usage of raev
(ns symmetri.core
(:use quil.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)"
(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)
(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]
(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]
;;(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))))]
(doseq [ s (concat (:s1 metri) (reverse (:s2 metri))) ]
(draw-vertex (:x s) (:y s) line-col (:line-thickness draw-info)))
(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 []
(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)
(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