Created
September 24, 2009 17:51
-
-
Save LauJensen/192900 to your computer and use it in GitHub Desktop.
This file contains 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 dk.bestinclass.ikeda | |
(:import (javax.swing JFrame JPanel) | |
(java.awt Color Graphics) | |
(java.awt.image BufferedImage) | |
(java.awt.event MouseListener))) | |
;= Definitions | |
(def x-axis [-10 10]) | |
(def y-axis [-10 10]) | |
(def width-axis (Math/abs (apply - x-axis))) | |
(def height-axis (Math/abs (apply - y-axis))) | |
(def width-screen 640) | |
(def height-screen 480) | |
(def globals (ref {})) | |
(def running (ref true)) | |
(def animator (agent 0)) | |
(def n (atom 0)) | |
(def u 0.92) ; You're thinking.. why 0.92? :P | |
;= Logic | |
(defn ikeda [x y u] | |
(iterate (fn [[x_n y_n]] | |
(let [t_n (- 0.4 (/ 6 (+ 1 (* x_n x_n) (* y_n y_n))))] | |
[(inc (* u (- (* x_n (Math/cos t_n)) | |
(* y_n (Math/sin t_n))))) | |
(* u (+ (* x_n (Math/sin t_n)) | |
(* y_n (Math/cos t_n ))))])) | |
[x y])) | |
(def spawns (for [i (range 100)] | |
(let [x (+ (first x-axis) (rand-int width-axis)) | |
y (+ (first y-axis) (rand-int height-axis))] | |
(atom (ikeda x y u))))) | |
;==== GUI | |
(defn >screencoordinate [coordinate] | |
" Takes a coordinate from our x-axis/y-axis coordinate system and | |
converts it to a pixel coordinate on screen " | |
(let [[x y] coordinate | |
xmin (first x-axis) | |
xmax (last x-axis) | |
ymin (first y-axis) | |
ymax (last y-axis)] | |
[(* (/ (- x xmin) (- xmax xmin)) width-screen) | |
(* (/ (- y ymin) (- ymax ymin)) height-screen)])) | |
(defn draw-ikeda-map [#^Graphics canvas] | |
" Walks through the spawn points, calling their next position from | |
ikeda and draws it on the canvas " | |
(.setColor canvas (Color. 50 50 50)) | |
(doseq [spawnpoint spawns] | |
(let [point (>screencoordinate (first @spawnpoint))] | |
(.drawRect canvas (first point) (last point) 1 1))) | |
(doto canvas | |
(.setColor Color/WHITE) | |
(.fillRect 0 3 100 16) | |
(.setColor Color/BLACK) | |
(.drawRect -5 2 101 17)) | |
(.drawString canvas (format "iteration %d" @n) 5 15)) | |
(defn render [g] | |
"On first run it draws a white background on all other runs it | |
draws the ikeda maps next iteration" | |
(let [img (BufferedImage. width-screen height-screen BufferedImage/TYPE_INT_ARGB) | |
bg (.getGraphics img)] | |
(draw-ikeda-map bg) | |
(.drawImage g img 0 0 nil) | |
(.dispose bg))) | |
(defn animate [a] | |
" Calls next on all spawn points, leaving the next item for rendering at the | |
head " | |
(doseq [point spawns] (swap! point next)) | |
(swap! n inc) | |
(.updateUI (:surface @globals)) | |
(when (and @running (< @n 2000)) | |
(send-off *agent* animate))) | |
(defn init-ui [] | |
(let [frame (JFrame. "Ikeda map") | |
panel (doto | |
(proxy [JPanel] [] | |
(paint [g] (render g))))] | |
(dosync (alter globals assoc :surface panel)) | |
(doto frame | |
(.add panel) | |
.pack | |
(.setSize width-screen height-screen) | |
.show) | |
(send-off animator animate))) | |
(init-ui) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment