Skip to content

Instantly share code, notes, and snippets.

@quephird
Created July 7, 2012 15:59
Show Gist options
  • Save quephird/3066996 to your computer and use it in GitHub Desktop.
Save quephird/3066996 to your computer and use it in GitHub Desktop.
leaves-on-the-lawn
(ns leaves-on-the-lawn
(:import [processing.core PApplet PConstants])
(:use quil.core))
(def screen-w 3840)
(def screen-h 2160)
(def really-dark-brown [43 17 0])
; TODO: Implement a means of randomly generating grassy greens
(def grass-blade-greens [[105 139 34] [110 139 61] [85 107 47]])
(def grass-crease-greens [[95 130 34] [100 130 61] [75 97 47]])
(def grass-square-width 40)
(def maple-vertices [[-2 0] [-2 0] [-2 -150] [-2 -150]
[-100 -125] [-200 -150] [-200 -150] [-200 -150]
[-150 -200] [-250 -250] [-250 -250] [-250 -250]
[-200 -275] [-250 -400] [-250 -400] [-250 -400]
[-175 -375] [-175 -400] [-175 -400] [-175 -400]
[-100 -325] [-100 -475] [-100 -475] [-100 -475]
[-50 -450] [0 -525] [0 -525]])
(def maple-veins [[0 -150 -200 -150]
[0 -150 -250 -400]
[0 -150 0 -525]
[0 -150 250 -400]
[0 -150 200 -150]
[-100 -250 -250 -250]
[-125 -275 -175 -400]
[0 -300 -100 -475]
[0 -300 100 -475]
[100 -250 250 -250]
[125 -275 175 -400]])
(def oak-vertices [[-2 100] [-2 100] [-2 0] [-2 0]
[-75 -25] [-50 -50]
[-115 -75] [-125 -115] [-100 -135] [-50 -135]
[-140 -185] [-150 -225] [-125 -245] [-65 -235]
[-115 -280] [-125 -320] [-100 -340] [-50 -330]
[-90 -370] [-100 -410] [-75 -430] [-30 -420]
[-50 -470] [-50 -490] [-30 -500] [-20 -530]
[-10 -540] [0 -542] [0 -542]])
(def oak-veins [[0 0 0 -542]
[0 -10 -75 -25]
[0 -10 75 -25]
[0 -70 -125 -115]
[0 -70 125 -115]
[0 -160 -150 -225]
[0 -160 150 -225]
[0 -260 -125 -320]
[0 -260 125 -320]
[0 -350 -100 -410]
[0 -350 100 -410]
[0 -450 -50 -490]
[0 -450 50 -490]])
; This is an attempt to restrict the set of colors to fall-like ones:
; reds, oranges, yellows, browns, and some purples
(defn- random-fallen-leaf-color []
[(+ 127 (random 128)) (random 128) (random 64)])
; As usual, simulate detritus
(defn- generate-background []
(apply fill really-dark-brown)
(no-stroke)
(doseq [_ (range 300)]
(ellipse (random screen-w) (random screen-h) 100 100)))
; TODO: Tighten up the code a bit here
(defn- generate-grass-blade [color-idx]
(let [top-of-blade-x (- (random 100) 50)
top-of-blade-y (- 0 (random 50) 300)]
(apply fill (grass-blade-greens color-idx))
(apply stroke (grass-crease-greens color-idx))
(begin-shape)
(curve-vertex 0 0)
(curve-vertex 0 0)
(curve-vertex top-of-blade-x top-of-blade-y)
(curve-vertex 20 0)
(curve-vertex 0 0)
(end-shape)
(stroke-weight 2)
(line 10 0 top-of-blade-x top-of-blade-y)
(no-stroke)))
(defn- generate-lawn []
(doseq [row (range (/ screen-w grass-square-width))
col (range (+ 10 (/ screen-h grass-square-width)))]
(push-matrix)
(translate (+ (* row grass-square-width) (random 10))
(+ (* col grass-square-width) (random 10)))
(generate-grass-blade (int (random (count grass-blade-greens))))
(pop-matrix)))
(defn- generate-leaf [leaf-vertices leaf-veins]
; Draw main body of leaf
(let [leaf-color (random-fallen-leaf-color)]
(apply fill leaf-color)
(begin-shape)
(doseq [leaf-vertex leaf-vertices]
(apply curve-vertex leaf-vertex))
(doseq [leaf-vertex (map (fn [[x y]] [(Math/abs x) y]) (reverse leaf-vertices))]
(apply curve-vertex leaf-vertex))
(end-shape)
; Draw veins using slightly darker color than that of the leaf
(apply stroke (map #(* % 0.9) leaf-color))
(doseq [leaf-vein leaf-veins]
(apply line leaf-vein))
(no-stroke)))
(defn- generate-fallen-leaves []
(doseq [_ (range 100)]
(push-matrix)
(translate (random screen-w) (random screen-h))
(rotate (radians (random 360)))
(let [coin-toss (random 2)]
(if (< coin-toss 1)
(generate-leaf maple-vertices maple-veins)
(generate-leaf oak-vertices oak-veins)))
(pop-matrix)))
(defn setup []
(smooth)
(background 0)
(no-loop))
(defn draw []
(generate-background)
(generate-lawn)
(generate-fallen-leaves)
(save "leaves-on-the-lawn.png"))
(defsketch main
:title "leaves-on-the-lawn"
:setup setup
:draw draw
:size [screen-w screen-h])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment