-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathex37_fractal_bestiary.clj
307 lines (268 loc) · 21.5 KB
/
ex37_fractal_bestiary.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
;; http://www.brainfillingcurves.com/
;;
;; Press keys:
;;
;; * LEFT / RIGHT - change depth
;; * UP / DOWN - zoom in / out
;; * WSAD - move object in window
;; * F - save image
;; * R - random setup
;; * SPACE - randomize preset setup from the book
;; * M - modulate flip (see pages 20-26)
;; * B - draw shorten lines
(ns ex37-fractal-bestiary
(:require [clojure2d.core :as c2d]
[fastmath.core :as m]
[fastmath.vector :as v]
[fastmath.random :as r])
(:import [fastmath.vector Vec2]))
(set! *warn-on-reflection* true)
(set! *unchecked-math* :warn-on-boxed)
(m/use-primitive-operators)
(def ^:const size 800)
(def ^:const s60 (m/sin (m/radians 60.0)))
(def ^:const s60- (- s60))
(def ^:const unit (Vec2. 1.0 0.0))
;; canvas
(def cnvs (c2d/canvas size size :highest))
;; main algorithm
(def default-recipe {:pos [0.1 0.6] :len 0.25 :grid :tri :short-line false :depth 5
:recipe [[1 0 1 1] [0 1 1 1] [1 -1 1 1] [1 0 1 1]]})
(defn transform-sq->tri
"Transform square grid coordinates to triangle grid."
[v]
(let [matched (first (filter #(< (v/angle-between v (first %)) 1.0e-6) [[(Vec2. 1.0 0.0) (Vec2. 1.0 0.0)]
[(Vec2. -1.0 0.0) (Vec2. -1.0 0.0)]
[(Vec2. 1.0 -1.0) (Vec2. 0.5 s60-)]
[(Vec2. 0.0 -1.0) (Vec2. -0.5 s60-)]
[(Vec2. -1.0 -1.0) (Vec2. -0.5 s60-)]
[(Vec2. -1.0 1.0) (Vec2. -0.5 s60)]
[(Vec2. 0.0 1.0) (Vec2. 0.5 s60)]
[(Vec2. 1.0 1.0) (Vec2. 0.5 s60)]]))]
(when-let [[in tg] matched]
(v/mult tg (/ ^double (v/mag v) ^double (v/mag in))))))
(defn process-recipe
"Process recipe from book format to angles and lengths required by method provided in this example"
[recipe]
(let [{:keys [grid recipe pos len short-line]} (merge default-recipe recipe)
transform (if (= grid :tri) transform-sq->tri identity)
vects (map #(let [[x y] %] (transform (Vec2. x y))) recipe)
angles (map #(v/relative-angle-between %2 %1) (cons unit vects) vects)
rot (reduce v/add vects)]
{:pos pos
:len len
:short-line short-line
:scale (/ ^double (v/mag rot))
:rot (v/relative-angle-between unit rot)
:recipe (map #(conj (drop 2 %2) %1 (v/mag %3)) angles recipe vects)}))
(defn draw-beast
"Draw recursively given recipe (internally processed)."
[canvas ^long depth ^double len {:keys [short-line ^double scale rot recipe] :as all}]
(run! #(let [[^double vlen ^double angle ^double forward ^double flip] %
slen (* vlen len)
linelen (if short-line (* 0.6 slen) slen)]
(c2d/rotate canvas angle)
(c2d/push-matrix canvas)
(when (neg? forward)
(-> canvas
(c2d/translate slen 0)
(c2d/flip-y)
(c2d/rotate m/PI)))
(when (neg? flip) (c2d/flip-y canvas))
(if (zero? depth)
(c2d/line canvas 0 0 linelen 0)
(-> canvas
(c2d/rotate rot)
(draw-beast (dec depth) (* slen scale) all)))
(c2d/pop-matrix canvas)
(c2d/translate canvas slen 0)) recipe)
canvas)
(defn draw-recipe
"Draw recipe for given depth."
([recipe depth]
(println recipe)
(let [{^double len :len pos :pos :as precipe} (process-recipe recipe)
[px py] (v/mult pos size)]
(c2d/with-canvas-> cnvs
(c2d/set-background 21 20 25)
(c2d/set-color :lightgrey 240)
(c2d/set-stroke 0.8)
(c2d/translate px py)
(draw-beast depth (* size len) precipe)))
recipe)
([recipe] (draw-recipe recipe (or (:depth recipe) 5))))
;; recipes from book
(def recipes {;; preliminary part
:koch {:grid :tri :recipe [[1 0 1 1] [0 1 1 1] [1 -1 1 1] [1 0 1 1]]}
:p20 {:grid :tri :recipe [[1 0 1 1] [0 1 1 1] [1 -1 1 1] [1 0 1 -1]]}
:p22 {:grid :sq :recipe [[0 2 1 1] [1 1 1 1] [2 0 1 1] [1 -1 1 1] [0 -2 1 1]] :len 0.1 :pos [0.3 0.7]}
:p23 {:grid :sq :recipe [[0 2 1 1] [1 1 1 -1] [2 0 1 1] [1 -1 1 -1] [0 -2 1 1]] :len 0.1 :pos [0.3 0.7]}
:p24 {:grid :sq :recipe [[0 1 1 1] [1 -1 1 1] [1 0 -1 -1]] :pos [0.3 0.6] :depth 8}
;; :p25 {:grid :sq :recipe [[0 2 -1 1] [1 0 -1 -1] [1 0 1 1] [-1 -1 1 1] [2 -1 1 -1]] :len 0.2 :pos [0.3 0.7]} ;;wrong
:p37-dragon-curve {:grid :sq :recipe [[1 0 1 1] [0 1 -1 -1]] :len 0.3 :pos [0.3 0.6] :depth 11}
:p37-5-dragon {:grid :sq :recipe [[0 1 1 1] [1 0 1 1] [0 -1 1 1] [1 0 1 1] [0 1 1 1]] :len 0.3 :pos [0.2 0.65]}
:p50-ab12 {:grid :sq :recipe [[1 0 1 1] [0 1 1 1]] :pos [0.3 0.55] :depth 11}
:p50-a3 {:grid :sq :recipe [[1 0 1 1] [0 1 1 -1]] :pos [0.3 0.55] :len 0.4 :depth 11}
:p50-c1 {:grid :sq :recipe [[1 0 1 -1] [0 1 1 1]] :pos [0.1 0.7] :len 0.4 :depth 11}
:p50-a1 {:grid :sq :recipe [[1 0 1 1] [0 1 -1 -1]] :len 0.4 :pos [0.3 0.6] :depth 11}
:p50-d1 {:grid :sq :recipe [[1 0 -1 -1] [0 1 1 1]] :len 0.4 :pos [0.2 0.7] :depth 11}
:p50-d2 {:grid :sq :recipe [[1 0 -1 -1] [0 1 -1 1]] :len 0.4 :pos [0.15 0.65] :depth 13}
:p50-b4 {:grid :sq :recipe [[1 0 -1 1] [0 1 -1 -1]] :len 0.4 :pos [0.2 0.55] :depth 13}
:p50-c2 {:grid :sq :recipe [[1 0 1 -1] [0 1 -1 1]] :len 0.4 :pos [0.1 0.7] :depth 11}
:p50-b3 {:grid :sq :recipe [[1 0 -1 1] [0 1 1 -1]] :len 0.4 :pos [0.2 0.5] :depth 11}
:p50-cd34 {:grid :sq :recipe [[1 0 -1 -1] [0 1 -1 -1]] :len 0.6 :pos [0.2 0.75] :depth 12}
;; sqrt(2)
:p52-dragon-curve {:grid :sq :recipe [[1 0 1 1] [0 1 -1 -1]] :len 0.3 :pos [0.3 0.6] :depth 8}
:p52-polya {:grid :sq :recipe [[1 0 1 -1] [0 1 -1 1]] :len 0.4 :pos [0.1 0.7] :depth 8}
;; sqrt(3)
:p53-sq {:grid :sq :recipe [[1 0 1 1] [0 1 1 1] [1 -1 1 1] [1 0 1 1]] :depth 3}
:p53-tri {:grid :tri :recipe [[1 0 1 1] [0 1 1 1] [1 -1 1 1] [1 0 1 1]] :depth 3}
:p55-ter-dragon {:grid :tri :recipe [[0 1 1 1] [1 -1 1 1] [0 1 1 1]] :len 0.4 :pos [0.2 0.7]}
:p56-inverted-ter-dragon {:grid :tri :recipe [[0 1 -1 1] [1 -1 -1 1] [0 1 -1 1]] :len 0.4 :pos [0.2 0.7]}
:p56 {:grid :tri :recipe [[0 1 -1 1] [1 -1 1 1] [0 1 -1 1]] :len 0.4 :pos [0.2 0.7]}
:p57-a {:grid :tri :recipe [[0 1 -1 1] [0 1 -1 1] [1 -1 1 -1]] :len 0.3 :pos [0.35 0.8] :depth 6}
:p57-b {:grid :tri :recipe [[0 1 1 1] [0 1 1 1] [1 -1 -1 -1]] :len 0.3 :pos [0.35 0.8]}
:p57-c {:grid :tri :recipe [[0 1 1 -1] [0 1 -1 1] [1 -1 1 -1]] :len 0.3 :pos [0.3 0.8] :depth 6}
:p58-a {:grid :tri :recipe [[0 1 1 -1] [0 1 -1 1] [1 -1 -1 1]] :len 0.3 :pos [0.25 0.85] :depth 6}
:p58-b {:grid :tri :recipe [[0 1 -1 -1] [0 1 1 1] [1 -1 1 1]] :len 0.25 :pos [0.25 0.7] :depth 6}
:p58-c {:grid :tri :recipe [[0 1 1 -1] [0 1 -1 1] [1 -1 1 1]] :len 0.25 :pos [0.25 0.7] :depth 6}
:p59-yin-dragon {:grid :tri :recipe [[0 1 1 1] [0 1 -1 -1] [1 -1 1 1]] :len 0.25 :pos [0.25 0.75] :depth 6}
;; sqrt(4)
:p60-cesaros-sweep {:grid :sq :recipe [[1 0 1 1] [0 1 1 1] [0 -1 1 1] [1 0 1 1]] :len 0.4 :pos [0.1 0.7] :depth 4}
:p62 {:grid :sq :recipe [[0 1 -1 1] [1 0 1 1] [0 -1 -1 -1] [1 0 1 -1]] :pos [0.35 0.6] :depth 4}
:p63-a {:grid :sq :recipe [[0 1 1 -1] [1 0 -1 1] [0 -1 1 -1] [1 0 -1 1]] :pos [0.25 0.75] :depth 4}
:p63-b {:grid :sq :recipe [[0 1 -1 1] [1 0 1 -1] [0 -1 -1 1] [1 0 1 -1]] :pos [0.3 0.6] :depth 4}
:p64-peano-sweep {:grid :sq :recipe [[0 1 -1 -1] [1 0 1 1] [1 0 1 1] [0 -1 -1 -1]] :len 0.4 :pos [0.1 0.9] :depth 4}
:p66-v1-dragon {:grid :sq :recipe [[1 1 1 1] [1 0 -1 -1] [0 -1 1 1]] :len 0.2 :pos [0.3 0.6] :depth 6}
:p67 {:grid :sq :recipe [[1 1 -1 -1] [1 0 1 1] [0 -1 1 1]] :len 0.2 :pos [0.25 0.6] :depth 6}
:p70 {:grid :sq :recipe [[1 1 -1 1] [0 -1 -1 -1] [1 0 1 1]] :len 0.4 :pos [0.1 0.85] :depth 4}
:p70-v2-dragon {:grid :sq :recipe [[1 1 1 1] [0 -1 1 1] [1 0 -1 -1]] :pos [0.3 0.6] :depth 4}
:p71 {:grid :sq :recipe [[0 1 1 1] [1 -1 -1 -1] [1 0 1 1]] :len 0.3 :pos [0.3 0.6]}
:p73-dragon-of-eve {:grid :sq :recipe [[0 1 1 1] [1 -1 1 1] [1 0 -1 -1]] :pos [0.3 0.6]}
:p74-sierpinski-arrowhead-curve {:grid :tri :recipe [[0 1 1 -1] [1 0 1 1] [1 -1 1 -1]] :len 0.4 :pos [0.1 0.85] :depth 6}
:p75-sierpinski-family {:grid :tri :recipe [[0 1 1 -1] [1 -1 -1 -1] [1 0 1 1]] :len 0.4 :pos [0.1 0.7]}
:p76 {:grid :tri :recipe [[1 -1 1 1] [0 1 1 1] [0 1 1 1] [1 -1 1 1]] :pos [0.25 0.5] :depth 4}
:p77 {:grid :tri :recipe [[1 -1 1 -1] [0 1 -1 1] [0 1 1 -1] [1 -1 1 -1]] :pos [0.25 0.5] :depth 3}
:p78-a {:grid :tri :recipe [[1 -1 -1 1] [0 1 1 1] [0 1 -1 1] [1 -1 -1 1]] :pos [0.25 0.5] :depth 4}
:p78-b {:grid :tri :recipe [[0 1 -1 1] [0 1 1 -1] [1 -1 -1 1] [1 -1 1 -1]] :pos [0.35 0.7]}
:p80 {:grid :tri :recipe [[0 1 1 1] [0 1 -1 1] [1 -1 1 -1] [1 -1 -1 1]] :len 0.2 :pos [0.3 0.7] :depth 4} ;; result different...
:p81 {:grid :tri :recipe [[0 1 -1 -1] [0 1 1 1] [1 -1 -1 -1] [1 -1 1 1]] :len 0.2 :pos [0.3 0.7] :depth 3}
:p82 {:grid :tri :recipe [[1 0 1 1] [-1 1 -1 -1] [1 0 1 1] [1 -1 -1 -1]] :len 0.4 :pos [0.1 0.85] :depth 3}
:p83-a {:grid :tri :recipe [[1 0 -1 1] [-1 1 1 -1] [1 0 -1 1] [1 -1 1 -1]] :len 0.4 :pos [0.1 0.85] :depth 3}
:p83-b {:grid :tri :recipe [[1 0 1 1] [-1 1 -1 -1] [1 0 1 1] [1 -1 1 -1]] :len 0.4 :pos [0.1 0.85] :depth 3}
;; sqrt(5)
:p84-5-dragon {:grid :sq :recipe [[0 1 1 1] [1 0 1 1] [0 -1 1 1] [1 0 1 1] [0 1 1 1]] :pos [0.25 0.65] :depth 3}
:p84-pinched-5-dragon {:grid :sq :recipe [[0 1 -1 1] [1 0 -1 1] [0 -1 -1 1] [1 0 -1 1] [0 1 -1 1]] :pos [0.25 0.65] :depth 3}
:p85-quartet {:grid :sq :recipe [[0 1 -1 -1] [0 1 1 1] [1 0 1 1] [0 -1 -1 -1] [1 0 1 1]] :pos [0.35 0.85] :depth 3}
:p85-inner-flip-quartet {:grid :sq :recipe [[0 1 1 -1] [0 1 -1 1] [1 0 -1 1] [0 -1 1 -1] [1 0 -1 1]] :pos [0.35 0.85] :depth 3}
:p86-a {:grid :sq :recipe [[0 1 1 -1] [1 0 1 1] [0 1 -1 -1] [1 0 -1 1] [0 -1 -1 -1]] :pos [0.25 0.85] :depth 2}
:p86-b {:grid :sq :recipe [[0 1 1 -1] [1 0 1 1] [0 1 1 -1] [1 0 -1 1] [0 -1 1 -1]] :pos [0.25 0.85] :depth 2}
:p87-a {:grid :sq :recipe [[0 1 1 1] [1 0 -1 -1] [0 1 1 1] [1 0 -1 -1] [0 -1 1 1]] :pos [0.25 0.8] :depth 3}
:p87-b {:grid :sq :recipe [[0 1 -1 1] [1 0 1 -1] [0 1 -1 1] [1 0 1 -1] [0 -1 -1 1]] :pos [0.25 0.75] :depth 3}
:p87-c {:grid :sq :recipe [[0 1 1 -1] [1 0 -1 1] [0 1 1 -1] [1 0 -1 1] [0 -1 1 -1]] :pos [0.25 0.85] :depth 3}
:p87-d {:grid :sq :recipe [[0 1 -1 -1] [1 0 1 1] [0 1 -1 -1] [1 0 1 1] [0 -1 -1 -1]] :pos [0.25 0.8] :depth 3}
:p88-a {:grid :sq :recipe [[0 1 1 -1] [0 1 -1 1] [1 0 1 -1] [1 0 -1 1] [0 -1 1 -1]] :pos [0.3 0.85] :depth 3}
:p88-b {:grid :sq :recipe [[0 1 1 1] [0 1 -1 -1] [1 0 1 1] [1 0 -1 -1] [0 -1 1 1]] :len 0.2 :pos [0.3 0.8] :depth 3}
:p89-a {:grid :sq :recipe [[1 0 1 -1] [0 1 1 -1] [0 1 -1 1] [1 0 -1 1] [0 -1 -1 1]] :len 0.2 :pos [0.25 0.7] :depth 3}
:p89-b {:grid :sq :recipe [[1 0 -1 -1] [0 1 -1 -1] [0 1 1 1] [1 0 1 1] [0 -1 1 1]] :len 0.2 :pos [0.25 0.7] :depth 3}
;; sqrt(7)
:p91-7-dragon {:grid :tri :recipe [[1 0 1 1] [-1 1 1 1] [1 0 1 1] [0 -1 1 1] [1 0 1 1] [-1 1 1 1] [1 0 1 1]] :pos [0.2 0.6] :depth 2}
:p92 {:grid :tri :recipe [[1 0 -1 1] [1 0 -1 1] [-1 1 1 -1] [0 -1 -1 1] [-1 1 -1 1] [1 0 1 -1] [1 0 1 -1]] :pos [0.2 0.6] :depth 2}
:p93-a {:grid :tri :recipe [[1 0 1 1] [1 0 -1 -1] [-1 1 -1 -1] [0 -1 -1 -1] [-1 1 1 1] [1 0 -1 -1] [1 0 1 1]] :pos [0.2 0.6] :depth 2}
:p93-b {:grid :tri :recipe [[1 0 -1 -1] [1 0 -1 -1] [-1 1 1 1] [-1 0 -1 -1] [1 -1 -1 -1] [0 1 1 1] [1 0 -1 -1]] :pos [0.2 0.6] :depth 2}
:p95-gosper-curve {:grid :tri :recipe [[1 0 1 1] [0 1 -1 -1] [-1 0 -1 -1] [-1 1 1 1] [1 0 1 1] [1 0 1 1] [1 -1 -1 -1]] :pos [0.25 0.8] :depth 2}
:p96-inner-flip-gosper {:grid :tri :recipe [[1 0 -1 1] [0 1 1 -1] [-1 0 1 -1] [-1 1 -1 1] [1 0 -1 1] [1 0 -1 1] [1 -1 1 -1]] :pos [0.25 0.8] :depth 2}
:p97-anti-gosper {:grid :tri :recipe [[1 0 1 1] [0 1 1 1] [-1 0 -1 -1] [-1 1 -1 -1] [1 0 1 1] [1 0 -1 -1] [1 -1 -1 -1]] :pos [0.2 0.8] :depth 3}
:p98 {:grid :tri :recipe [[0 1 -1 -1] [-1 1 -1 -1] [1 0 1 1] [0 -1 1 1] [1 0 -1 -1] [0 1 1 1] [1 -1 -1 -1]] :pos [0.2 0.8] :depth 3}
:p100 {:grid :tri :recipe [[1 0 1 1] [-1 1 1 1] [1 0 1 1] [1 -1 1 1] [-1 0 1 1] [0 1 1 1] [1 0 1 1]] :depth 2 :pos [0.1 0.65] :len 0.33}
:p101-root-7-yin {:grid :tri :recipe [[1 0 1 1] [1 0 -1 -1] [-1 1 1 1] [-1 1 -1 -1] [1 0 1 1] [1 0 -1 -1] [0 -1 1 1]] :depth 3 :pos [0.15 0.7] :len 0.23}
:p102-a {:grid :tri :recipe [[0 -1 1 -1] [1 0 -1 1] [1 0 1 -1] [-1 1 -1 1] [-1 1 1 -1] [1 0 -1 1] [1 0 1 -1]] :depth 3 :pos [0.25 0.5]}
:p102-b {:grid :tri :recipe [[1 0 1 -1] [1 0 -1 1] [1 0 1 -1] [1 0 -1 1] [-1 1 1 -1] [-1 1 -1 1] [0 -1 1 -1]] :depth 3 :pos [0.05 0.6] :len 0.22}
:p103 {:grid :tri :recipe [[1 0 1 1] [1 0 -1 -1] [1 0 1 1] [1 0 -1 -1] [-1 1 1 1] [-1 1 -1 -1] [0 -1 1 1]] :depth 3 :pos [0.05 0.6] :len 0.22}
:p104-a {:grid :tri :recipe [[0 -1 -1 1] [1 0 -1 1] [1 0 1 -1] [1 0 -1 1] [-1 1 -1 1] [-1 1 1 -1] [1 0 1 -1]] :depth 3 :len 0.2 :pos [0.3 0.5]}
:p104-b {:grid :tri :recipe [[0 -1 1 1] [1 0 1 1] [1 0 -1 -1] [1 0 1 1] [-1 1 1 1] [-1 1 -1 -1] [1 0 -1 -1]] :depth 3 :len 0.2 :pos [0.3 0.5]}
:p105 {:grid :tri :recipe [[0 1 1 1] [-1 1 1 1] [1 0 1 1] [0 -1 1 1] [1 -1 1 1] [1 0 1 1] [0 1 1 1]] :depth 2 :pos [0.25 0.8]}
:p106-a {:grid :tri :recipe [[-1 1 -1 -1] [1 0 1 1] [-1 1 -1 -1] [1 0 1 1] [1 0 1 1] [0 -1 -1 -1] [1 0 1 1]] :depth 2 :pos [0.25 0.85]}
:p106-b {:grid :tri :recipe [[-1 1 1 -1] [1 0 -1 1] [-1 1 1 -1] [1 0 -1 1] [1 0 -1 1] [0 -1 1 -1] [1 0 -1 1]] :depth 2 :pos [0.25 0.85]}
:p107-a {:grid :tri :recipe [[1 0 1 1] [0 -1 1 1] [1 0 1 1] [-1 1 1 1] [-1 1 1 1] [1 0 1 1] [1 0 1 1]] :depth 2 :pos [0.2 0.5]}
:p107-b {:grid :tri :recipe [[1 0 -1 -1] [0 -1 -1 -1] [1 0 -1 -1] [-1 1 -1 -1] [-1 1 -1 -1] [1 0 -1 -1] [1 0 -1 -1]] :depth 2 :pos [0.2 0.55]}
:p108-a {:grid :tri :recipe [[-1 1 -1 1] [1 0 1 -1] [1 0 -1 1] [0 -1 1 -1] [1 0 1 -1] [1 0 1 -1] [-1 1 -1 1]] :depth 2 :len 0.2 :pos [0.25 0.6]}
:p108-b {:grid :tri :recipe [[-1 1 1 1] [1 0 1 1] [1 0 -1 -1] [0 -1 -1 -1] [1 0 1 1] [1 0 1 1] [-1 1 1 1]] :depth 2 :len 0.2 :pos [0.25 0.6]}
:p109 {:grid :tri :recipe [[-1 1 1 -1] [-1 1 -1 1] [1 0 1 -1] [1 0 -1 1] [1 0 1 -1] [1 0 -1 1] [0 -1 1 -1]] :depth 3 :len 0.15 :pos [0.4 0.7]}
:p110 {:grid :tri :recipe [[1 -1 -1 -1] [1 0 1 1] [0 -1 1 1] [-1 1 1 1] [0 1 -1 -1] [1 0 1 1] [0 1 -1 -1]] :depth 2 :pos [0.2 0.45]}
;; sqrt(8)
:p112 {:grid :sq :recipe [[-1 1 -1 1] [0 1 1 -1] [1 1 1 1] [1 0 -1 -1] [1 -1 1 1]] :depth 4 :len 0.165 :pos [0.4 0.75]}
:p113-a {:grid :sq :recipe [[1 1 1 1] [1 0 1 1] [0 -1 -1 -1] [1 1 1 1] [-1 1 1 -1]] :depth 3 :len 0.23 :pos [0.05 0.7]}
:p113-b {:grid :sq :recipe [[1 1 1 1] [1 -1 -1 -1] [1 1 1 1] [-1 0 1 -1] [0 1 -1 1]] :depth 3 :len 0.23 :pos [0.05 0.7]}
:p114 {:grid :sq :recipe [[1 0 1 -1] [0 1 -1 1] [1 -1 -1 -1] [0 2 -1 1]] :len 0.21 :pos [0.1 0.7]}
:p115 {:grid :sq :recipe [[1 1 1 1] [0 1 1 -1] [-1 0 -1 1] [1 1 1 1] [0 -1 1 -1] [1 0 -1 1]] :depth 2 :pos [0.3 0.9] :len 0.2}
:p116-a {:grid :sq :recipe [[1 -1 -1 -1] [1 1 1 1] [-1 0 -1 1] [0 1 1 -1] [1 0 -1 1] [0 1 1 -1]] :depth 2 :pos [0.2 0.55] :len 0.2}
:p116-b {:grid :sq :recipe [[0 2 -1 1] [1 -1 -1 -1] [0 1 -1 1] [1 0 1 -1]] :depth 3 :pos [0.5 0.8] :len 0.2}
:p117 {:grid :sq :recipe [[-1 1 1 1] [1 1 1 1] [1 -1 1 1] [1 0 -1 -1] [0 1 -1 -1]] :depth 3 :len 0.19 :pos [0.4 0.7]}
:p118-a {:grid :sq :recipe [[1 -1 1 1] [0 1 -1 -1] [-1 1 1 1] [1 0 -1 -1] [1 1 1 1]] :depth 3 :len 0.25 :pos [0.3 0.6]}
:p118-b {:grid :sq :recipe [[0 1 -1 -1] [1 0 1 1] [0 -1 -1 -1] [1 0 1 1] [0 -1 -1 -1] [1 0 1 1] [0 -1 -1 -1] [-1 0 1 1]] :depth 2 :len 0.189 :pos [0.2 0.4]}
:p118-c {:grid :sq :recipe [[0 1 1 1] [0 1 -1 -1] [-1 0 1 1] [0 1 1 1] [1 0 -1 -1] [1 0 1 1] [0 -1 -1 -1] [1 0 -1 -1]] :depth 2 :len 0.189 :pos [0.5 0.85]}
:p118-d {:grid :sq :recipe [[1 0 1 1] [0 1 -1 -1] [-1 0 1 1] [0 1 -1 -1] [1 0 1 1] [0 -1 -1 -1] [1 0 1 1] [0 1 -1 -1]] :depth 2 :len 0.25 :pos [0.25 0.75]}
:p118-e {:grid :sq :recipe [[-1 1 -1 -1] [-1 1 1 1] [1 0 -1 -1] [1 0 1 1] [1 0 -1 -1] [1 0 1 1]] :depth 4 :len 0.189 :pos [0.55 0.75]}
:p119-twin-twin-dragon {:grid :sq :recipe [[0 1 1 1] [0 1 1 1] [1 -1 1 1] [1 -1 -1 -1] [0 1 1 1] [0 1 -1 -1]] :depth 3 :len 0.25 :pos [0.25 0.75]}
:p120 {:grid :sq :recipe [[0 2 1 1] [1 -1 1 1] [1 0 1 1] [0 1 -1 -1]] :depth 3 :len 0.189 :pos [0.35 0.7]}
:p121 {:grid :sq :recipe [[-1 1 1 1] [1 0 1 1] [0 1 -1 -1] [1 -1 1 1] [1 0 1 1] [0 1 -1 -1]] :depth 2 :len 0.217 :pos [0.35 0.7]}
:p122 {:grid :sq :recipe [[1 1 -1 -1] [-1 0 -1 -1] [0 1 1 1] [2 0 1 1]] :depth 3 :len 0.217 :pos [0.3 0.8]}
:p123 {:grid :sq :recipe [[0 2 1 1] [1 0 1 1] [0 -1 -1 -1] [1 0 1 1] [0 1 -1 -1]] :depth 2 :len 0.217 :pos [0.35 0.75]}
:p124-curled-dragon-of-eve {:grid :sq :recipe [[-1 1 1 1] [0 1 1 1] [1 -1 1 1] [1 0 -1 -1] [1 1 -1 -1]] :depth 3 :len 0.189 :pos [0.4 0.7]}
:p125-brainfiller {:grid :sq :recipe [[0 1 -1 1] [1 0 1 -1] [0 -1 -1 1] [1 0 1 -1] [0 2 1 -1]] :depth 3 :len 0.2875 :pos [0.15 0.7]}
;; sqrt(9)
:p126-square-koch {:grid :sq :recipe [[1 0 1 1] [0 1 1 1] [1 0 1 1] [0 -1 1 1] [1 0 1 1]] :depth 3}
:p126-original-peano-curve {:grid :sq :recipe [[1 0 1 1] [0 1 1 1] [1 0 1 1] [0 -1 1 1] [-1 0 1 1] [0 -1 1 1] [1 0 1 1] [0 1 1 1] [1 0 1 1]] :depth 2 :pos [0.1 0.5]}
})
;; (draw-recipe (set-state! window (:p126-original-peano-curve recipes)))
;; window / events
(def window-name "Fractal Bestiary - Brainfilling Curves.")
(def window (c2d/show-window {:canvas cnvs
:window-name window-name
:state default-recipe}))
(defmethod c2d/key-released [window-name c2d/virtual-key] [e {^long depth :depth ^double len :len :or {depth 5 len 0.25} :as state}]
(let [ndepth (condp = (c2d/key-code e)
:right (min 20 (inc depth))
:left (max 0 (dec depth))
depth)
nlen (condp = (c2d/key-code e)
:up (* len 1.15)
:down (/ len 1.15)
len)]
(if (or (not= len nlen) (not= ndepth depth))
(do
(println (str "New depth: " ndepth))
(draw-recipe (assoc state :depth ndepth :len nlen)))
state)))
(defmethod c2d/key-pressed [window-name \space] [_ _]
(let [name (rand-nth (keys recipes))]
(println (str "Recipe: " name))
(draw-recipe (name recipes))))
(defmethod c2d/key-pressed [window-name \b] [_ {short-line :short-line :as state}]
(draw-recipe (assoc state :short-line (not short-line))))
(defmethod c2d/key-pressed [window-name \r] [_ _]
(draw-recipe {:grid (if (r/brand) :tri :sq)
:len 0.1
:pos [0.5 0.5]
:recipe (filter #(let [[^int x ^int y] %]
(not (and (zero? x) (zero? y)))) (repeatedly (r/irand 2 9)
#(vector (r/irand -1 2)
(r/irand -1 2)
(r/randval -1 1)
(r/randval -1 1))))}))
(defmethod c2d/key-pressed [window-name \m] [_ {recipe :recipe :as state}]
(draw-recipe (assoc state :recipe (map #(let [[x y _ _] %]
[x y (r/randval -1 1) (r/randval -1 1)]) recipe))))
(defmethod c2d/key-pressed [window-name \w] [_ {pos :pos :or {pos [0.1 0.6]} :as state}]
(let [[^double x ^double y] pos] (draw-recipe (assoc state :pos [x (- y 0.05)]))))
(defmethod c2d/key-pressed [window-name \s] [_ {pos :pos :or {pos [0.1 0.6]} :as state}]
(let [[^double x ^double y] pos] (draw-recipe (assoc state :pos [x (+ y 0.05)]))))
(defmethod c2d/key-pressed [window-name \a] [_ {pos :pos :or {pos [0.1 0.6]} :as state}]
(let [[^double x ^double y] pos] (draw-recipe (assoc state :pos [(- x 0.05) y]))))
(defmethod c2d/key-pressed [window-name \d] [_ {pos :pos :or {pos [0.1 0.6]} :as state}]
(let [[^double x ^double y] pos] (draw-recipe (assoc state :pos [(+ x 0.05) y]))))
(defmethod c2d/key-pressed [window-name \f] [_ state]
(c2d/save cnvs (c2d/next-filename "results/ex37/" ".jpg"))
state)
(draw-recipe (c2d/get-state window))