Last active
December 31, 2015 16:39
-
-
Save quephird/8015304 to your computer and use it in GitHub Desktop.
Another shameless ripoff of another person's work of art: http://cutsquash.tumblr.com/post/67785356890/candy-version
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns candy | |
(:use quil.core)) | |
(def screen-w 1000) | |
(def screen-h 1000) | |
(def existing-spheres (atom [])) | |
(defn- rand-color [] | |
(vec (repeatedly 3 (fn [] (rand-int 255))))) | |
(defn- rand-xy [max-r] | |
(let [r (rand-int max-r) | |
t (rand-int 360) | |
x (* r (cos (radians t))) | |
y (* r (sin (radians t)))] | |
[x y])) | |
(defn- add-sphere! [x y r] | |
(swap! existing-spheres conj [x y r])) | |
(defn- draw-sphere [x y r c] | |
(push-matrix) | |
(translate x y) | |
(apply fill c) | |
(sphere r) | |
(pop-matrix)) | |
(defn- spheres-intersect? [[x1 y1 r1] [x2 y2 r2]] | |
(let [delta-x (- x1 x2) | |
delta-y (- y1 y2) | |
sum-of-radii (+ r1 r2) | |
distance-squared (+ (* delta-x delta-x) (* delta-y delta-y))] | |
(> (* sum-of-radii sum-of-radii) distance-squared))) | |
(defn- intersects-any-sphere? [existing-spheres new-sphere] | |
(if (empty? existing-spheres) | |
false | |
(some true? (map (fn [existing-sphere] (spheres-intersect? existing-sphere new-sphere)) existing-spheres)))) | |
(defn setup [] | |
(background 75) | |
(smooth) | |
(no-stroke) | |
(no-loop)) | |
(defn draw [] | |
(directional-light 255 255 225 1000 1000 -500) | |
(ambient-light 100 100 100) | |
(ambient 100 100 100) | |
(shininess 2.0) | |
; This is to prevent distortion of the spheres. | |
(ortho (* screen-w -1.1) (* screen-w 1.1) (* screen-h 1.1) (* screen-h -1.1) 0 2000) | |
(translate (/ screen-w 2) (/ screen-h 2) -1000) | |
(doseq [_ (range 1000)] | |
; Randomize x,y coordinates based on radius of set of candy spheres | |
(let [[x y] (rand-xy 1000) | |
; Randomize radius of each candy | |
r (+ 5 (rand-int 95)) | |
c (rand-color)] | |
; This is a fairly hackish approach since I just throw away undesired choices; | |
; ideally I ought to generate usable coordinates each time but I don't know | |
; how to implement that at this time. | |
(if (not (intersects-any-sphere? @existing-spheres [x y r])) | |
(do | |
(draw-sphere x y r c) | |
(add-sphere! (int x) (int y) r)))))) | |
(sketch | |
:title "candy" | |
:setup setup | |
:draw draw | |
:size [screen-w screen-h] | |
:renderer :opengl) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment