Skip to content

Instantly share code, notes, and snippets.

@triclops200
Last active August 29, 2015 14:22
Show Gist options
  • Save triclops200/ba6093143094ba56a8f6 to your computer and use it in GitHub Desktop.
Save triclops200/ba6093143094ba56a8f6 to your computer and use it in GitHub Desktop.
Works. Need to fully port tonight then publish as open source.
(ns test-sketch.core
(:require [quil.core :as q]
[quil.middleware :as m])
(:gen-class))
(import megamu.mesh.Voronoi)
(def minr 10)
(def maxr 100)
(import megamu.mesh.Voronoi)
(import math.geom2d.polygon.SimplePolygon2D)
(import math.geom2d.Box2D)
(import math.geom2d.Point2D)
(import math.geom2d.polygon.LinearRing2D)
(import math.geom2d.polygon.Polygons2D)
(defn points->array [points]
(let [l1 (count points)
l2 (count (first points))
a (make-array Float/TYPE l1 l2)]
(doseq [i (range l1)
j (range l2)]
(aset a i j (get-in points [i j])))
a))
(defn array->point [a]
(into [] a))
(defn array->points [a]
(mapv array->point a))
(defn point->Point [[^Double x ^Double y]]
(Point2D. x y))
(defn poly->Poly [poly]
(let [p (SimplePolygon2D.)]
(doseq [point poly]
(.addVertex p (point->Point point)))
p))
(defn Point->point [p]
[(.x p) (.y p)])
(defn Poly->poly [p]
(mapv (comp Point->point #(.firstPoint %)) (.edges p)))
(defn box->Box [[p1 p2]]
(Box2D. (point->Point p1) (point->Point p2)))
(defn clip-poly [box poly]
(let [p ^SimplePolygon2D (poly->Poly poly)
b (box->Box box)]
(try (Poly->poly (Polygons2D/intersection p (.asRectangle b)))
(catch Exception e []))))
(defn points->array [points]
(let [l1 (count points)
l2 (count (first points))
a (make-array Float/TYPE l1 l2)]
(doseq [i (range l1)
j (range l2)]
(aset a i j (get-in points [i j])))
a))
(defn array->point [a]
(into [] a))
(defn array->points [a]
(mapv array->point a))
(defn voronoi [points]
(let [v (Voronoi. (points->array points))]
(mapv (comp array->points #(.getCoords %)) (.getRegions v))))
(defn setup []
; Set frame rate to 30 frames per second.
(q/frame-rate 60)
; Set color mode to HSB (HSV) instead of default RGB.
(q/color-mode :hsb)
; setup function returns initial state. It contains
; circle color and position.
{:color 0
:x 0
:y 0})
(defn draw-poly [points]
(q/begin-shape)
(doseq [point points]
(apply q/vertex point))
(q/end-shape))
(defn update-state [state]
; Update sketch state by changing circle color and position.
state)
(defn mouse-moved [state event]
(-> state
(assoc :x (:x event) :y (:y event))))
(defn draw-state [state]
; Clear the sketch by filling it with light-grey color.
(q/background 240)
; Set circle color.
; Calculate x and y coordinates of the circle.
(let [x (:x state)
y (:y state)]
(q/fill 10 255 255)
(mapv (fn [poly]
(q/fill (rand-int 256) (rand-int 256) (rand-int 256))
(draw-poly poly)) (voronoi [[250 250] [x y] (if (not (= [x y] [y x]))
[y x]
[(inc y) (inc x)])]))
state))
(defn -main []
(q/sketch
:title "You spin my circle right round"
:size [500 500]
; setup function called only once, during sketch initialization.
:setup setup
; update-state is called on each iteration before draw-state.
:update update-state
:mouse-moved mouse-moved
:draw draw-state
; This sketch uses functional-mode middleware.
; Check quil wiki for more info about middlewares and particularly
; fun-mode.
:middleware [m/fun-mode]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment