Skip to content

Instantly share code, notes, and snippets.

@quephird
Last active January 4, 2016 18:09
Show Gist options
  • Select an option

  • Save quephird/8659225 to your computer and use it in GitHub Desktop.

Select an option

Save quephird/8659225 to your computer and use it in GitHub Desktop.
This sketch creates a set of branches and includes a blurring effect to simulate depth of perception.
(ns looking-up
(:use quil.core quil.applet))
(def screen-w 1920)
(def screen-h 1080)
(defn leaf [l]
; unfortunately, I still need to resort to dumping down
; into Java until bug fix for bezier-vertex is released.
(let [p (current-applet)
half-w (* 0.3 l) half-l (* 0.5 l)
bottom-x 0 bottom-y 0
top-x 0 top-y (- bottom-y l)
left-x (- bottom-x half-w) left-y (- bottom-y half-l)
right-x (+ bottom-x half-w) right-y (- bottom-y half-l)]
(no-stroke)
(fill 0 127 0)
(begin-shape)
(vertex bottom-x bottom-y)
(.bezierVertex p left-x left-y left-x left-y top-x top-y)
(.bezierVertex p right-x right-y right-x right-y bottom-x bottom-y)
(end-shape)
; center vein
(stroke-weight 2)
(stroke 0 100 0)
(line 0 0 top-x top-y)
(no-fill)))
(defn leaf-group [l]
; draw two or three leaves
(let [leaf-count (+ 2 (rand-int 2))
start-θ (/ 180 leaf-count 2)
delta-θ (* start-θ 4)]
(push-matrix)
(rotate (radians (rand-int start-θ)))
(doseq [_ (range leaf-count)]
(leaf l)
(rotate (radians (rand-int delta-θ))))
(pop-matrix)))
(defn- branch-iter [segment-w segment-l current-depth branch-depth]
; draw two branches, each at a slightly randomized angle
(let [left-θ (- -5 (rand-int 25))
right-θ (+ 5 (rand-int 25))
left-l (+ segment-l (rand-int (* 0.4 segment-l)))
right-l (+ segment-l (rand-int (* 0.4 segment-l)))]
(doseq [[θ l] [[left-θ left-l] [right-θ right-l]]]
(push-matrix)
(rotate (radians θ))
(stroke 40 20 0)
(stroke-weight segment-w)
(line 0 0 l 0)
(translate l 0)
; draw leaves at end of branch segment only if we are in the last iteration
(if (< current-depth branch-depth)
(branch-iter (* segment-w 0.7) (* l 0.7) (inc current-depth) branch-depth)
(leaf-group (* l 0.5)))
(pop-matrix))))
(defn branch [start-segment-l start-segment-w z max-z]
; bigger z means greater distance from viewer.
; to try to simulate distance, branch segment widths and lengths are scaled down;
; blur values are scaled up.
(let [scaled-segment-w (* (- max-z (dec z)) (/ start-segment-w max-z))
scaled-segment-l (* (- max-z (dec z)) (/ start-segment-l max-z))
blur-factor (* 2 (dec z))
branch-depth 5]
(branch-iter scaled-segment-w scaled-segment-l 1 branch-depth)
(display-filter :blur blur-factor)))
(defn sky []
; for now, this just paints a background;
; TODO: perhaps a sun could be added later
(background 127 127 255)
)
(defn tree [branch-θ start-segment-l start-branch-w]
; this draws branches in the background first.
; branches in the foreground are in focus; those farther away are blurred.
(let [max-z 3]
(doseq [z (range max-z 0 -1)]
(push-matrix)
(rotate (radians (+ branch-θ (- 20 (rand-int 40)))))
(branch start-segment-l start-branch-w z max-z)
(pop-matrix))))
(defn setup []
(smooth)
(no-loop))
(defn draw []
(let [trunk-x (* screen-w 1.1)
trunk-y (* screen-h 0.9)
start-branch-w 50
start-segment-l 450
branch-θ 210]
(sky)
(translate trunk-x trunk-y)
(tree branch-θ start-segment-l start-branch-w)
(save "looking-up.png")))
(sketch
:title "looking up"
:setup setup
:draw draw
:size [screen-w screen-h]
:renderer :java2d
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment