-
Notifications
You must be signed in to change notification settings - Fork 24
/
utils.clj
85 lines (75 loc) · 2.31 KB
/
utils.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
(ns clojure2d.extra.utils
"Set of various utilities which can be used to display various objects."
(:require [clojure2d.core :refer :all]
[clojure2d.color :as c]
[fastmath.vector :as v]
[fastmath.core :as m]
[clojure2d.pixels :as p]
[fastmath.random :as r]))
(set! *warn-on-reflection* true)
(set! *unchecked-math* :warn-on-boxed)
(m/use-primitive-operators)
(defn show-palette
"Display palette.
Input: list of colors."
[palette]
(let [c (canvas 1000 300)
h2 (/ ^int (height c) 2)
bottom (- ^int (height c) 100)
step (/ (- ^int (width c) 100.0) (count palette))
cstep (inc (m/ceil step))]
(with-canvas [c c]
(-> c
(set-background 30 30 30)
(set-color 225 225 225)
(rect 0 h2 (width c) h2))
(doseq [col-no (range (count palette))]
(set-color c (nth palette col-no))
(rect c (+ 50 (* ^int col-no step)) 50 cstep bottom)))
(show-window {:canvas c})
c))
(defn show-gradient
"Display gradient.
Input: gradient function (see [[gradient]])."
[gradient]
(show-palette (map gradient (range 0.0 1.0 (/ 1.0 700.0)))))
(defn show-color
"Display color.
Input: color"
[col]
(let [c (show-palette [col])]
(with-canvas-> c
(set-color :white)
(set-font-attributes 14)
(text (str col " (" (c/format-hex col) ")") 10 20))))
(defn show-scalar-field
"Show scalar field R^2->R"
[f norm-in norm-out]
(let [p (p/pixels 800 800)
g (c/gradient [:black :white])
c (canvas 800 800)]
(dotimes [x 800]
(dotimes [y 800]
(let [col (g (norm-out (f (v/vec2 (norm-in x) (norm-in y)))))]
(p/set-color p x y col))))
(p/set-canvas-pixels! c p)
(show-window {:canvas c})))
(defn show-vector-field
"Show scalar field R^2->R^2"
[f norm-out]
(let [c (canvas 800 800)]
(with-canvas [c c]
(set-color c :white 8)
(dotimes [i 2500000]
(let [x (r/drand (- m/PI) m/PI)
y (r/drand (- m/PI) m/PI)
v (f (v/vec2 x y))]
(point c (norm-out (v 0)) (norm-out (v 1))))))
(show-window {:canvas c})))
(defn show-image
"Show image"
[img]
(let [c (canvas (width img) (height img))]
(with-canvas-> c
(image (get-image img)))
(show-window {:canvas c})))