-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCatalan_Interpretations_FPS_drawing_routines.scm
1254 lines (1117 loc) · 49.6 KB
/
Catalan_Interpretations_FPS_drawing_routines.scm
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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ;;
;; Catalan_Interpretations_FPS_drawing_routines.scm ;;
;; ;;
;; Functions for drawing some combinatorial interpretations of Catalan ;;
;; numbers with the help of Olin Shivers' and Wandy Sae-Tan's FPS ;;
;; (Functional PostScript) library. ;;
;; ;;
;; This Scheme-code is Copyright (C) 2002-2011 by Antti Karttunen ;;
;; (E-mail: <my_firstname>.<my_surname>@gmail.com) and is placed under ;;
;; The OEIS Contributor's License Agreement ;;
;; (see: http://oeis.org/wiki/The_OEIS_Contributor%27s_License_Agreement ) ;;
;; (More specifically, revision dated 21:40, 1 April 2010, available as:) ;;
;; http://oeis.org/w/index.php?title=The_OEIS_Contributor%27s_License_Agreement&oldid=1388581
;; ;;
;; ;;
;; ;;
;; For an older version of this module, see URL: ;;
;; http://web.archive.org/web/20050827072848/http://ndirty.cute.fi/~karttu/matikka/Nekomorphisms/gato-fps.scm
;; ;;
;; For the current version, see under: ;;
;; http://oeis.org/wiki/User:Antti_Karttunen ;;
;; ;;
;; Note: this module runs only in scsh (Scheme Shell) and uses ;;
;; the FPS (functional PostScript) library by Wandy Sae-Tan and ;;
;; Olin Shivers, located at http://www.scsh.net/resources/fps.html ;;
;; and also archived under: ;;
;; http://wayback.archive.org/web/*/http://www.scsh.net/resources/fps.html ;;
;; ;;
;; The latest scsh (Scheme Shell) can be found at http://www.scsh.net/ ;;
;; (Tested to work at least under the version 0.6.7, released May 16, 2006) ;;
;; ;;
;; This file last edited June 17 2011 by Antti Karttunen. ;;
;; ;;
;; After unpacking the shar-archive (which includes this source-file and ;;
;; a few *.sxp files) to the SAME DIRECTORY fps-1.0 as where the patched ;;
;; FPS-library is located, cd there and start scsh as: ;;
;; ;;
;; % scsh ;;
;; Welcome to scsh 0.6.7 (R6RS) ;;
;; Type ,? for help. ;;
;; > ,config ,load fps-package.scm ;;
;; fps-package.scm ;;
;; > ,open fps ;;
;; Load structure fps (y/n)? y ;;
;; > ,load Catalan_Interpretations_FPS_drawing_routines.scm ;;
;; Catalan_Interpretations_FPS_drawing_routines.scm ;;
;; Catalan_Interpretations_conversion_functions.scm ;;
;; > ;;
;; ;;
;; Use (for example) as: ;;
;; (output-sexp-file-as-ps-file "a014486.sxp" "a014486.ps" 12 ;;
;; "Page ~A of http://oeis.org/A014486/a014486.pdf" *FIRST-PAGE*) ;;
;; ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; The following utility functions are from lstfuns1.scm module:
;; (load "../Schemuli/lstfuns1.scm") ;;
(define attach! ; Borrowed from Franz lisp, is like destructive cons.
(lambda (elem lista)
(set-cdr! lista (cons (car lista) (cdr lista)))
(set-car! lista elem)
lista
)
)
(define (copy-tree bt)
(cond ((not (pair? bt)) bt)
(else (cons (copy-tree (car bt))
(copy-tree (cdr bt)))
)
)
)
(define (nthcdr n lista)
(if (or (zero? n) (null? lista))
lista
(nthcdr (- n 1) (cdr lista))
)
)
(define (count-pars a)
(cond ((not (pair? a)) 0)
(else (+ 1 (count-pars (car a)) (count-pars (cdr a))))
)
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Load the conversion routines from parenthesizations (sexp's)
;; to appropriate manifestations of the Catalan's family.
(load "Catalan_Interpretations_conversion_functions.scm")
(define (1+ n) (+ 1 n))
(define (-1+ n) (- n 1))
(define (post-incr! n-list)
(let ((z (car n-list)))
(set-car! n-list (1+ z))
z
)
)
(define (monus1 n) (if (> n 0) (-1+ n) n))
(define (find-max a)
(cond ((not (pair? a))
(if (number? a) a 0)
)
(else (max (find-max (car a)) (find-max (cdr a))))
)
)
(define (pin-headed-line pt1 pt2)
(compose (line pt1 pt2)
(arc pt1 0.5 0 2pi)
(arc pt2 0.5 0 2pi)
)
)
(define (pin-headed-pict-line pt1 pt2)
(stroke
(compose (line pt1 pt2)
(arc pt1 0.5 0 2pi)
;; (arc pt2 0.5 0 2pi) ;; Don't draw the lower bullet, as it has been already drawn!
)
)
)
(define (pin-headed-pict-line-red pt1 pt2)
(stroke
(compose (line pt1 pt2)
(arc pt1 0.5 0 2pi)
;; (arc pt2 0.5 0 2pi) ;; Don't draw the lower bullet, as it has been already drawn!
)
(:color (rgb 1 0 0))
)
)
;; Test:
;; (define pentagon (draw-polygon-triangularization '((() ())) 24 100 300 #f))
;; (define handshakes (draw-n-chords '((() ())) 24 200 300 #f #f))
;; (define both (compose pentagon handshakes))
;; (out both "esim1.ps")
;; Or:
;; (define hexadecigon (draw-polygon-triangularization '((() ((()) ()) ((() ()))) (() (()))) 48 100 300 #f))
;; (define handshakes2 (draw-n-chords '((() ((()) ()) ((() ()))) (() (()))) 48 200 300 #f #f))
;; (define both2 (compose hexadecigon handshakes2))
;; (out both2 "esim2.ps")
;;
(define (draw-binary-tree sexp ox oy x_displ y_displ diamwidth)
(with-attrib ((:line-width (or diamwidth 0.6)) (:line-cap 'round))
(translate ox oy
(compose
(draw-bincordtree
(fill-cordtree-x-coordinates! (construct-coordinate-tree sexp) x_displ #f)
0 0 y_displ
)
)
)
)
)
(define (draw-uncontracted-binary-tree sexp ox oy x_displ y_displ diamwidth)
(with-attrib ((:line-width (or diamwidth 0.6)) (:line-cap 'round))
(translate ox oy
(compose
(draw-bincordtree
(fill-cordtree-x-coordinates! (construct-coordinate-tree sexp) x_displ #t)
0 0 y_displ
)
)
)
)
)
(define (draw-n-ary-tree sexp ox oy x_displ y_displ diamwidth)
(with-attrib ((:line-width (or diamwidth 0.6)) (:line-cap 'round))
(translate ox oy
(compose
(draw-cordtree
(normalize-root-to-zero-and-scale! (p->tree-x-coordinates sexp) x_displ)
0 0 y_displ
)
)
)
)
)
;; (fill-cordtree-x-coordinates! (construct-coordinate-tree '((a . (b . c)) . ((d . e) . f))) 1)
;; --> (((0 . 0)) ((-1 . 0) (1 . 0)) ((-3/2 . -1) (-1/2 . -1) (1/2 . 1) (3/2 . 1)) ((-3/4 . -1/2) (-1/4 . -1/2) (1/4 . 1/2) (3/4 . 1/2)))
;; If the car-side of pair (caar level) can be found at
;; cdr-side of some pair of the nextlev, then this is an
;; "internal" edge colored black, otherwise it's a "leaf-edge"
;; colored red.
(define (draw-one-level-of-bincordtree level nextlev ox oy ny)
(let loop ((level level))
(cond ((pair? level)
(compose
(cond
((find (lambda (x) (= (cdr x) (caar level))) nextlev)
(pin-headed-pict-line
(pt (+ ox (caar level)) ny)
(pt (+ ox (cdar level)) oy)
)
)
(else
(pin-headed-pict-line-red
(pt (+ ox (caar level)) ny)
(pt (+ ox (cdar level)) oy)
)
)
)
(loop (cdr level))
)
)
(else the-empty-pict)
)
)
)
;; Draw from top to bottom, because the result is nicer.
(define (draw-bincordtree cot ox oy y_displ)
(let ((revcot (reverse! (cdr cot))))
(compose
(let loop ((ct revcot) (nextlev '())
(y (+ oy (* (length revcot) y_displ)))
)
(cond ((pair? ct)
(compose
(draw-one-level-of-bincordtree
(car ct)
nextlev
ox (- y y_displ) y
)
(loop (cdr ct) (car ct) (- y y_displ))
)
)
(else the-empty-pict)
)
)
((if (eq? (length cot) 1)
pin-headed-pict-line-red ;; For empty trees a red root dot
pin-headed-pict-line ;; For others a black one.
)
(pt (+ ox (caaar cot)) (+ oy (cdaar cot)))
(pt (+ ox (caaar cot)) (+ oy (cdaar cot)))
)
) ;; compose
)
)
(define (draw-bincordtree-bit-older cot ox oy y_displ)
;;(stroke ;; Already stroked, now composing pict's, not paths!
(compose
((if (eq? (length cot) 1)
pin-headed-pict-line ;; For empty trees a black root dot
pin-headed-pict-line-red ;; For others a red one.
)
(pt (+ ox (caaar cot)) (+ oy (cdaar cot)))
(pt (+ ox (caaar cot)) (+ oy (cdaar cot)))
)
(let loop ((ct (cdr cot)) (y oy))
(cond ((pair? ct)
(compose
(draw-one-level-of-bincordtree
(car ct)
(if (pair? (cdr ct)) (cadr ct) '())
ox y (+ y y_displ)
)
(loop (cdr ct) (+ y y_displ))
)
)
(else the-empty-pict)
)
)
) ;; compose
;;)
)
;; (normalize-root-to-zero-and-scale! (p->tree-x-coordinates '(()((())()))) 12)
;; --> (0 (-9) (9 (3 (3)) (15)))
(define (draw-cordtree ct ox oy y_displ)
(stroke
(let recurse ((ct ct) (y oy) (prev_y oy) (prev_x (car ct)))
(cond ((pair? ct)
(compose
(cond ((number? (car ct)) ;; At the beginning of branch-list?
(compose
(pin-headed-line
(pt (+ ox prev_x) prev_y)
(pt (+ ox (car ct)) y)
)
(recurse (cdr ct) (+ y y_displ) y (car ct)) ;; Start scanning sub-branches.
)
)
(else ;; Still scanning the sub-branches.
(compose
(recurse (car ct) y prev_y prev_x) ;; Draw this branch.
(recurse (cdr ct) y prev_y prev_x) ;; Continue sub-branches.
)
)
) ;; cond
) ;; compose
)
(else the-empty-path)
)
)
)
)
(define (width-for-digit-list digit-list scale)
(let* ((font-used (font "Courier" scale))
(str-glyph (simple-string->glyphpath font-used
(string-concatenate (map (lambda (x)
(format #f "~A"
(cond ((< x 10) x)
(else (ascii->char (+ x 55))) ;; 'A' = 65.
)
)
)
digit-list
)
)
)
)
(max-pt (bounding-box:max (bounding-box str-glyph)))
(str-width (pt:x max-pt))
)
str-width
)
)
(define (draw-digit-list digit-list ox oy scale)
(let* ((font-used (font "Courier" scale))
(Lword (simple-string->glyphpath font-used
(string-concatenate (map (lambda (x)
(format #f "~A"
(cond ((< x 10) x)
(else (ascii->char (+ x 55))) ;; 'A' = 65.
)
)
)
digit-list
)
)
)
)
)
(translate ox oy (stroke Lword))
)
)
;; Show also the last zero explicitly!
(define (draw-Lukasiewicz-word sexp ox oy scale)
(draw-digit-list (append! (p->Lw sexp) (list 0)) ox oy scale)
)
(define (draw-A071158-word sexp ox oy scale)
(draw-digit-list (if (null? sexp) (list 0) (sexp->A071158 sexp)) ox oy scale)
)
(define (draw-cardinal-number cardno ox oy scale)
(let* ((font-used (font "Courier" scale))
(ostr (simple-string->glyphpath font-used (format #f "~A" cardno)))
)
(translate ox oy (stroke ostr))
)
)
(define (draw-cardinal-number-right cardno ox oy scale)
(let* ((font-used (font "Courier" scale))
(ostr (simple-string->glyphpath font-used (format #f "~A" cardno)))
(max-pt (bounding-box:max (bounding-box ostr)))
(str-width (pt:x max-pt))
)
(translate (- ox str-width) oy (stroke ostr))
)
)
(define (draw-ordinal-number ordno ox oy scale)
(let* ((font-used (font "Courier" scale))
(ostr (simple-string->glyphpath font-used (format #f "~A." ordno)))
)
(translate ox oy (stroke ostr))
)
)
(define (draw-parenthesization-ugly sexp ox oy scale)
(with-attrib ((:line-width 0.6) (:line-cap 'round))
(let* ((x_now (list ox))
(font-used (font "Courier" scale))
(pars (simple-string->glyphpath font-used (format #f "~S" sexp)))
)
(translate ox oy (stroke pars))
)
)
)
;; Well, we do violence to the kerning here, but we want
;; the parentheses to be exactly in same x-positions as
;; the corresponding digits of TBBS, and slopes of the Dyck path.
(define (draw-parenthesization sexp ox oy scale binexp-also?)
(with-attrib ((:line-width 0.6) (:line-cap 'round))
(let* ((x_now (list ox)) ;; Perfaito?
(s 0)
(letrsize (* 2 scale))
(num-y-level oy)
(par-y-level (if binexp-also? (- oy letrsize) oy))
(font-used (font "Courier" letrsize))
(lpar (char->glyphpath font-used #\())
(rpar (char->glyphpath font-used #\)))
(zero (char->glyphpath font-used #\0))
(one (char->glyphpath font-used #\1))
)
(compose
(let recurse ((p sexp))
(cond ((not (null? p))
(compose (translate (car x_now) par-y-level (stroke lpar))
(if binexp-also?
(translate (car x_now) num-y-level (stroke one))
the-empty-pict
)
(begin
(set! s (+ (* 2 s) 1))
(set-car! x_now (+ (car x_now) scale))
(recurse (car p)) ;; Recurse between.
)
(translate (car x_now) par-y-level (stroke rpar))
(if binexp-also?
(translate (car x_now) num-y-level (stroke zero))
the-empty-pict
)
(begin
(set! s (+ (* 2 s) 0))
(set-car! x_now (+ (car x_now) scale))
(recurse (cdr p)) ;; Recurse the rest.
)
)
)
(else the-empty-pict)
)
) ;; let recurse
(cond (binexp-also?
(let* ((str (format #f "~A" s))
(str-glyph (simple-string->glyphpath font-used str))
(max-pt (bounding-box:max (bounding-box str-glyph)))
(str-width (pt:x max-pt))
)
(translate
(- ox (+ (* 2 scale) str-width)) ;; Was: (- ox (* scale (+ 2 (string-length str))))
num-y-level
(stroke str-glyph)
)
)
)
(else the-empty-pict)
) ;; let recurse
) ;; compose
) ;; let*
) ;;
)
(define (parenthesization->binexp sexp)
(let ((s 0))
(let recurse ((p sexp))
(cond ((not (null? p))
(set! s (+ (* 2 s) 1))
(recurse (car p)) ;; Recurse between.
(set! s (+ (* 2 s) 0))
(recurse (cdr p)) ;; Recurse the rest.
)
)
) ;; let recurse
s
)
)
(define (draw-decimal-tbbs sexp ox oy scale)
(with-attrib ((:line-width 0.6) (:line-cap 'round))
(compose
(let* ((str (format #f "~A" (parenthesization->binexp sexp)))
(letrsize scale)
(font-used (font "Courier" letrsize))
(str-glyph (simple-string->glyphpath font-used str))
(max-pt (bounding-box:max (bounding-box str-glyph)))
(str-width (pt:x max-pt))
)
(translate
(- ox str-width)
oy
(stroke str-glyph)
)
) ;; let*
) ;; compose
) ;;
)
(define (draw-dyck-path sexp ox oy scale linewidth)
(with-attrib ((:line-width (or linewidth 0.6)) (:line-cap 'round))
(let ((x_now (list ox)))
(stroke
(let recurse ((s sexp) (level 0))
(cond ((not (null? s))
(compose
(pin-headed-line ;; Upward slope /
(pt (car x_now) (+ oy (* scale level)))
(pt (+ (car x_now) scale) (+ oy (* scale (+ 1 level))))
)
(begin (set-car! x_now (+ (car x_now) scale))
(recurse (car s) (+ level 1)) ;; Recurse between.
)
(pin-headed-line ;; Downward slope \
(pt (car x_now) (+ oy (* scale (+ 1 level))))
(pt (+ (car x_now) scale) (+ oy (* scale level)))
)
(begin (set-car! x_now (+ (car x_now) scale))
(recurse (cdr s) level) ;; Recurse the rest.
)
) ;; compose
)
((zero? level) ;; We want one dot . for an empty path ().
(pin-headed-line (pt (car x_now) oy) (pt (car x_now) oy))
)
(else the-empty-path)
)
)
) ;; stroke
) ;; let
) ;; with-attrib
)
;; Draw a non-crossing Murasaki-diagram:
(define (draw-rr-diagram sexp x y scale linewidth height-overdrive?)
(let* ((height (or height-overdrive? (* scale (max 3 (count-pars sexp)))))
(y (+ y height))
)
(with-attrib ((:line-width (or linewidth 0.6)) (:line-cap 'round))
(stroke
(let loop ((partitions (add-depths-to-rr-parts! (sexp->pp-qq-rr-cycles sexp))))
(cond ((pair? partitions)
(let* ((part (cdar partitions))
(depth (caar partitions))
(leftmost (car part))
(rightmost (car (last-pair part)))
)
(compose ;; First the horizontal line: (Just a dot if leftmost = rightmost)
(line (pt (+ x (* scale leftmost)) (- y (* scale depth)))
(pt (+ x (* scale rightmost)) (- y (* scale depth)))
)
(let inloop ((part part)) ;; then the vertical lines.
(cond ((null? part) the-empty-path)
(else
(compose
(line (pt (+ x (* scale (car part))) (- y (* scale depth)))
(pt (+ x (* scale (car part))) (- y height))
)
(inloop (cdr part))
)
)
)
)
(loop (cdr partitions))
) ;; compose
)
)
(else the-empty-path)
)
)
)
)
)
)
(define (draw-polygon-triangularization sexp ox oy radius chordwidth)
(draw-chords-with-or-without-circle (bt->pt sexp) radius ox oy #f #f chordwidth #f 0 0 0)
)
(define (draw-n-chords sexp ox oy radius chordwidth perimwidth)
(draw-chords-with-or-without-circle (sexp->hs sexp) radius ox oy #t #t chordwidth perimwidth 1 0 0)
)
(define (draw-qq-chords sexp ox oy radius chordwidth perimwidth)
(draw-chords-with-or-without-circle (sexp->pp-qq-rr sexp) radius ox oy #t #f chordwidth perimwidth 0 0 1)
)
(define (draw-chords-with-or-without-circle chords radius ox oy circle? curved? chordwidth perim_width r g b)
(let ((perimwidth (or perim_width 0.3)))
(with-attrib ((:line-width (or chordwidth 0.5)))
(translate ox (+ oy radius) ;; According to the bottom, not the center.
(compose
(draw-chords chords radius curved?)
(if circle?
(stroke (arc origin (+ radius perimwidth) 0 2pi)
(:color (rgb r g b))
(:line-width perimwidth)
)
the-empty-pict
)
)
)
)
)
)
;;
;; The point 1 is a half-angle clockwise from the angle 3/2 pi
;; The point 2 is one angle and half clockwise from the angle 3/2 pi
;; The point n is a half-angle counter-clockwise from the angle 3/2 pi.
;;
;;
;;
;; angle = (3/2 pi + pi/n) - (v * (2pi/n))
(define (compute-vert-angle v n)
(let ((a (- (+ (/ (* 3 pi) 2) (/ pi n)) ;; Subtract from the angle of n
(* v (/ 2pi n)) ;; ... the v * angle used.
)
))
(if (< a 0) (+ a 2pi) a) ;; Ensure that it is positive angle.
)
)
(define (get-edge-point v n radius)
(let ((angle (compute-vert-angle v n)))
(pt (* radius (cos angle)) (* radius (sin angle)))
)
)
;; g1 and g2 are angles from the origo to the vertices v1 and v2
;; respectively.
;; angle = the mean of the angles g1 and g2, the angle from origo
;; to antiorigo.
;; h = height of equilateral triangle whose base is the line segment v1-v2,
;; and 2h is distance between the origo and the antiorigo
;; The angle between the first and the last vertices is straight down.
(define (draw-anti-arc v1 v2 n radius)
(let* ((g1 (compute-vert-angle
((if (eq? (abs (- v1 v2)) (- n 1)) min max) v1 v2) n))
(g2 (compute-vert-angle
((if (eq? (abs (- v1 v2)) (- n 1)) max min) v1 v2) n))
(h (* radius (cos (/ pi n))))
;; (angle (/ (+ g1 g2) 2)) ;; Doesn't work that way...
(angle (compute-vert-angle
(if (eq? (abs (- v1 v2)) (- n 1)) ;; First & the last vert?
(/ 1 2) ;; Then straight down.
(/ (+ v1 v2) 2) ;; Otherwise their average.
)
n
)
)
(antiorigo (pt (* 2 h (cos angle)) (* 2 h (sin angle))))
)
(arc antiorigo radius (+ pi g1) (+ pi g2))
)
)
(define (draw-chords chords radius curved?)
(stroke
(let ((n (find-max chords)))
(let loop ((chords chords))
(cond ((and (pair? chords) (pair? (car chords)))
(compose
(cond
((= (caar chords) (cdar chords)) ;; Point connected only with itself!?
(arc (get-edge-point (caar chords) n radius) 0.5 0 2pi) ;; Then draw a smallish circle.
)
((and curved?
(> n 2)
(memq (abs (- (caar chords) (cdar chords)))
(list 1 (- n 1)) ;; Neighbours?
)
)
(draw-anti-arc (caar chords) (cdar chords) n radius)
)
(else ;; A straight line.
(line (get-edge-point (caar chords) n radius)
(get-edge-point (cdar chords) n radius)
)
)
) ;; cond
(loop (cdr chords))
) ;; compose
)
(else the-empty-path)
)
)
)
)
)
;; draw-arcs-with-or-without-base: a function for drawing Siteswap-diagrams for
;; Juggling-patterns, somewhat like the figures 4, 5 and 6 in
;;
;; Buhler, Graham et al paper "Juggling Drops and Descents",
;; Amer. Math. Monthly, 101, (no. 6) 1994, 507 - 519.
;; which can be seen at:
;;
;; http://www.cecm.sfu.ca/organics/papers/buhler/paper/html/node2.html
;;
;; arcs: the same syntax as with chords: ((n1 . n2) (n3 . n4) ...)
;; size: distance between the beats.
;; baselinewidth: #f if no baseline is requested, otherwise its width.
;; baselinemargin: margin to the left and right of the beat 1 and max-beat
(define (draw-arcs-with-or-without-base arcs size ox oy downside? arcwidth base_linewidth baselinemargin r g b)
(let ((baselinewidth (or base_linewidth 0.3))
(max-beat (find-max arcs))
)
(with-attrib ((:line-width (or arcwidth 0.5)))
(translate (+ ox baselinemargin) oy
(compose
(draw-arcs arcs max-beat size downside?)
(if base_linewidth
(stroke (line (pt (- baselinemargin) 0)
(pt (+ baselinemargin (* size (- max-beat 1))) 0)
)
(:color (rgb r g b))
(:line-width baselinewidth)
)
the-empty-pict
)
)
)
)
)
)
(define (draw-arcs arcs max-beat size downside?)
(stroke
(let loop ((arcs arcs))
(cond ((and (pair? arcs) (pair? (car arcs)))
(compose
(arc (pt (* size (- (/ (+ (caar arcs) (cdar arcs)) 2) 1))
0
)
(* (/ size 2)
(- (max (caar arcs) (cdar arcs))
(min (caar arcs) (cdar arcs))
)
)
(if downside? pi 0)
(if downside? 2pi pi)
)
(loop (cdr arcs))
) ;; compose
)
(else the-empty-path)
)
)
)
)
;; (outeps (draw-arcs-with-or-without-base '((1 . 5) (2 . 6) (3 . 4) (4 . 8) (5 . 9) (6 . 7) (7 . 11) (8 . 12) (9 . 10)) 20 40 40 #f #f #f 0 0 0 0) "j441.eps")
;; (outeps (draw-arcs-with-or-without-base '((1 . 5) (2 . 6) (3 . 4) (4 . 8) (5 . 9) (6 . 7) (7 . 11) (8 . 12) (9 . 10)) 20 40 40 #f #f 0.5 10 0 0 1) "jb441.eps")
;; 45141 (= 14514): a tennis pattern with shades of 441.
;; This starts with 441 and then changes to 14514.
;; (outeps (draw-arcs-with-or-without-base '((1 . 5) (2 . 6) (3 . 4) (4 . 8) (5 . 10) (6 . 7) (7 . 11) (8 . 9) (9 . 13) (10 . 15) (11 . 12) (12 . 16) (13 . 14) (14 . 18) (15 . 20) (16 . 17) (17 . 21) (18 . 19)) 10 10 50 #f #f 1.5 5 1 0 0) "jb14514.eps")
;; (outeps (draw-arcs-with-or-without-base '((1 . 10) (2 . 5) (3 . 4) (6 . 7) (8 . 9)) 6 0 35 #t #f 0.5 3 1 0 0) "ob54.eps")
(define (out pict filename) (show-w/ps2-text-channel filename pict))
(define (outeps pict filename) (show-w/ps2-text-channel filename pict (:format "EPS")))
(define (read-lists-from infile)
(call-with-input-file infile
(lambda (inport)
(let loop ((sexp (read inport)) (res (list)))
(cond ((eof-object? sexp) (reverse! res))
(else (loop (read inport) (cons sexp res)))
)
)
)
)
)
;; The following functions contain some very ugly constants, until I
;; invent something better...
(define (compose-one-cycle-old sexps radius x_start x_displ y_now)
(let loop ((sexps sexps) (x x_start))
(cond ((not (null? sexps))
(compose
(draw-n-ary-tree (car sexps) x (car y_now) (/ radius 2) (/ radius 2) #f)
(draw-binary-tree (car sexps) x (- (car y_now) (* 3 radius)) (/ radius 2) (/ radius 2) #f)
(draw-polygon-triangularization (car sexps) x (- (car y_now) (* 6 radius)) radius #f)
(draw-n-chords (car sexps) x (- (car y_now) (* 9 radius)) radius #f #f)
(loop (cdr sexps) (+ x x_displ))
)
)
(else the-empty-pict)
)
)
)
;; Some of these interpretations are very Ad Hoc.
;; It would be better, if we could have format (interpretation various-output-position-et-style-attributes ...)
;; instead of a simple symbol.
;; Yes, now we start building something like that.
(define (compose-all-interpretations-for-a-single sexp scale x y interpretations chordwidth perimwidth ordnow)
(let ((somewhat-left (* 4 scale))
(size (count-pars sexp))
)
(cond
((not (pair? interpretations)) the-empty-pict)
(else
(compose
(let* ((desc (car interpretations))
(interp-let (if (pair? desc) (car desc) desc))
(centered? (and (pair? desc) (or (memq ':c desc) (memq ':C desc))))
(x_extra_offset (if (and (pair? desc) (pair? (cdr desc)) (number? (cadr desc))) (cadr desc) 0))
(transl_x
(+ x_extra_offset
(cond (centered? (* 0.5 scale size) 0) (else 0))
)
)
(transl_y 0)
)
(translate transl_x transl_y
(case interp-let
((()) ;; Nil for padding. Draw nothing, except consume some Y-space.
the-empty-pict
)
((a) ;; triangulations
(draw-polygon-triangularization sexp x y scale chordwidth)
)
((d) ;; plane binary trees with 2n+1 vertices
(draw-binary-tree sexp x y (/ scale 2) (/ scale 2) chordwidth)
)
((dU) ;; plane binary trees with 2n+1 vertices, uncontracted
(draw-uncontracted-binary-tree sexp x y (/ scale 2) (/ scale 2) chordwidth)
)
((e) ;; plane (general) trees with n+1 vertices
(draw-n-ary-tree sexp x y (/ scale 2) (/ scale 2) chordwidth)
)
((e+d) ;; plane (general) trees with n+1 vertices, shifted by 0.5 * scale * size points right. + binary trees to their left.
(compose
(translate (* 0.5 scale (count-pars sexp)) 0
(draw-n-ary-tree sexp x y (/ scale 2) (/ scale 2) chordwidth)
)
(draw-binary-tree sexp (- x somewhat-left) y (/ scale 2) (/ scale 2) chordwidth)
)
)
((i) ;; ordinary Dyck paths
(draw-dyck-path sexp x y (/ scale 2) chordwidth)
)
((n) ;; non-crossing handshakes at a circular table.
(draw-n-chords sexp x y scale chordwidth perimwidth)
)
((qq) ;; non-crossing partitions.
(draw-qq-chords sexp x y scale chordwidth perimwidth)
)
((rr) ;; non-crossing Murasaki-diagrams
(draw-rr-diagram sexp (- x (/ scale 2)) y (/ scale 2) chordwidth #f)
)
((A071156)
(draw-cardinal-number (sexp->A071156 sexp) x y scale)
)
((A071158)
(draw-A071158-word sexp x y scale)
)
((L) ;; Lukasiewicz-words
(draw-Lukasiewicz-word sexp x y scale)
)
((Td) ;; Totally balanced binary sequences as a decimal number, as in A014486, right-justified.
(draw-decimal-tbbs sexp x y scale)
)
((Tdsmall) ;; Totally balanced binary sequences as a decimal number, as in A014486, right-justified.
(draw-decimal-tbbs sexp x y (- scale 2)) ;; But with a smaller font. Kludge!
)
((O) ;; Ordinal numbers... (somewhat a kludge...)
(draw-ordinal-number (post-incr! ordnow) (* 4 scale) y scale)
)
((S) ;; Size
(draw-cardinal-number (count-pars sexp) (* 4 scale) y scale)
)
((S*2+1) ;; Size*2 + 1
(draw-cardinal-number (1+ (* 2 (count-pars sexp))) (* 4 scale) y scale)
)
((P) ;; Parenthesizations
(draw-parenthesization sexp x y (/ scale 2) #f)
)
((P+T) ;; Parenthesizations + totally balanced binary sequences.
(draw-parenthesization sexp x y (/ scale 2) #t)
)
) ;; case
) ;; translate
) ;; let
(compose-all-interpretations-for-a-single sexp scale x (- y (* 3 scale)) (cdr interpretations) chordwidth perimwidth ordnow)
) ;; compose
) ;; else
) ;; cond
)
)
(define (compose-one-cycle sexps radius x_start x_displ y_now interpretations ordnow)
(let loop ((sexps sexps) (x x_start))
(cond ((not (null? sexps))
(compose (compose-all-interpretations-for-a-single (car sexps) radius x (car y_now) interpretations #f #f ordnow)
(loop (cdr sexps) (+ x x_displ))
)
)
(else the-empty-pict)
)
)
)
(define (compose-one-partition partition radius x_start x_displ y_now y_required interpretations ordnow)
(let loop ()
(cond ((and (pair? partition) (pair? (cdr partition)) ;; Still something to print?
(> (car y_now) y_required) ;; Still fits in this page?
)
(compose (compose-one-cycle (cadr partition) radius x_start x_displ y_now interpretations ordnow)
(begin
(set-car! y_now (- (car y_now) y_required))
(delete! (cadr partition) partition)
(loop)
)
)
)
(else the-empty-pict)
)
)
)
(define (compose-pictures-of-partition lists radius x_start x_displ y_now y_required interpretations ordnow)
(let loop () ;; ((lists (cdr lists)))
(cond ((and (pair? lists) (pair? (cdr lists)) ;; Still something to print?
(> (car y_now) y_required) ;; Still fits in this page?
)
(begin
;; First, insert our "anchor" to the beginning of partition (an integer, in contrast to list structures), if not already there:
(cond ((not (integer? (caadr lists))) (attach! (length (cadr lists)) (cadr lists))))
(compose (compose-one-partition (cadr lists) radius x_start x_displ y_now y_required interpretations ordnow)
(cond ((and (pair? (cadr lists)) (pair? (cdr (cadr lists)))) ;; Still something to print, but no space...
the-empty-pict
)
(else ;; We exhausted this partition
(delete! (cadr lists) lists)
(loop)
)
)
)
)
)
(else the-empty-pict)