Skip to content

Instantly share code, notes, and snippets.

@ztellman
Last active August 29, 2015 14:13
Show Gist options
  • Select an option

  • Save ztellman/ac5ae0f51c2710ee55a4 to your computer and use it in GitHub Desktop.

Select an option

Save ztellman/ac5ae0f51c2710ee55a4 to your computer and use it in GitHub Desktop.
(import
'[java.awt.geom
Ellipse2D$Double
Rectangle2D$Double])
(defn rectangle [x y w h]
(Rectangle2D$Double. x y w h))
(defn circle [x y r]
(Ellipse2D$Double. (- x r) (- y r) (* r 2) (* r 2)))
(defn area [x]
(condp instance? x
Ellipse2D$Double
(* 3.1415 (/ (.getWidth x) 2) (/ (.getWidth x) 2))
Rectangle2D$Double
(* (.getWidth x) (.getHeight x))))
(defn grid [w h steps]
(let [dx (/ w steps)
dy (/ h steps)]
(for [x (range steps)
y (range steps)]
(rectangle (* x dx) (* y dy) dx dy))))
(defn random-circle [w h r]
(circle
(+ r (* (rand) (- w (* 2 r))))
(+ r (* (rand) (- h (* 2 r))))
r))
(defn rasterization-error [grid ^Ellipse2D$Double circle]
(/ (->> grid
(filter #(.intersects circle ^Rectangle2D$Double %))
(map area)
(reduce +))
(area circle)))
(defn avg [s]
(double (/ (reduce + s) (count s))))
(comment
(def g (grid 10 10 100))
(for [x (range 1 21)]
(avg (repeatedly 1e3 #(rasterization-error g (random-circle 10 10 (* x 0.1))))))
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment