-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdiagrams.clj
534 lines (402 loc) · 15.3 KB
/
diagrams.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
(ns mindra.diagrams
(:refer-clojure :exclude [cat])
(:require [mindra.common :refer [sep vstr]]))
;;
;; Please see https://diagrams.github.io/doc/manual.html
;;
;; Most function names closely match the names used by diagrams.
;;
;; See https://github.com/rorokimdim/mindra/blob/master/src/Mindra/Diagrams/Parser/SVG.hs
;; for how minda will parse the constructed string.
;;
(defn construct-arrow-options
"Constructs arrow options for styling an arrow diagram.
head-type: A shape to place at the head of the arrow. Must be one of
[tri, dart, halfDart, spike, thorn, lineHead, noHead]
tail-type: A shape to place at the tail of the arrow. Must be one of
[tri, dart, halfDart, spike, thorn, lineTail, noTail, quill, block]
head-gap: Distance to leave between the head and the target point.
tail-gap: Distance to leave between the starting point and the tail.
head-color: Color of head as a map with keys :red, :green, :blue and :alpha
tail-color: Color of head as a map with keys :red, :green, :blue and :alpha
shapt-points: Optional list of points to use to draw the shaft -- a cubic spline
of the points will be used instead of the default straight line.
See https://diagrams.github.io/doc/arrow.html"
[& {:keys [head-type
tail-type
head-gap
tail-gap
head-color
tail-color
shaft-points]
:or {head-type "dart"
tail-type "noTail"
head-gap 0
tail-gap 0
head-color {:red 0 :green 0 :blue 0 :alpha 255}
tail-color {:red 0 :green 0 :blue 0 :alpha 255}
shaft-points []}}]
(sep "ArrowOpts"
head-type
tail-type
head-gap
tail-gap
(:red head-color 0) (:green head-color 0) (:blue head-color 0) (:alpha head-color 255)
(:red tail-color 0) (:green tail-color 0) (:blue tail-color 0) (:alpha tail-color 255)
(vstr shaft-points)))
(defn arrow-at
"An arrow starting at given point with the length and direction of given vector.
See https://hackage.haskell.org/package/diagrams-lib/docs/Diagrams-TwoD-Arrow.html"
([point vector]
(sep "ArrowAt" point vector))
([point vector options]
(sep "ArrowAt" point vector options)))
(defn arrow-between
"An arrow from point-a to point-b.
See https://hackage.haskell.org/package/diagrams-lib/docs/Diagrams-TwoD-Arrow.html"
([point-a point-b]
(sep "ArrowBetween" point-a point-b))
([point-a point-b options]
(sep "ArrowBetween" point-a point-b options)))
(defn arrow-connect
"An arrow connecting origins of diagrams named n1 and n2 in given diagram.
See https://hackage.haskell.org/package/diagrams-lib/docs/Diagrams-TwoD-Arrow.html"
([n1 n2 diagram]
(sep "ArrowConnect" (pr-str n1) (pr-str n2) diagram))
([n1 n2 diagram options]
(sep "ArrowConnect" (pr-str n1) (pr-str n2) diagram options)))
(defn arrow-connect-outside
"An arrow connecting boundaries of diagrams named n1 and n2 in given diagram.
See https://hackage.haskell.org/package/diagrams-lib/docs/Diagrams-TwoD-Arrow.html"
([n1 n2 diagram]
(sep "ArrowConnectOutside" (pr-str n1) (pr-str n2) diagram))
([n1 n2 diagram options]
(sep "ArrowConnectOutside" (pr-str n1) (pr-str n2) diagram options)))
(defn arrow-connect-perim
"An arrow connecting perimeter of diagrams named n1 and n2 at angles a1 and a2 (in degrees) in given diagram.
See https://hackage.haskell.org/package/diagrams-lib/docs/Diagrams-TwoD-Arrow.html"
([n1 n2 a1 a2 diagram]
(sep "ArrowConnectPerim" (pr-str n1) (pr-str n2) a1 a2 diagram))
([n1 n2 a1 a2 diagram options]
(sep "ArrowConnectPerim" (pr-str n1) (pr-str n2) a1 a2 diagram options)))
;;
;; Functions for setting diagram attributes
;;
(defn background
"Sets background color of a diagram.
Superimposes the diagram on top of a bounding rectangle of the given color.
See https://hackage.haskell.org/package/diagrams-lib/docs/Diagrams-TwoD.html"
[r g b diagram]
(sep "Background" r g b diagram))
(defn background-frame
"Similar to background but makes the background rectangle larger than the diagram by given size.
See https://hackage.haskell.org/package/diagrams-lib/docs/Diagrams-TwoD.html"
[n r g b diagram]
(sep "BackgroundFrame" n r g b diagram))
(defn dashing-g
"Sets global line-dashing style of a diagram.
NS is list of alternating lengths of on and off portions of the stroke.
N is offset into the dash pattern at which the stroke should start.
See https://hackage.haskell.org/package/diagrams-lib/docs/Diagrams-Attributes.html"
[ns n diagram]
(sep "DashingG" (vstr ns) n diagram))
(defn dashing-l
"Sets local line-dashing style of a diagram.
NS is list of alternating lengths of on and off portions of the stroke.
N is offset into the dash pattern at which the stroke should start.
See https://hackage.haskell.org/package/diagrams-lib/docs/Diagrams-Attributes.html"
[ns n diagram]
(sep "DashingL" (vstr ns) n diagram))
(defn dashing-n
"Sets normalized line-dashing style of a diagram.
NS is list of alternating lengths of on and off portions of the stroke.
N is offset into the dash pattern at which the stroke should start.
See https://hackage.haskell.org/package/diagrams-lib/docs/Diagrams-Attributes.html"
[ns n diagram]
(sep "DashingN" (vstr ns) n diagram))
(defn fill-color
"Sets the fill-color of a diagram to given red, green, blue, alpha values."
[r g b a diagram]
(sep "FillColor" r g b a diagram))
(defn font-size
"Sets the font-size of text in a diagram."
[n diagram]
(sep "FontSize" n diagram))
(defn line-color
"Sets line color of a diagram."
[r g b a diagram]
(sep "LineColor" r g b a diagram))
(defn line-width-g
"Sets global line-width of a diagram.
See https://hackage.haskell.org/package/diagrams-lib/docs/Diagrams-Attributes.html"
[w diagram]
(sep "LineWidthG" w diagram))
(defn line-width-l
"Sets local line-width of a diagram.
See https://hackage.haskell.org/package/diagrams-lib/docs/Diagrams-Attributes.html"
[w diagram]
(sep "LineWidthL" w diagram))
(defn line-width-n
"Sets normalized line-width of a diagram.
See https://hackage.haskell.org/package/diagrams-lib/docs/Diagrams-Attributes.html"
[w diagram]
(sep "LineWidthN" w diagram))
;;
;; Alignment and combinator functions
;;
(defn align-left
"Moves the local origin horizontally to the left edge of the envelope.
See https://hackage.haskell.org/package/diagrams-lib/docs/Diagrams-TwoD-Align.html"
[n diagram]
(sep "AlignLeft" n diagram))
(defn align-right
"Moves the local origin horizontally to the right edge of the envelope.
See https://hackage.haskell.org/package/diagrams-lib/docs/Diagrams-TwoD-Align.html"
[n diagram]
(sep "AlignRight" n diagram))
(defn align-top
"Moves the local origin vertically to the top edge of the envelope.
See https://hackage.haskell.org/package/diagrams-lib/docs/Diagrams-TwoD-Align.html"
[n diagram]
(sep "AlignTop" n diagram))
(defn align-bottom
"Moves the local origin vertically to the bottom edge of the envelope.
See https://hackage.haskell.org/package/diagrams-lib/docs/Diagrams-TwoD-Align.html"
[n diagram]
(sep "AlignBottom" n diagram))
(defn align-x
"Moves the local origin horizontally by given distance.
Negative values move the local origin to the left edge of the boundary.
Positive values move the local origin to the right edge of the boundary.
See https://hackage.haskell.org/package/diagrams-lib/docs/Diagrams-TwoD-Align.html"
[n diagram]
(sep "AlignX" n diagram))
(defn align-y
"Moves the local origin vertically by given distance.
Negative values move the local origin to the bottom edge of the boundary.
Positive values move the local origin to the top edge of the boundary.
See https://hackage.haskell.org/package/diagrams-lib/docs/Diagrams-TwoD-Align.html"
[n diagram]
(sep "AlignY" n diagram))
(defn center-x
"Centers the local origin along the x-axis.
See https://hackage.haskell.org/package/diagrams-lib/docs/Diagrams-TwoD-Align.html"
[diagram]
(sep "CenterX" diagram))
(defn center-y
"Centers the local origin along the y-axis.
See https://hackage.haskell.org/package/diagrams-lib/docs/Diagrams-TwoD-Align.html"
[diagram]
(sep "CenterY" diagram))
(defn center-xy
"Centers the local origin along both x-axis and y-axis.
See https://hackage.haskell.org/package/diagrams-lib/docs/Diagrams-TwoD-Align.html"
[diagram]
(sep "CenterXY" diagram))
(defn beside
"Places two diagrams beside each other along given x-y vector.
See https://diagrams.github.io/haddock/diagrams-lib/Diagrams-Combinators.html#v:beside"
[x y diagram-a diagram-b]
(sep "Beside" x y diagram-a diagram-b))
(defn cat
"Positions diagrams so that their local origins lie along given x-y vector.
See https://diagrams.github.io/haddock/diagrams-lib/Diagrams-TwoD-Combinators.html"
[x y diagrams]
(sep "Cat" x y "[" (apply sep diagrams) "]"))
(defn cat'
"Variadic version of cat."
[x y & diagrams]
(cat x y diagrams))
(defn hcat
"Horizontally concatenates given diagrams.
See https://diagrams.github.io/haddock/diagrams-lib/Diagrams-TwoD-Combinators.html"
[diagrams]
(sep "HCat" (str "[" (apply sep diagrams) "]")))
(defn hcat'
"Variadic version of hcat."
[& diagrams]
(hcat diagrams))
(defn vcat
"Vertically concatenates given diagrams.
See https://diagrams.github.io/haddock/diagrams-lib/Diagrams-TwoD-Combinators.html"
[diagrams]
(sep "VCat" (str "[" (apply sep diagrams) "]")))
(defn vcat'
"Variadic version of vcat."
[& diagrams]
(vcat diagrams))
(defn hsep
"Horizontally separates given diagrams with given pad (separation).
See https://diagrams.github.io/haddock/diagrams-lib/Diagrams-TwoD-Combinators.html"
[pad diagrams]
(sep "HSep" pad (str "[" (apply sep diagrams) "]")))
(defn hsep'
"Horizontally separates given diagrams with given pad (separation).
See https://diagrams.github.io/haddock/diagrams-lib/Diagrams-TwoD-Combinators.html"
[pad & diagrams]
(hsep pad diagrams))
(defn vsep
"Vertically separates given diagrams with given pad (separation).
See https://diagrams.github.io/haddock/diagrams-lib/Diagrams-TwoD-Combinators.html"
[pad diagrams]
(sep "VSep" pad (str "[" (apply sep diagrams) "]")))
(defn vsep'
"Variadic version of vsep."
[pad & diagrams]
(vsep pad diagrams))
(defn superimpose
"Superimposes given diagrams on top of each other."
[diagrams]
(str "[" (apply sep diagrams) "]"))
(defn superimpose'
"Variadic version of superimpose."
[& diagrams]
(superimpose diagrams))
(defn pad
"A diagram padded by give x and y factors."
[pad-x pad-y diagram]
(sep "Pad" pad-x pad-y diagram))
(defn position
"Positions given diagrams at given points."
[diagrams points]
(sep "Position" (vstr points) "[" (apply sep diagrams) "]"))
(defn show-envelope
"Shows envelope of diagram with a red cubic spline.
Useful for debugging purposes."
[diagram]
(sep "ShowEnvelope" diagram))
(defn show-origin
"Shows origin of diagram as a red dot.
Useful for debugging purposes."
[diagram]
(sep "ShowOrigin" diagram))
;;
;; Geometric shapes
;;
(defn circle
"A circle with the given radius."
[radius]
(sep "Circle" radius))
(defn ellipse
"An ellipse with given eccentricity.
The eccentricity must be within the interval [0,1).
See https://hackage.haskell.org/package/diagrams-lib/docs/Diagrams-TwoD-Ellipse.html"
[eccentricity]
(sep "Ellipse" eccentricity))
(defn ellipse-xy
"An axis-aligned ellipse, centered at the origin, with radius x along the x-axis and radius y along the y-axis.
See https://hackage.haskell.org/package/diagrams-lib/docs/Diagrams-TwoD-Ellipse.html"
[radius-x radius-y]
(sep "EllipseXY" radius-x radius-y))
(defn rectangle
"A rectangle of given width and height."
[width height]
(sep "Rectangle" width height))
(defn rounded-rectangle
"A rectangle of given width and height with circular rounded corners of given radius.
If radius is negative the corner will be cut out in a reverse arc.
If radius is larger than half the smaller dimension of width and height, then it will be reduced to fit in that range,
to prevent the corners from overlapping.
See https://hackage.haskell.org/package/diagrams-lib/docs/Diagrams-TwoD-Shapes.html#g:4"
[width height radius]
(sep "RoundedRectangle" width height radius))
(defn square
"A square of given length."
[n]
(sep "Rectangle" n n))
(defn bspline
"A uniform cubic B-spline with given control points.
See https://hackage.haskell.org/package/diagrams-lib/docs/Diagrams-CubicSpline.html"
[points]
(sep "BSpline" (vstr points)))
(defn bspline'
"Variadic version of bspline."
[& points]
(bspline points))
(defn cubic-spline
"A cubic spline from a list of points.
The first argument specifies whether the path should be closed.
See https://hackage.haskell.org/package/diagrams-lib/docs/Diagrams-CubicSpline.html"
[closed? points]
(sep "CubicSpline" closed? (vstr points)))
(defn cubic-spline'
"Variadic version of cubic-spline."
[closed? & points]
(cubic-spline closed? points))
(defn hrule
"A horizontal line of given length."
[n]
(sep "HRule" n))
(defn vrule
"A vertical line of given length."
[n]
(sep "VRule" n))
(defn line
"A line along a list of points."
[points]
(sep "Line" (vstr points)))
(defn line'
"A line along a list of points."
[& points]
(line points))
(defn triangle
"An equilateral triangle, with sides of the given length and base parallel to the x-axis.
See https://hackage.haskell.org/package/diagrams-lib/docs/Diagrams-TwoD-Shapes.html"
[n]
(sep "Triangle" n))
(defn polygon-regular
"A regular polygon with n sides, each of given length."
[n length]
(sep "PolygonRegular" n length))
(defn polygon-polar
"A polar polygon with given list of central angles (in degrees) and given list of radii.
See https://hackage.haskell.org/package/diagrams-lib/docs/Diagrams-TwoD-Polygons.html#g:1"
[angles radii]
(sep "PolygonPolar" (vstr angles) (vstr radii)))
(defn polygon-sides
"A polygon with given list of external angles (in degrees) and given lengths.
See https://hackage.haskell.org/package/diagrams-lib/docs/Diagrams-TwoD-Polygons.html#g:1"
[angles lengths]
(sep "PolygonSides" (vstr angles) (vstr lengths)))
;;
;; Transformations
;;
(defn reflect-x
"Flip the x-coordinates of points in a diagram.
Maps (x, y) to (-x, y)
See https://hackage.haskell.org/package/diagrams-lib/docs/Diagrams-TwoD-Transform.html#g:5"
[diagram]
(sep "ReflectX" diagram))
(defn reflect-y
"Flip the y-coordinates of points in a diagram.
Maps (x, y) to (x, -y)
See https://hackage.haskell.org/package/diagrams-lib/docs/Diagrams-TwoD-Transform.html#g:5"
[diagram]
(sep "ReflectY" diagram))
(defn rotate
"A diagram rotated anticlockwise by the given angle (in degrees)."
[angle diagram]
(sep "Rotate" angle diagram))
(defn scale
"A diagram scaled by the given x and y factors."
[x y diagram]
(sep "Scale" x y diagram))
(defn translate
"A diagram translated by the given x and y coordinates."
[x y diagram]
(sep "Translate" x y diagram))
;;
;; Miscellaneous
;;
(defn named
"Names a diagram."
[s diagram]
(sep "Named" (pr-str s) diagram))
(defn text
"A diagram with given text."
[s]
(pr-str s))
(defn image
"An image from given file-path with given width and height."
[file-path width height]
(sep "Image" (pr-str file-path) width height))