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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
|
%%
%% This is file `grafbase.mp',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% grafbase.dtx (with options: `MP')
%%
%% -------------------------------------------------------------------
%%
%% Copyright 2002--2012, Daniel H. Luecking
%%
%% Mfpic may be distributed and/or modified under the conditions of the
%% LaTeX Project Public License, either version 1.3c of this license or (at
%% your option) any later version. The latest version of this license is in
%% <http://www.latex-project.org/lppl.txt>
%% and version 1.3c or later is part of all distributions of LaTeX version
%% 2008/12/01 or later.
%%
%% Mfpic has maintenance status "author-maintained". The Current Maintainer
%% is Daniel H. Luecking. There are several Base Interpreters associated
%% with mfpic: plain TeX, LaTeX, plain Metafont and plain MetaPost.
%%
if (known grafbaseversion) or (known grafbase):
message "Grafbase (" & jobname & "): You have loaded grafbase more "
& "than once! Please make sure that it is loaded only once.";
endinput;
fi
boolean grafbase; grafbase := true;
string fileversion, filedate;
fileversion := "1.10"; filedate := "2012/12/03";
message " Loading grafbase macros, version " & fileversion & ", " &
filedate & ".";
message " ";
def GBmsg expr s = message "Grafbase (" & jobname & "): " & s;
enddef;
def GBwarn expr s = GBmsg "Warning, " & s; enddef;
def GBerrmsg (expr s) expr t = errhelp t;
errmessage "Grafbase (" & jobname & "): " & s; errhelp "";
enddef;
boolean MFPIC; MFPIC := false;
def checkversions (expr g)=
numeric grafbaseversion; grafbaseversion := g;
if unknown mfpicversion: % no mfpic, or < 0.63
GBmsg "Recent mfpic not detected.";
elseif g = mfpicversion:
MFPIC := true;
else:
message "";
GBwarn "Version mismatch: "
& "mfpic and grafbase versions do not match.";
message "";
fi
enddef;
checkversions (110);
if unknown base_name : input plain;
elseif not string base_name: input plain;
elseif base_name <> "plain": input plain;
fi
if not boolean debug: boolean debug; debug := false; fi
def GBdebug =
begingroup
save >>; def >> = message " " & enddef;
message "Grafbase DEBUG";
enddef;
def GBenddebug =
message "End DEBUG";
>> "";
endgroup
enddef;
vardef mftitle expr t =
if string t: t; message t; fi
enddef;
boolean METAFONT, METAPOST;
METAPOST := known color Carl Philipp Emanuel Bach;
if METAPOST: METAFONT := false; else: METAFONT := true; fi
if METAFONT:
GBerrmsg ("wrong compiler.")
"This file is for Metapost. For Metafont, use grafbase.mf.";
fi
numeric metapostversion;
boolean has_cmyk;
boolean has_filenametemplate; % e.g., filenametemplate "%j.%n;
boolean has_outputtemplate; % e.g., outputtemplate := "%j.%n;
if unknown mpversion:
% prior to introduction of cmykcolor and output filename templates.
% Latest such version was 0.641.
metapostversion := 0.641;
has_cmyk := false;
has_filenametemplate := false;
has_outputtemplate := false;
else:
metapostversion := scantokens (mpversion);
has_cmyk := true;
has_filenametemplate := true;
if metapostversion < 1.200:
has_outputtemplate := false;
else:
has_outputtemplate := true;
fi
fi
pt# := pt; bp# := bp;
def t_ = transformed currenttransform enddef;
if unknown aspect_ratio: aspect_ratio := 1; fi
if unknown hppp: hppp := 1 fi;
if unknown currenttransform:
transform currenttransform;
currenttransform := identity yscaled aspect_ratio;
fi
interim warningcheck := 0;
numeric unitlen, xscale, yscale, xneg, xpos, yneg, ypos;
unitlen := 1 bp#;
xscale := 7.2;
yscale := 7.2;
xneg := 0; xpos := 10;
yneg := 0; ypos := 10;
newinternal deg, pi, radian;
deg := 1; pi := 3.14159;
radian := 57.29578;
numeric degree; degree := deg;
newinternal penwd; penwd := 0.5pt;
pen drawpen;
def resizedrawpen (expr s) =
interim penwd := s;
setvariable (pen) (drawpen) pencircle scaled penwd;
save currentpen; pen currentpen; pickup drawpen;
enddef;
numeric hatchwd; hatchwd := 0.5bp;
pen hatchpen; hatchpen := pencircle scaled hatchwd;
boolean clipall; clipall := false;
boolean ClipOn; ClipOn := false;
path ClipPath[]; numeric ClipPath; ClipPath = 0;
boolean truebbox; truebbox := false;
def DoClip (suffix v) =
if ClipOn and (ClipPath > 0): clipsto (v, ClipPath); fi
enddef;
def noclip (text t) =
hide ( setboolean (ClipOn) false; t)
enddef;
boolean showbbox; showbbox := false;
if not has_cmyk: let rgbcolor = color; let cmykcolor = color; fi
def _wc_ = withcolor enddef;
color currentcolor, drawcolor, fillcolor, hatchcolor,
headcolor, pointcolor, tlabelcolor, background;
currentcolor := fillcolor := drawcolor := hatchcolor :=
headcolor := pointcolor := tlabelcolor := black;
background := white;
vardef snapto expr t =
if numeric t:
if unknown t: 0
elseif t < 0: 0
elseif t > 1: 1
else: t
fi
else:
GBerrmsg ("Improper expression type.")
"The argument to `snapto' must be a numeric.";
fi
enddef;
vardef rgbgray (expr g) = (snapto g) * white enddef;
vardef cmykgray (expr g) = cmyk(0,0,0,1 - snapto g) enddef;
def colorchoice (expr clr) (text D)(text N)(text R)(text C) =
if unknown clr: D
elseif numeric clr: N
elseif rgbcolor clr: R
elseif cmykcolor clr: C
else: D
fi
enddef;
if has_cmyk :
vardef grayscalegray (expr g) = snapto g enddef;
vardef gray (expr g) = grayscalegray (g) enddef;
vardef cmyk (expr c, m, y, k) =
(snapto c, snapto m, snapto y, snapto k)
enddef;
vardef makecmyk primary clr =
colorchoice (clr)(cmykblack)(cmykgray(clr))
(rgbtocmyk(redpart clr,greenpart clr,bluepart clr))
(clr)
enddef;
vardef makergb primary clr =
colorchoice (clr)(rgbblack)(rgbgray(clr))(clr)
(cmyktorgb(cyanpart clr, magentapart clr,
yellowpart clr, blackpart clr))
enddef;
vardef makegray primary clr =
colorchoice (clr)(grayscaleblack)(grayscalegray(clr))
(rgbtogray (redpart clr, greenpart clr, bluepart clr))
(cmyktogray(cyanpart clr, magentapart clr,
yellowpart clr, blackpart clr))
enddef;
vardef iscolor expr clr =
(rgbcolor clr) or (cmykcolor clr) or (numeric clr) or (boolean clr)
enddef;
else:
vardef gray (expr g) = rgbgray(g) enddef;
vardef grayscalegray (expr g) = rgbgray(g) enddef;
vardef cmyk (expr c, m, y, k) = rgb (1-c-k, 1-m-k, 1-y-k) enddef;
vardef makergb primary clr =
colorchoice (clr)(rgbblack)(rgbgray(clr))(clr)()
enddef;
vardef makegray primary clr =
colorchoice (clr)(rgbblack)(rgbgray(clr))
(rgbtogray (redpart clr, greenpart clr, bluepart clr))()
enddef;
def makecmyk = makergb enddef;
vardef iscolor expr clr = (color clr) enddef;
fi
vardef knowncolor expr clr = (known clr) and (iscolor clr) enddef;
vardef forceclr (expr c) =
if unknown c :
if numeric c: grayscaleblack
elseif rgbcolor c: rgbblack
elseif cmykcolor c: cmykblack
else: black
fi
elseif numeric c: gray (c)
elseif iscolor c: c
else: black
fi
enddef;
vardef named (suffix c) = forceclr (c) enddef;
vardef togray (expr r, g, b) =
gray (sqrt((2r*r + 4g*g + b*b)/7))
enddef;
vardef rgbtogray (expr r, g, b) =
togray(snapto r, snapto g, snapto b)
enddef;
vardef cmyktogray (expr c, m, y, k) =
rgbtogray (1-c-k,1-m-k,1-y-k)
enddef;
vardef cmyktorgb (expr c,m,y,k) =
rgb(1-c-k,1-m-k,1-y-k)
enddef;
vardef rgbtocmyk (expr r,g,b) =
cmyk(1-r,1-g,1-b,0)
enddef;
vardef rgb (expr r, g, b) =
(snapto r, snapto g, snapto b)
enddef;
vardef RGB (expr R, G, B) =
rgb (R/255, G/255, B/255)
enddef;
def list (suffix v) (text lst) =
v := 0; for _itm = lst: v[incr v] := _itm; endfor
if v = 0:
GBerrmsg ("No list to process!")
"An attempt was made to produce an array from a "
& "list of expressions having no valid entries.";
fi
enddef;
def map (text proc) (text lst) =
hide (_map := 0;)
for _a = lst:
if _map = 0: hide (_map := 1;) else: , fi
proc (_a)
endfor
enddef;
vardef knownnumericarray suffix arr =
setboolean (_kna) (known arr) and (numeric arr);
if _kna :
_kna := (arr = floor arr) and (arr >= 1);
for _idx = 1 upto arr :
exitif not _kna;
_kna := (known arr[_idx]) and (numeric arr[_idx]);
endfor
fi
_kna
enddef;
def copyarray (suffix src, dest) =
for _idx = 1 upto src: dest[_idx] := src[_idx]; endfor
dest := src;
enddef;
def maparr (text proc) (suffix p) =
for _idx = 1 upto p: proc (p[_idx]); endfor
enddef;
def textpairs = gsetarray (pair) enddef;
def setuniquepairs (suffix p) (text t) =
save p; pair p[];
setpairs (_up) (t);
if _up > 0:
p := 1; p1 := _up1;
for _i = 2 upto _up:
if _up[_i] <> p[p]: p[incr p] := _up[_i]; fi
endfor
else:
p := 0;
fi
enddef;
vardef chpair (text proc) (expr p) =
(proc (xpart p), proc (ypart p))
enddef;
vardef floorpair (expr p) = (floor (xpart p), floor (ypart p))
enddef;
vardef ceilingpair (expr p) = (ceiling (xpart p), ceiling (ypart p))
enddef;
vardef emin (expr a, b) = if a < b: a else: b fi enddef;
vardef emax (expr a, b) = if a > b: a else: b fi enddef;
vardef pairmin (expr z, w) =
( emin (xpart z, xpart w), emin (ypart z, ypart w ) )
enddef;
vardef pairmax (expr z, w) =
( emax (xpart z, xpart w), emax (ypart z, ypart w ) )
enddef;
vardef minpair (suffix p) = setpair (_mp) p1;
for _idx = 2 upto p - 1: _mp := pairmin (_mp, p[_idx]); endfor
pairmin (_mp, p[p])
enddef;
vardef maxpair (suffix p) = setpair (_mp) p1;
for _idx = 2 upto p - 1: _mp := pairmax (_mp, p[_idx]); endfor
pairmax (_mp, p[p])
enddef;
primarydef Z xprod W = (xpart Z * ypart W - xpart W * ypart Z)
enddef;
def force_initial (expr p) (suffix f) =
hide( setnumeric (_n) length f;
f := p
if _n = 0:
{0,0}
else:
..controls post0 (f) and pre 1 (f).. subpath (1,_n) of f
fi;)
enddef;
def force_terminal (expr p) (suffix f) =
hide(setpath (_f) reverse f;
force_initial (p) (_f);
f := reverse _f;)
enddef;
def force_equal_ends (suffix f, g) =
hide(save _p; pair _p;
_p := .5[pnt[length f] (f), pnt0(g)];
force_terminal (_p) (f); force_initial (_p) (g);)
enddef;
def replace_ends_of_cycle (expr p) (suffix f) =
hide(
if cycle f:
save _n; _n := length f;
f := p
if _n = 0: &cycle
else: .. controls post0 (f) and pre 1 (f) ..
if _n = 1: cycle
else: subpath (1, _n - 1) of f ..
controls post[_n - 1](f) and pre[_n](f) .. cycle
fi
fi;
fi)
enddef;
pair thetimes;
numeric _Xtime, _Ytime;
tertiarydef a intersects b =
begingroup
thetimes := a intersectiontimes b;
_Xtime := xpart thetimes;
_Ytime := ypart thetimes;
(_Xtime > -1)
endgroup
enddef;
tertiarydef a misses b = ((a intersectiontimes b) < origin) enddef;
vardef makepicture (expr s) =
if picture s: s
elseif string s: s infont defaultfont scaled defaultscale
elseif path s: picpath (s)
else: nullpicture
fi
enddef;
vardef onepointpath (expr cyclic, q) =
q if cyclic: &cycle else: {0,0} fi
enddef;
vardef fallbackpath (expr cyclic, p) (text t) =
onepointpath (cyclic, p)
enddef;
def even = not odd enddef;
primarydef a divides b =
((b mod a) = 0)
enddef;
def beginimage =
begingroup
newpicture (currentpicture);
enddef;
def endimage =
; currentpicture
endgroup
enddef;
def makeimage (suffix name) (expr refpt) =
setpair (_image_reference_point) zconv (refpt);
setpicture (name) beginimage
enddef;
def concludeimage =
endimage shifted
-_image_reference_point
enddef;
def setvariable (text kind) (suffix name) =
save name; kind name; name :=
enddef;
def gsetvariable (text kind) (suffix name) = kind name; name :=
enddef;
def setnumeric (suffix name) = save name; name := enddef;
def setboolean = setvariable (boolean) enddef;
def setpair = setvariable (pair) enddef;
def setpath = setvariable (path) enddef;
def setpicture = setvariable (picture) enddef;
def setstring = setvariable (string) enddef;
def settransform = setvariable (transform) enddef;
def setpen = setvariable (pen) enddef;
def settension (suffix tn) expr tens =
setnumeric (tn) if tens > 0: tens else: default_tension fi;
enddef;
def fixtension (suffix tn) = if tn < .75: tn := .75; fi enddef;
def newpicture (suffix pic) = setpicture (pic) nullpicture; enddef;
def convertpath (suffix g) expr f = setpath (g) zconv (f); enddef;
def setarray (text kind) (suffix name) =
save name; kind name[]; list (name)
enddef;
def setpairs = setarray (pair) enddef;
def gsetarray (text kind) (suffix name) =
numeric name; kind name[]; list (name)
enddef;
def setbbox (suffix ll, ur) =
save ll, ur; pair ll, ur; getbbox (ll, ur)
enddef;
def setsplit (suffix s) expr ss =
setnumeric (s) emax (1, ceiling ss);
enddef;
if has_cmyk:
def setrgbcolor = setvariable (rgbcolor) enddef;
def setcmykcolor = setvariable (cmykcolor) enddef;
def setcolor (suffix name) expr val =
if boolean val : setboolean
elseif numeric val : setnumeric
elseif rgbcolor val : setrgbcolor
elseif cmykcolor val : setcmykcolor
% this should give a suitable error message:
else: setvariable (color)
fi (name) val;
enddef;
def gsetcolor (suffix name) expr val =
if boolean val : boolean name;
elseif numeric val : numeric name;
elseif rgbcolor val : rgbcolor name;
elseif cmykcolor val : cmykcolor name;
else: color name;
fi name := val;
enddef;
else:
def setrgbcolor = setcolor enddef;
def setcmykcolor = setcolor enddef;
def setcolor = setvariable (color) enddef;
def gsetcolor = gsetvariable (color) enddef;
fi
setcolor(rgbblack) rgb(0,0,0);
setcolor(red) rgb(1,0,0);
setcolor(green) rgb(0,1,0);
setcolor(blue) rgb(0,0,1);
setcolor(rgbwhite) rgb(1,1,1);
setcolor(cmykwhite) cmyk(0,0,0,0);
setcolor(cyan) cmyk(1,0,0,0); % Maybe these should
setcolor(magenta) cmyk(0,1,0,0); % be rbg for backward
setcolor(yellow) cmyk(0,0,1,0); % compatibility?
setcolor(cmykblack) cmyk(0,0,0,1);
setcolor(grayscaleblack) gray(0);
setcolor(grayscalewhite) gray(1);
if has_outputtemplate:
def setoutputtemplate = outputtemplate := enddef;
elseif has_filenametemplate:
def setoutputtemplate = filenametemplate enddef;
else:
def setoutputtemplate text garbage = enddef;
fi
vardef GBromannumeral (expr X) =
save Y, _tmp, U; string U;
Y.m := X div 1000; % thousands digit
_tmp := X - 1000Y.m; % hundreds digits and lower
Y.c := _tmp div 100; % hundreds
_tmp := _tmp - 100Y.c; % tens and units
Y.x := _tmp div 10; % tens
Y.i := _tmp - 10Y.x; % units
strrepeat("m", Y.m) &
GBromandigit("c", "d", "m", Y.c) &
GBromandigit("x", "l", "c", Y.x) &
GBromandigit("i", "v", "x", Y.i)
enddef;
vardef GBromandigit (expr bot, mid, top, n) =
if n > 9 : top & strrepeat(bot, n-10) % shouldn't happen
elseif n > 8 :
bot & top % "ix"
elseif n > 4 : mid & strrepeat (bot, n-5) % "v"--"viii"
elseif n > 3 :
bot & mid % "iv"
else: strrepeat (bot, n) % ""--"iii" for 0--3
fi
enddef;
vardef strrepeat (expr st, rep) =
"" for i = 1 upto rep: & st endfor
enddef;
transform ztr, vtr;
def setztr =
if debug:
GBdebug;
>> "w_ = " & decimal w_ & "bp";
>> "h_ = " & decimal h_ & "bp";
>> "unitlen = " & decimal unitlen & "bp";
>> "xneg = " & decimal xneg;
>> "xpos = " & decimal xpos;
>> "yneg = " & decimal yneg;
>> "ypos = " & decimal ypos;
>> "xscale = " & decimal xscale;
>> "yscale = " & decimal yscale;
GBenddebug;
fi
save ztr, vtr;
transform ztr, vtr;
vtr := identity xscaled xscale yscaled yscale scaled (unitlen*hppp);
ztr := identity shifted (-xneg, -yneg) transformed vtr;
if debug:
GBdebug;
>> "ztr is";
show ztr;
>> "vtr is";
show vtr;
GBenddebug;
fi
enddef;
vardef zconv (expr a) = a transformed ztr enddef;
vardef invzconv (expr a) = a transformed (inverse ztr) enddef;
vardef vconv (expr v) = v transformed vtr enddef;
vardef invvconv (expr v) = v transformed (inverse vtr) enddef;
def active_plane = currentpicture enddef;
boolean overlaylabels, underlaylabels, havebackground;
overlaylabels := false;
underlaylabels := false;
havebackground := false;
def initpic =
setztr;
resizedrawpen (penwd);
if ClipOn: ClipPath := 1;
ClipPath1 := rect (origin, (w_, h_));
fi
if debug:
GBdebug;
>> "Drawing nominal bounding box around picture";
GBenddebug;
noclip ( safedraw rect (origin, (w_, h_)) );
fi
newpicture (foreground_labels);
newpicture (background_labels);
havebackground := false;
save labelbb; pair labelbb.ll, labelbb.ur;
labelbb.ll := labelbb.ur := origin;
enddef;
def mfpicenv = enddef;
def endmfpicenv = enddef;
def bounds (expr a, b, c, d) =
xneg := a; xpos := b;
yneg := c; ypos := d;
enddef;
string extra_beginmfpic; extra_beginmfpic := "";
string extra_endmfpic; extra_endmfpic := "";
def beginmfpic (expr ch) =
beginfig (ch);
gcode := ch;
save w_, h_, d_;
charwd := (xpos-xneg)*xscale*unitlen;
charht := (ypos-yneg)*yscale*unitlen;
chardp := 0;
w_ := charwd;
h_ := charht;
d_ := chardp;
initpic;
scantokens extra_beginmfpic;
enddef;
def endmfpic =
scantokens extra_endmfpic;
if debug:
GBdebug;
>> "width = " & decimal w_ & "bp";
>> "height = " & decimal h_ & "bp";
GBenddebug;
fi
DoClip (active_plane);
if clipall: clipto (active_plane) rect (origin, (w_, h_)); fi
if showbbox: noclip ( safedraw rect (origin, (w_, h_)) ); fi
save _ll, _ur;
pair _ll, _ur;
if truebbox:
_ll := llcorner active_plane;
_ur := urcorner active_plane;
elseif clipall:
_ll := origin;
_ur := (w_,h_);
else: % expand to accomodate labels
_ll := pairmin ((0, 0 ), labelbb.ll);
_ur := pairmax ((w_, h_), labelbb.ur);
fi
_ur := pairmax (_ur, _ll + eps*(1, 1));
setbounds active_plane to rect (_ll, _ur);
if havebackground:
addto background_labels also active_plane;
active_plane := background_labels;
background_labels := nullpicture;
fi
addto active_plane also foreground_labels;
foreground_labels := nullpicture;
endfig;
enddef;
pair label_adjust;
label_adjust := origin;
numeric label_sep, labelpath_sep ;
label_sep := 0; labelpath_sep := 0;
vardef newgblabel (expr hf, vf, BL, r) (expr s) (text pts) =
save _lab, _ll, _ur; picture _lab; pair _ll, _ur;
_lab := makepicture (s);
pathdims (origin, _lab) (_ll, _ur);
readjustdims (_ll, _ur) (label_sep);
_lab := thegblabel (ref_shift (hf, vf, BL, _ll, _ur), r, _lab);
save _b; pair _b;
for _itm = pts:
_b := zconv (_itm);
if overlaylabels:
addto foreground_labels also _lab shifted _b _wc_ tlabelcolor;
elseif underlaylabels:
addto background_labels also _lab shifted _b _wc_ tlabelcolor;
havebackground := true;
else:
addto active_plane also _lab shifted _b _wc_ tlabelcolor;
labelbb.ll := pairmin (_b + llcorner _lab, labelbb.ll);
labelbb.ur := pairmax (_b + urcorner _lab, labelbb.ur);
fi
endfor
enddef;
vardef gblabel (expr a, b, c, d, r) (expr s) (text t) =
newgblabel (b, d, (c = 0) and (d = 0), r) (s) (t);
enddef;
vardef ref_shift (expr hf, vf, BL, ll, ur) =
- ( (hf)[xpart ll, xpart ur],
(vf)[if BL: 0 else: (ypart ll) fi, ypart ur] )
enddef;
vardef thegblabel (expr z, r, p) =
((p shifted z) rotated r) shifted label_adjust
enddef;
vardef textrect (expr lbl, rad, loc) =
textrectx (.5, .5, false, 0) (origin, lbl, rad, loc)
enddef;
vardef textoval (expr lbl, mult, loc) =
xellipse (true, .5, .5, false, 0) (origin, lbl, mult, loc)
enddef;
vardef textellipse (expr lbl, rat, loc) =
xellipse (false, .5, .5, false, 0) (origin, lbl, rat, loc)
enddef;
boolean roundends; roundends := true;
vardef textrectx (expr a, b, c, rot, xy, lbl, rad, loc) =
save ll, ur, _r, f, zz;
pair ll, ur, zz; path f;
pathdims (xy, lbl) (ll, ur);
readjustdims (ll, ur) (labelpath_sep)
_r := if numeric rad: rad
elseif not boolean rad: 0
elseif rad: emin (xpart(ur-ll), ypart (ur-ll))/sqrt(2)
else: 0
fi;
if _r = 0:
f := rect (ll, ur);
else:
save p, q;
pair p[]; path q;
p1 := ur - _r*dir(45);
p3 := ll + _r*dir(45);
p2 := (xpart p3, ypart p1);
p4 := (xpart p1, ypart p3);
q := if _r < 0: reverse fi quartercircle scaled 2_r;
f :=
(q shifted p1)--(q rotated 90 shifted p2)
--(q rotated 180 shifted p3)
--(q rotated -90 shifted p4)--cycle;
fi
readjustdims (ll, ur) (label_sep - labelpath_sep);
invvconv (thegblabel (ref_shift(a, b, c, ll, ur), rot, f))
shifted loc
enddef;
def textovalx = xellipse (true) enddef;
def textellipsex = xellipse (false) enddef;
vardef xellipse (expr aspect, a, b, c, r, xy, lbl, mult, loc) =
if mult = 0:
textrectx (a, b, c, r) (xy, lbl, 0, loc)
else:
save ll, ur, cc, ww, hh, f;
pair ll, ur, cc; path f;
pathdims (xy, lbl) (ll, ur);
readjustdims (ll, ur) (labelpath_sep)
cc := .5[ll, ur];
(ww, hh) = ur - cc;
if (ww = 0) or (hh = 0):
f = (ll--ur);
else:
save aa, bb;
aa := ww ++ if aspect: ww else: hh fi *mult;
bb := hh ++ if aspect: hh else: ww fi /mult;
f := ellipse (cc, aa, bb, 0);
fi
readjustdims (ll, ur) (label_sep - labelpath_sep);
invvconv (thegblabel (ref_shift(a, b, c, ll, ur), r, f))
shifted loc
fi
enddef;
def pathdims (expr xy, lbl) (suffix ll, ur) =
if pair lbl:
ll := xy; ur := lbl;
else:
setpicture (_lbl) makepicture (lbl);
ll := llcorner _lbl;
ur := urcorner _lbl;
fi
enddef;
def readjustdims (suffix ll, ur) (expr s) =
ll := ll - s*(1,1);
ur := ur + s*(1,1);
enddef;
newinternal reallysmall; reallysmall := 3epsilon;
newinternal nottoosmall; nottoosmall := eps/2 + 2epsilon;
def signof (expr X) = if X < 0: - fi enddef;
def TruncateWarn expr s =
GBwarn s & " is too large or undefined, so it will be truncated.";
enddef;
vardef secd primary X =
setnumeric (temp) cosd(X);
if abs(temp) < reallysmall:
TruncateWarn "Secant or Tangent";
temp := signof (temp) reallysmall;
fi
1/temp
enddef;
vardef tand primary X = sind(X)*secd(X) enddef;
vardef cscd primary X =
setnumeric (temp) sind(X);
if abs(temp) < reallysmall:
TruncateWarn "Cosecant or Cotangent";
temp := signof(temp) reallysmall;
fi
1/temp
enddef;
vardef cotd primary X = cosd(X)*cscd(X) enddef;
vardef acos primary X =
if abs X > 1:
TruncateWarn "Argument of arccosine";
angle (signof(X) 1, 0)
else:
angle (X, 1 +-+ X)
fi
enddef;
vardef asin primary X =
if abs X > 1:
TruncateWarn "Argument of arcsine";
angle (0, signof(X) 1)
else:
angle (1 +-+ X, X)
fi
enddef;
vardef atan primary X = angle (1, X) enddef;
vardef sin primary X = sind (X*radian) enddef;
vardef cos primary X = cosd (X*radian) enddef;
vardef tan primary X = tand (X*radian) enddef;
vardef cot primary X = cotd (X*radian) enddef;
vardef sec primary X = secd (X*radian) enddef;
vardef csc primary X = cscd (X*radian) enddef;
vardef degrees (expr t) = t*radian enddef;
vardef radians (expr t) = t/radian enddef;
vardef invcos primary X = radians (acos X) enddef;
vardef invsin primary X = radians (asin X) enddef;
vardef invtan primary X = radians (atan X) enddef;
vardef exp primary X = mexp (256 * X) enddef;
vardef ln primary X = (mlog X) / 256 enddef;
vardef log primary X = ln (X) enddef;
vardef logbase (expr B) primary X = (mlog X)/(mlog B) enddef;
vardef logtwo primary X = logbase( 2) (X) enddef;
vardef logten primary X = logbase(10) (X) enddef;
vardef cosh primary X =
setnumeric (temp) 2 exp (-abs(X));
if temp < reallysmall:
TruncateWarn "Cosh";
temp := reallysmall;
fi
1/temp + temp/4
enddef;
vardef sinh primary X =
setnumeric (temp) 2 exp (-abs(X));
if temp < reallysmall:
TruncateWarn "Sinh";
temp := reallysmall;
fi
signof (X) (1/temp - temp/4)
enddef;
vardef sech primary X =
setnumeric (temp) exp(-(abs (X)));
2temp/(1 + temp*temp)
enddef;
vardef tanh primary X =
setnumeric (temp) exp(-2(abs (X)));
signof (X) (1 - temp)/(1 + temp)
enddef;
vardef csch primary X =
save temp, tempa; temp := exp(-(abs (X)));
tempa := (1 - temp*temp)/2;
if tempa < reallysmall:
TruncateWarn "Csch";
tempa := reallysmall;
fi
signof (X) temp / tempa
enddef;
vardef coth primary X =
setnumeric (temp) tanh(X);
if abs(temp) < reallysmall:
TruncateWarn "Coth";
temp := signof (X) reallysmall;
fi
1/temp
enddef;
vardef acosh primary y =
if y < 1:
TruncateWarn "acosh";
0
else:
ln (y + (y +-+ 1))
fi
enddef;
vardef asinh primary y = ln (y + (y ++ 1)) enddef;
vardef atanh primary y =
if abs (y) < 1:
(ln (1 + y) - ln (1 - y))/2
else:
TruncateWarn "atanh";
signof (y) infinity
fi
enddef;
vardef Arg primary Z = (angle Z)/radian enddef;
vardef Log primary Z = (ln (abs Z), Arg Z) enddef;
vardef cis primary T = dir (T*radian) enddef;
vardef zexp primary Z = (exp (xpart Z)) * cis (ypart Z) enddef;
vardef sgn primary Z = if not (Z = origin): unitvector fi Z
enddef;
vardef zsqrt primary Z =
if Z = origin: origin else: sqrt(abs(Z)) * dir ((angle Z)/2) fi
enddef;
vardef conj primary Z = (xpart Z, -ypart Z) enddef;
primarydef Z zmul W = Z zscaled W enddef;
primarydef Z zdiv W =
Z zmul ( unitvector (conj W) / (abs W) )
enddef;
vardef Moebius (expr A) primary Z =
save _D; pair _D;
_D := (1, 0) + (Z zscaled (conj A));
(Z + A)/(abs _D) rotated (- angle _D)
enddef;
vardef pshdist (expr Z,W) = abs(Moebius(-W)(Z)) enddef;
vardef pshdist_hp (expr Z,W) = abs(Z-W)/abs(Z-conj(W)) enddef;
vardef kelvin (expr Z) =
save tmp_; tmp_ = abs(Z);
if tmp_ = 0:
(infinity, infinity)
elseif tmp_ < reallysmall:
infinity*unitvector Z
else:
(1/tmp_)*unitvector Z
fi
enddef;
vardef polar primary p = (xpart p) * dir (ypart p) enddef;
def id (expr x) = x enddef;
primarydef x**y =
if y=2: x*x
elseif (x = floor x) and (abs y = floor y):
1 for n=1 upto y: *x endfor
else: takepower y of x
fi
enddef;
let ^ = **;
transform T_stack[];
numeric T_stack; T_stack := 0;
def T_push (expr T) = T_stack[incr T_stack] := T; enddef;
def T_pop (suffix $) =
if T_stack > 0:
$ := T_stack[T_stack];
T_stack := T_stack - 1;
fi
enddef;
def bcoords = hide ( T_push (ztr) ) enddef;
def ecoords = hide ( T_pop (ztr); vtr := vectorpart ztr ) enddef;
vardef vectorpart primary T = T shifted -(origin transformed T)
enddef;
def apply_t (text Transformer) =
ztr := identity Transformer transformed ztr;
vtr := vectorpart ztr;
enddef;
def xslant = slanted enddef; % (x+sy, y).
def yslant primary s = % (x, y+sx).
transformed
begingroup
save T; transform T;
origin transformed T = origin;
(1, 0) transformed T = (1, s);
(0, 1) transformed T = (0, 1);
T
endgroup
enddef;
def zslant primary p = % (xu+yv, xv+yu), where p = (u, v).
transformed
begingroup
save T; transform T;
xpart T = ypart T = 0;
xxpart T = yypart T = xpart p;
xypart T = yxpart T = ypart p;
T
endgroup
enddef;
def xyswap = zslant (0, 1) enddef;
def boost primary X = zslant (cosh X, sinh X) enddef;
vardef transformedpath (text Transformer) expr f = f Transformer
enddef;
def rotatedpath (expr p, th) =
transformedpath (
transformed vtr
rotatedaround (p transformed vtr, th)
transformed (inverse vtr)
)
enddef;
def reflectedpath (expr p, q) =
transformedpath (
transformed vtr
reflectedabout (p transformed vtr, q transformed vtr)
transformed (inverse vtr)
)
enddef;
def scaledpath (expr p, s) =
transformedpath (shifted -p scaled s shifted p)
enddef;
def xscaledpath (expr a, s) =
transformedpath (shifted (-a, 0) xscaled s shifted (a, 0))
enddef;
def yscaledpath (expr b, s) =
transformedpath (shifted (0, -b) yscaled s shifted (0, b))
enddef;
def slantedpath = xslantedpath enddef;
def xslantedpath (expr b, s) =
transformedpath (shifted (0, -b) slanted s shifted (0, b))
enddef;
def yslantedpath (expr a, s) =
transformedpath (shifted (-a, 0) yslant s shifted (0, a))
enddef;
def shiftedpath (expr v) = transformedpath (shifted v) enddef;
def xyswappedpath = transformedpath (xyswap) enddef;
vardef partialpath (expr a, b) expr f =
save flag, flo, fhi, lo, hi, n;
boolean flag; flag = true;
convertpath (g) f;
n := length f;
flo := snapto emin(a,b);
if flo = 0:
lo := 0;
elseif flo < 1:
setuplengtharray (cum, tot, idx) g;
flag := false;
lo := gettime (cum, idx) (flo*tot);
else:
lo := n;
fi
fhi := snapto emax (a,b);
if flo = fhi:
hi := lo;
elseif fhi < 1:
if flag: setuplengtharray (cum, tot, idx) g; fi
hi := gettime (cum, idx) (fhi*tot);
else:
hi := n;
fi
if a > b: reverse fi subpath (lo, hi) of f
enddef;
vardef gsubpath (expr a, b) expr f = subpath (a, b) of f enddef;
def setuplengtharray (suffix cum, tot, idx) =
save cum, tot, idx; idx := 0; tot := makelengtharray (cum)
enddef;
vardef pathtime@# (suffix p) =
if @# <= 0: 0
elseif @# >= 1: length p
else:
setuplengtharray (cum, tot, idx) p;
gettime (cum, idx) (@#*tot)
fi
enddef;
vardef pathpoint (expr frac) (suffix p) =
convertpath (_pp) p; pnt[pathtime[frac] (_pp)] (p)
enddef;
def coloraddto (expr clr) (suffix u) (expr v) =
addto u also v _wc_ clr;
enddef;
def orto (suffix u) (expr v) = addto u also v; enddef;
def coloraddon (expr clr) (suffix v) =
addto active_plane also v _wc_ clr;
enddef;
def _orto (suffix u, v) =
addto u also v;
enddef;
vardef interior expr c =
newpicture (v);
addto v contour (c.t_);
v
enddef;
vardef interiors suffix cc =
newpicture (_ints);
for _idx = 1 upto cc:
addto _ints also interior cc[_idx]);
endfor
_ints
enddef;
def clipto (suffix vt) expr c =
if path c:
clip vt to c;
fi
enddef;
def clipsto (suffix vt, cc) =
begingroup
save _cl, _cl_; picture _cl, _cl_; _cl_ := nullpicture;
for _idx = 1 upto cc:
_cl := vt; clip _cl to cc[_idx]; addto _cl_ also _cl;
endfor
vt := _cl_;
endgroup
enddef;
vardef Clipped (suffix vt) expr c =
setpicture (_Cl) vt; clipto (_Cl) c; _Cl
enddef;
vardef picneg (suffix vt) expr c =
setpicture (_cl) vt; clip _cl to c;
newpicture (_pn);
addto _pn also (interior c ) _wc_ fillcolor;
addto _pn also _cl _wc_ background;
_pn
enddef;
def shpath (suffix v) (expr q, f) =
addto v doublepath (f.t_) withpen (q.t_);
enddef;
numeric minpenwd;
minpenwd := .05bp; % 1 pixel at 1440dpi
vardef picpath expr d =
newpicture (v);
if penwd >= minpenwd:
shpath (v, drawpen) (d);
fi
v
enddef;
def picdot (suffix v) (expr w, p) =
addto v also
(w shifted p);
enddef;
vardef setdot (expr apath, sc) =
if cycle apath: interior
else: picpath
fi
(apath scaled emax (sc, minpenwd))
enddef;
vardef shaded (expr clr) expr c =
if cycle c:
newpicture (v);
addto v contour c _wc_ clr;
v
else: picpath c % should we? or just make it null?
fi
enddef;
vardef fillwith (suffix v) (expr pic, dims, ll, ur) =
newpicture (b);
for s = xpart ll step xpart dims until xpart ur:
addto b also pic shifted (s, 0);
endfor
for s = ypart ll step ypart dims until ypart ur:
addto v also b shifted (0, s);
endfor
enddef;
def thatchf (suffix v) (expr CT, sp, a, b) =
begingroup
setnumeric (_sp) signof (ypart b - ypart a) abs(sp);
for _y = _sp*( ceiling ((ypart a)/_sp) ) step _sp until ypart b:
shpath (v, hatchpen)
( ( (xpart a, _y)--(xpart b, _y) ) transformed CT );
endfor
endgroup
enddef;
def axialgradientf (suffix clr, v) (expr theta, sp, a, b) =
begingroup
save _hh, _sp, _nn, _y;
_hh := ypart b - ypart a;
_sp := signof (_hh) abs(sp);
_nn := emax (1, round (_hh/_sp));
_sp := _hh/_nn + signof (_hh) epsilon;
_nn := _nn-1;
setpath (_p) rect ((xpart a, 0),(xpart b, _sp));
_y := ypart a;
for _i = 0 upto _nn:
addto v contour (_p shifted (0,_y)) rotated theta
withcolor clr(_i/_nn);
_y := _y + _sp;
endfor
endgroup
enddef;
def areagradientf (suffix clr, v) (expr sp, tp, a, b) =
begingroup
save _ww, _hh, _sp, _tp, _nn, _mm, _x, _y;
_ww := xpart b - xpart a;
_hh := ypart b - ypart a;
_sp := signof (_ww) abs(sp);
_tp := signof (_hh) abs(tp);
_nn := emax (1, round (_ww/_sp));
_mm := emax (1, round (_hh/_tp));
_sp := _ww/_nn + signof (_ww) epsilon;
_tp := _hh/_mm + signof (_hh) epsilon;
_mm := _mm-1; _nn := _nn-1;
setpath (_p) rect (origin,(_sp,_tp));
_x := xpart a; y_a := ypart a;
for _i = 0 upto _nn:
_y := y_a;
for _j = 0 upto _mm:
addto v contour (_p shifted (_x,_y)) withcolor
clr(_i/_nn,_j/_mm);
_y := _y + _tp;
endfor
_x := _x + _sp;
endfor
endgroup
enddef;
path unitcircle;
unitcircle := fullcircle scaled 2;
def radialgradientf (suffix clr, v) (expr sp, ctr, rad) =
begingroup
save _sp, _r, _nn;
_nn := emax (1, round (rad/sp));
_sp := rad/_nn + epsilon;
_nn := _nn - 1;
_r := _sp;
% fill the small center circle first
addto v contour (unitcircle scaled _r shifted ctr)
withcolor clr(0);
for _i = 1 upto _nn:
addto v contour
(unitcircle scaled (_r + _sp) -- reverse unitcircle scaled _r
--cycle) shifted ctr withcolor clr(_i/_nn);
_r := _r + _sp;
endfor
endgroup
enddef;
def tile (suffix atile) (expr unit, width, height, clipit) =
picture atile.pic; atile.pic := nullpicture;
pair atile.dims;
atile.dims := (width, height)*unit;
begingroup
save active_plane;
def active_plane = atile.pic enddef;
save ztr, vtr; transform ztr, vtr;
ztr := identity scaled unit; vtr := ztr;
save xneg, xpos, yneg, ypos;
xneg := 0; xpos := width; yneg := 0; ypos := height;
save ClipOn; boolean ClipOn;
if clipit:
ClipOn := true;
setarray (path) (ClipPath) (rect(origin, atile.dims));
else:
ClipOn := false;
fi
enddef;
def endtile =
DoClip (active_plane);
endgroup
enddef;
vardef is_tile (suffix atile) =
(known atile.pic ) and (picture atile.pic) and
(known atile.dims) and (pair atile.dims )
enddef;
vardef pnt@# (expr p) = point @# of p enddef;
vardef pre@# (expr p) = precontrol @# of p enddef;
vardef post@# (expr p) = postcontrol @# of p enddef;
numeric bbox_split; bbox_split := 4;
def getbbox (suffix ll, ur) expr g =
ll := llcorner g; ur := urcorner g;
if showbbox: noclip ( safedraw rect (ll, ur) ); fi
enddef;
def getradius (suffix rad) expr g =
setsplit (_s) bbox_split;
rad := abs (pnt0 (g));
for _j = 1 upto length g:
rad := emax(rad, abs(pnt[_j] (g)));
endfor
for _j = 1 upto _s*(length g):
ctrlsradius (rad) subpath ((_j-1)/_s, _j/_s) of g;
endfor
enddef;
def ctrlsradius (suffix rad) expr p =
rad := emax( emax (rad, abs(post0 (p))), abs(pre1 (p) ))
enddef;
def safedraw = colorsafedraw (drawcolor) enddef;
def colorsafedraw (expr clr) expr d =
begingroup
setpicture (v) picpath d;
DoClip (v); coloraddon (clr, v);
endgroup
enddef;
def NoCycle (expr s) expr p =
GBwarn s & " cannot be applied to an open path."
& " The path will be drawn instead.";
safedraw p;
enddef;
def safefill = colorsafefill (fillcolor) enddef;
vardef colorsafefill (expr clr) expr c =
if cycle c:
setpicture (v) interior c;
DoClip (v);
coloraddon (clr, v);
else: NoCycle("fill") c;
fi
enddef;
def safeunfill expr c =
if cycle c: noclip (colorsafefill (background) c);
else: NoCycle("unfill") c;
fi
enddef;
def safeclip expr c =
if cycle c: clipto (active_plane) c;
else: NoCycle("clip") c;
fi
enddef;
def store (suffix fs) expr f =
hide (
if (not path f) and (not pair f):
GBerrmsg ("Improper expression type.")
"The second argument to `store' must be a path or pair.";
fi
if not path fs: path fs; fi
fs := f
)
enddef;
vardef stored (suffix fs) expr f = store (fs) f; f enddef;
def drawn = colordrawn (drawcolor) enddef;
vardef colordrawn (expr clr) expr f =
colorsafedraw (clr) (zconv (f)); f
enddef;
def zigzag = colorzigzag (drawcolor) enddef;
def colorzigzag (expr clr) = colorwiggle (false, clr, 0) enddef;
def sinewave = colorsinewave (drawcolor) enddef;
def colorsinewave = colorwiggle (true) enddef;
vardef colorwiggle (expr smth, clr, tens, blen, elen, len, wid) expr f
=
convertpath (g) f;
setuplengtharray (cumlen, totlen, ct) g;
save B;
if cycle f:
B := 0;
else:
B := abs(blen)/_rescale_factor;
totlen := totlen - B - abs(elen)/_rescale_factor;
fi
setnumeric (n) 2*round (totlen/len*_rescale_factor);
if n < 2:
colorsafedraw (clr) g;
else:
save T, U, X, Y, Z, p;
pair U, X, Y, Z; path p;
T := if cycle f: 0 else: gettime (cumlen, ct) (B) fi;
Z := pnt[T] (g);
p :=if not cycle f: (subpath (0,T) of g)
if smth: {curl 0} ..tension tens.. else: -- fi
fi
for i = 1 upto n:
hide(
T := gettime (cumlen, ct) (B+(i/n)*totlen);
X := Z; Z := pnt[T] (g);
Y := .5[X,Z]; U := sgn (Z-X);
)
(Y + (U zscaled (0, if even i: - fi wid)))
if smth: {U}..tension tens.. else: -- fi
endfor
if cycle f: cycle
else: if smth: {curl 0} fi (subpath (T, length g) of g)
fi;
newpicture (v);
shpath (v, drawpen) (p);
DoClip(v); coloraddon (clr, v);
fi
f
enddef;
def corkscrew = colorcorkscrew (drawcolor) enddef;
vardef colorcorkscrew (expr clr, tens, blen, elen, len, wid) expr f =
convertpath (g) f;
setuplengtharray (cumlen, totlen, ct) g;
save B;
if cycle f:
B := 0;
else:
B := abs(blen)/_rescale_factor;
totlen := totlen - B - abs(elen)/_rescale_factor;
fi
setnumeric (n) round (totlen/len*_rescale_factor);
if n < 2:
colorsafedraw (clr) g;
else:
save T, U, X, Y, Z, p;
pair U, X, Y, Z; path p;
T := if cycle f: 0 else: gettime (cumlen, ct) (B) fi;
Z := pnt[T] (g);
p :=if (not cycle f) and (B > 0): (subpath (0,T) of g)-- fi
for i = 1 upto n:
hide(
T := gettime (cumlen, ct) (B+(i/n)*totlen);
X := Z; Z := pnt[T] (g);
Y := .5[X,Z]; U := sgn (Z-X);
)
(X + (U zscaled (0,-wid))){ U}..tension tens..
(Y + (U zscaled (0, wid))){-U}..tension tens..
endfor
if cycle f: cycle
else:
{U}(Z + (U zscaled (0,-wid)))
if elen <> 0: --(subpath(T, length g) of g) fi
fi;
newpicture (v);
shpath (v, drawpen) (p);
DoClip(v); coloraddon (clr, v);
fi
f
enddef;
def filled = colorfilled (fillcolor) enddef;
vardef colorfilled (expr clr) expr c =
colorsafefill (clr) zconv (c); c
enddef;
vardef unfilled expr c = safeunfill zconv (c); c enddef;
vardef Clip expr c = safeclip zconv (c); c enddef;
numeric shadewd; shadewd := 0.5bp;
path shadedotpath;
shadedotpath := fullcircle;
vardef shade (expr sp) expr f =
convertpath (g) f;
setnumeric (gr) 1 - (.88*abs(shadewd)/sp)**2;
if not cycle g: NoCycle("shade") g;
elseif gr <= 0: safefill g;
else:
colorsafefill (gr*white) g;
fi
f
enddef;
polkadotwd := 5bp;
mindotspace := 1bp;
path polkadotpath; polkadotpath := fullcircle;
vardef polkadot (expr sp) expr f =
convertpath (g) f;
if not cycle g: NoCycle("polkadot") g;
elseif sp <= emax (2*polkadotwd/3, mindotspace):
safefill g;
else:
setbbox (ll, ur) g;
save dx, dy, dshift; pair dshift;
dx := sp/2; dy := dx*sqrt 3;
dshift := (xpart(ur - ll) mod dx, ypart (ur - ll) mod dy)/2;
save p, dims; pair p, dims;
p := ll + dshift;
dims := 2(dx, dy);
setpicture (thepolkadot) setdot (polkadotpath, polkadotwd);
newpicture (v);
fillwith (v) (thepolkadot, dims, p, ur);
fillwith (v) (thepolkadot, dims, p + (dx, dy), ur);
DoClip (v); clipto (v) g;
coloraddon (fillcolor, v);
fi
f
enddef;
def thatch = colorthatch (hatchcolor) enddef;
vardef colorthatch (expr clr) (expr sp, theta) expr f =
convertpath (g) f;
if not cycle g: NoCycle("hatch") g;
elseif sp <= abs(hatchwd): colorsafefill (clr) g;
else:
newpicture (v);
setbbox (ll, ur) g rotated -theta;
thatchf (v, identity rotated theta, sp, ll, ur);
DoClip (v); clipto (v) (g);
coloraddon (clr, v);
fi
f
enddef;
def hhatch (expr sp) = thatch (sp, 0) enddef;
def vhatch (expr sp) = thatch (sp, 90) enddef;
def lhatch (expr sp) = thatch (sp, -45) enddef;
def rhatch (expr sp) = thatch (sp, 45) enddef;
def xhatch = colorxhatch (hatchcolor) enddef;
def colorxhatch (expr clr, sp) =
colorthatch (clr) (sp, 45) colorthatch (clr) (sp, -45)
enddef;
vardef axialgradient (suffix clr) (expr sp, theta) expr f =
convertpath (g) f;
if not cycle g: NoCycle("axialgradient") g;
else:
newpicture (_grd);
setbbox (ll, ur) g rotated -theta;
axialgradientf (clr, _grd) (theta, sp, ll, ur);
DoClip (_grd); clipto (_grd) (g);
_orto (active_plane, _grd);
fi
f
enddef;
vardef areagradient (suffix clr) (expr sp, tp) expr f =
convertpath (g) f;
if not cycle g: NoCycle("areagradient") g;
else:
newpicture (_agr);
setbbox (ll, ur) g;
areagradientf (clr, _agr) (sp, tp, ll, ur);
DoClip (_agr); clipto (_agr) (g);
_orto (active_plane, _agr);
fi
f
enddef;
vardef radialgradient (suffix clr) (expr sp, ctr) expr f =
convertpath (g) f;
if not cycle g: NoCycle("radialgradient") g;
else:
setpair (_ctr) zconv (ctr);
newpicture (_agr);
save _rad;
getradius (_rad) g shifted - _ctr;
radialgradientf (clr, _agr) (sp, _ctr, _rad);
DoClip (_agr); clipto (_agr) (g);
_orto (active_plane, _agr);
fi
f
enddef;
vardef NoTile (suffix atile) expr g =
GBwarn str atile & " is not a valid tile for tess()."
& " The path will be drawn instead.";
safedraw g;
enddef;
vardef tess (suffix atile) expr c =
convertpath (_g) c;
if not cycle _g: NoCycle("tess") _g;
elseif not is_tile (atile): NoTile (atile) _g;
else:
setbbox (_ll, _ur) _g;
newpicture (_ts);
fillwith (_ts) (atile.pic, atile.dims, _ll, _ur);
DoClip (_ts); clipto (_ts) _g;
_orto (active_plane, _ts);
fi
c
enddef;
if unknown segment_split: segment_split := 8; fi
if unknown dashsize: dashsize := 3bp; fi
if unknown dashgap: dashgap := dashsize + 2penwd; fi
if unknown dash_finish: dash_finish := .5; fi
if unknown dash_start: dash_start := .5; fi
if unknown _rescale_factor: _rescale_factor := 0.1in; fi
numeric last_dot_size; last_dot_size := 0;
vardef gendashed (suffix pat) expr f =
convertpath (_g) f;
save _dpat;
if not mkdasharrays (pat) (_dpat):
GBwarn "Dash pattern " & str pat
& " undefined. Path will be drawn instead.";
safedraw _g;
elseif _dpat.rep < 2:
safedraw _g;
else:
save _dl;
forsuffixes _s = start, rep, finish:
_dl._s := 0;
for i = 1 upto _dpat._s:
_dpat._s[i] := _dpat._s[i]/_rescale_factor;
_dl._s := _dl._s + _dpat._s[i];
endfor
endfor
if _dl.rep = 0:
GBwarn "Dash pattern " & str pat & " has length 0. "
& "Path will be drawn instead.";
safedraw _g;
else:
setuplengtharray (_cumlen, _totlen, _ct) _g;
save _n, _sf, _no_dots;
boolean _no_dots; _no_dots := true;
_sf := scale_adjust (_n, _dl) (_totlen);
if _n < 0: safedraw _g;
else:
forsuffixes _s = start, rep, finish:
for _i = 1 upto _dpat._s:
if (_dpat._s[_i] = 0) and _no_dots: _no_dots := false;
else: _dpat._s[_i] := _dpat._s[_i]*_sf;
fi
endfor
_dl._s := _dl._s*_sf;
endfor
if _no_dots:
else:
if unknown plot_pic:
save plot_pic; path plot_pic;
plot_pic := dotpath;
fi;
last_dot_size :=
if known plot_pic.size: plot_pic.size else: penwd fi;
setpicture (dashingdot) makesymbol (plot_pic, last_dot_size);
fi
save _t, _d, _v;
picture _v; _v := nullpicture;
_d0 := 0; _t0 := 0;
dashit (_dpat.start) (_v);
if _n > 0:
save _m; _m := ceiling sqrt(_n);
for _j = 0 step _m until _n - 1:
for _i = 0 upto _m - 1:
exitif (_i + _j) > _n - 1;
_d0 := _dl.start + (_j + _i)*_dl.rep;
_t0 := gettime (_cumlen, _ct) (_d0);
dashit (_dpat.rep) (_v);
endfor
DoClip (_v);
coloraddon (drawcolor, _v);
_v := nullpicture;
endfor
fi
_d0 := _totlen - _dl.finish;
_t0 := gettime (_cumlen, _ct) (_d0);
dashit (_dpat.finish) (_v);
DoClip (_v);
coloraddon (drawcolor, _v);
fi
fi
fi
f
enddef;
vardef makelengtharray (suffix clen) suffix p =
setsplit (_s) segment_split;
numeric clen[];
clen := _s * length p; clen0 := 0;
for _i = 1 upto clen:
clen[_i] := clen[_i-1] + abs (pnt[_i/_s] (p) - pnt[(_i-1)/_s] (p))
/ _rescale_factor;
endfor
clen[clen]
enddef;
vardef scale_adjust (suffix n, pl) (expr lngth) =
n := (lngth - pl.start - pl.finish)/pl.rep;
n := if n < 0: -1 else: round(n) fi;
lngth/(pl.start + emax (n, 0)*pl.rep + pl.finish)
enddef;
vardef gettime (suffix arr, ct) (expr lngth) =
setnumeric (_gtl) emax (arr[ct], emin (arr[arr], lngth));
setsplit (_s) segment_split;
forever: exitif inrange (arr[ct], arr[ct+1]) (_gtl);
next ct;
endfor
if arr[ct] = arr[ct+1]: ct
else: ( ct + (_gtl - arr[ct]) / (arr[ct+1] - arr[ct]) )
fi /_s
enddef;
def next suffix X = X := X + 1; enddef;
def dashit (suffix pos) (suffix pic) =
for _k = 1 upto pos:
if odd _k:
if pos[_k] = 0:
_d1 := _d0; _t1 := _t0;
picdot (pic, dashingdot, pnt [_t0] (_g));
else:
_d1 := _d0 + pos[_k];
_t1 := gettime (_cumlen, _ct) (_d1);
shpath (pic, drawpen) (subpath (_t0, _t1) of _g);
fi
else:
_d0 := _d1 + pos[_k];
_t0 := gettime (_cumlen, _ct) (_d0);
fi
endfor
enddef;
def dashpat (suffix pat) (text t) =
list (pat) (t);
if (pat = 0) or (odd (pat) and (pat > 1)):
pat[incr pat] := 0;
fi
enddef;
vardef mkdasharrays (suffix src, dest) =
save _bad; boolean _bad; _bad := false;
forsuffixes _s = start, rep, finish:
numeric dest._s, dest._s[];
boolean _bad._s;
if knownnumericarray src._s:
copyarray (src._s) (dest._s);
_bad._s := false;
else:
_bad := _bad._s := true;
fi
endfor
% _bad = one of the three arrays not copied.
if _bad:
if knownnumericarray src: _bad := false;
if _bad.rep: % make dest.rep = src
copyarray (src) (dest.rep);
fi
if _bad.start: % shrink first dash to get dest.start
copyarray (src) (dest.start);
dest.start1 := dash_start*src1;
fi
if _bad.finish: % use partial first dash for dest.finish
dest.finish := 1;
dest.finish1 := dash_finish*src1;
fi
fi
fi
not _bad
enddef;
vardef Dashed (expr dlen, dgap) expr f =
save dashes; dashpat (dashes) (dlen, dgap);
gendashed (dashes) f
enddef;
def DASHED = Dashed enddef;
vardef doplot (expr spath, sc, dgap) expr f =
save dots; dashpat (dots) (0, dgap);
setpicture (plot_pic) makesymbol (spath, sc);
plot_pic.size := sc;
gendashed (dots) f
enddef;
path dotpath; dotpath := fullcircle;
def dotted = doplot (dotpath) enddef;
vardef plotnodes (expr symbol, size) expr f =
if size > 0:
save pln; pair pln[];
pln := 0;
for _a = 0 upto (length f) if cycle f: - 1 fi:
pln[incr pln] := pnt[_a] (f);
endfor
dosymbols (drawcolor, symbol, size) (pln);
fi
f
enddef;
def showcontrols = colorshowcontrols (pointcolor) enddef;
vardef colorshowcontrols (expr clr, syma, symb, size) expr f =
save shpre, shpost;
pair shpre[], shpost[];
shpre := 0; shpost := 0;
for a = 0 upto (length f) if cycle f: - 1 fi:
shpre [incr shpre] := pre [a] (f);
shpost[incr shpost] := post[a] (f);
colorsafedraw (clr)
(zconv (shpre[shpre]--pnt[a](f)--shpost[shpost]));
endfor
if size > 0:
if not numeric syma: dosymbols (clr, syma, size) (shpre) ; fi
if not numeric symb: dosymbols (clr, symb, size) (shpost); fi
fi
f
enddef;
def doubledraw = colordoubledraw (drawcolor) enddef;
vardef colordoubledraw (expr clr, sep) expr f =
convertpath (g) f;
colorsafedraw (clr) (parapath ( sep/2) g);
colorsafedraw (clr) (parapath (-sep/2) g);
f
enddef;
vardef centerit (expr pic) =
pic shifted -(0.5[urcorner pic, llcorner pic])
enddef;
vardef makesymbol (expr spath, sc) =
if picture spath :
spath
elseif path spath: setdot (spath, sc)
elseif string spath:
spath infont defaultfont scaled defaultscale
else:
GBwarn "Undefined symbol for plotting, "
& "dotpath will be used instead.";
setdot (dotpath, sc)
fi
enddef;
vardef bpoint (expr ptwd, b) =
fullcircle scaled ptwd shifted b
enddef;
def pointd (expr ptwd, filled) (text t) =
if filled:
plotsymbol (SolidCircle, ptwd) (t);
else:
begingroup;
setboolean (clearsymbols) true;
plotsymbol (Circle, ptwd) (t);
endgroup
fi
enddef;
boolean clearsymbols; clearsymbols := false;
vardef clearable (expr pth) =
if path pth:
( pnt0 (pth) = pnt[length pth] (pth) ) and (not cycle pth)
and (length pth > 0)
else: false
fi
enddef;
def clearopenpath expr f =
if clearable (f): safeunfill f & cycle; fi
enddef;
def plotsymbol = colorplotsymbol (pointcolor) enddef;
def colorplotsymbol (expr clr, spath, sc) (text t) =
if sc > 0:
begingroup
setpairs (_cpls) (t);
if _cpls > 0: dosymbols (clr, spath, sc) (_cpls); fi
endgroup
fi
enddef;
def dosymbols (expr clr, spath, sc) (suffix arr) =
if clearsymbols and clearable (spath):
addsymbols (background, makesymbol (spath&cycle, sc)) (arr);
fi
addsymbols (clr, makesymbol (spath, sc)) (arr);
enddef;
def addsymbols (expr clr, symb) (suffix arr) =
newpicture (_pls);
for _idx = 1 upto arr:
picdot (_pls, symb, zconv (arr[_idx]));
endfor
DoClip (_pls); coloraddon (clr, _pls);
enddef;
def putimage (suffix pic) (text t) =
newpicture (_pti);
for _itm = t:
addto _pti also
(pic shifted zconv (_itm));
DoClip (_pti); addto active_plane also _pti;
_pti := nullpicture;
endfor
enddef;
def arrowdraw (expr hlen) (expr f) =
store (curpath) headpath (hlen, 0, 0) drawn f;
enddef;
def xaxis (expr hlen) = arrowdraw (hlen) ((xneg, 0)--(xpos, 0));
enddef;
def yaxis (expr hlen) = arrowdraw (hlen) ((0, yneg)--(0, ypos));
enddef;
def axes (expr hlen) = xaxis (hlen); yaxis (hlen); enddef;
laxis := baxis := raxis := taxis := 0;
vardef xlow = xneg + laxis enddef;
vardef xhigh = xpos - raxis enddef;
vardef ylow = yneg + baxis enddef;
vardef yhigh = ypos - taxis enddef;
vardef axisline.x = (xlow, 0)--(xhigh, 0) enddef;
vardef axisline.y = (0, ylow)--(0, yhigh) enddef;
vardef axisline.l = axisline.y shifted (xlow, 0) enddef;
vardef axisline.b = axisline.x shifted (0, ylow) enddef;
vardef axisline.r = axisline.y shifted (xhigh, 0) enddef;
vardef axisline.t = axisline.x shifted (0, yhigh) enddef;
vardef axis@# (expr len) = headpath (len, 0, 0) axisline@# enddef;
vardef borderrect =
rect((xlow,ylow),(xhigh,yhigh))
enddef;
vardef between (expr A, B, X) = (A < X) and (X < B) enddef;
vardef inrange (expr A, B, X) = (A <= X) and (X <= B) enddef;
vardef inbounds (expr Z) =
inrange (xlow, xhigh) (xpart Z) and inrange (ylow, yhigh) (ypart Z)
enddef;
tertiarydef X isbetween P = between (xpart P, ypart P, X) enddef;
tertiarydef X isinrange P = inrange (xpart P, ypart P, X) enddef;
tertiarydef P contains X = between (xpart P, ypart P, X) enddef;
numeric inside, outside, centered, onleft, onright, ontop, onbottom;
inside := -2;
outside := -1;
onright := 1;
onleft := 2;
centered := .5[onright, onleft];
onbottom := onright;
ontop := onleft;
ltick := rtick := ttick := btick := inside;
xtick := ytick := centered;
vardef axismarks (expr inang, tp, loc, pdir) (expr len) (text t) =
save _tp, _U, _P, _tic, _ticang;
pair _U, _P; path _tic;
_ticang := if tp < 0: inang else: 90 fi;
_tp := abs(tp) - 1;
_U := unitvector (vconv (pdir)) rotated _ticang;
_tic := (-_U--(0,0)) shifted (_tp*_U) scaled len;
for _a = t:
safedraw (_tic shifted zconv (loc + _a*pdir));
endfor
enddef;
def xmarks = axismarks ( 90, xtick, origin, right) enddef;
def ymarks = axismarks (-90, ytick, origin, up) enddef;
def lmarks = axismarks (-90, ltick, (xlow, 0), up) enddef;
def bmarks = axismarks ( 90, btick, (0, ylow), right) enddef;
def rmarks = axismarks ( 90, rtick, (xhigh, 0), up) enddef;
def tmarks = axismarks (-90, ttick, (0, yhigh), right) enddef;
path griddotpath; griddotpath := fullcircle;
def grid = vargrid (0.5bp) enddef;
vardef vargrid (expr dsize, xsp, ysp) =
save gdot, gridpic; picture gdot, gridpic;
gdot := setdot (griddotpath, dsize);
gridpic := nullpicture;
for n = ceiling ((xlow)/xsp) upto floor ((xhigh)/xsp):
for m = ceiling ((ylow)/ysp) upto floor ((yhigh)/ysp):
picdot (gridpic, gdot, zconv ((n*xsp, m*ysp)));
endfor
endfor
coloraddon (pointcolor, gridpic);
enddef;
def vgrid = vargrid enddef;
def hgridlines (expr ysp) =
for n = ceiling ((ylow)/ysp) upto floor ((yhigh)/ysp):
safedraw zconv ((xlow, n*ysp)--(xhigh, n*ysp));
endfor
enddef;
def vgridlines (expr xsp) =
for n = ceiling ((xlow)/xsp) upto floor ((xhigh)/xsp):
safedraw zconv ((n*xsp, ylow)--(n*xsp, yhigh));
endfor
enddef;
def gridlines (expr xsp, ysp) =
vgridlines (xsp); hgridlines (ysp);
enddef;
def vectorfield (expr len, xsp, ysp) (text fcn) (text cond) =
save _vf, _is_OK;
vardef _vf (expr x,y) = ((0,0)--(fcn)) shifted (x,y) enddef;
vardef _is_OK (expr x,y) = cond enddef;
mkvectorfield (len, xsp, ysp) (_vf, _is_OK);
enddef;
vardef mkvectorfield (expr len, xsp, ysp) (suffix vf, isOK) =
for n = ceiling ((xlow)/xsp) upto floor ((xhigh)/xsp):
for m = ceiling ((ylow)/ysp) upto floor ((yhigh)/ysp):
if isOK (n*xsp,m*ysp): arrowdraw (len) (vf(n*xsp,m*ysp)); fi
endfor
endfor
enddef;
def plrvectorfield (expr len, rsp, tsp) (text fcn) (text cond) =
save _vf, _is_OK, _A, _B, _C, _D;
_A := xlow; _B := xhigh;
_C := ylow; _D := yhigh;
vardef _vf (expr r,t) = ((0,0)--(fcn)) shifted (r*dir t) enddef;
vardef _is_OK (expr r,t) =
save _X, _Y; _X := r*cosd t; _Y := r*sind t;
(cond) and between (_A, _B) (_X) and between (_C, _D) (_Y)
enddef;
mkplrvectorfield (len, rsp, tsp) (_vf, _is_OK);
enddef;
vardef mkplrvectorfield (expr len, rsp, tsp) (suffix vf, isOK) =
save rmin, rmax, tmin, tmax;
getpolarbounds;
if rmin = 0:
if isOK (0,tmin): arrowdraw (len) (vf (0,tmin)); fi
rmin := rsp;
fi
for n = ceiling (rmin/rsp) upto floor (rmax/rsp):
for m = ceiling (tmin/tsp) upto floor (tmax/tsp):
if isOK (n*rsp,m*tsp): arrowdraw (len) (vf (n*rsp,m*tsp)); fi
endfor
endfor
enddef;
def patcharcs (suffix X) (expr rstart, rstop, rstep, tstart, tstop) =
for rad = (if rstart = 0: rstep else: rstart fi)
step rstep until rstop:
orto (X, picpath zconv (arcplr (origin, tstart, tstop, rad)) );
endfor
enddef;
def patchrays (suffix X) (expr tstart, tstop, tstep, rstart, rstop) =
for _ang = tstart step tstep until tstop:
orto (X) (picpath zconv ((rstart*dir _ang)--(rstop*dir _ang)));
endfor
enddef;
def plrpatch (expr rstart, rstop, rstep, tstart, tstop, tstep) =
begingroup
newpicture (v);
patcharcs (v) (rstart, rstop, rstep, tstart, tstop);
coloraddon (drawcolor, v);
v := nullpicture;
patchrays (v) (tstart, tstop, tstep, rstart, rstop);
coloraddon (drawcolor, v);
endgroup
enddef;
def gridarcs (expr rstep) =
beginpolargrid;
if rmin = 0:
picdot (gridpic, setdot (griddotpath, penwd), zconv (origin));
fi
rmin := rstep * floor (rmin/rstep + 1);
rmax := rstep * ceiling (rmax/rstep - 1);
patcharcs (gridpic) (rmin, rmax, rstep, tmin, tmax);
endpolargrid (drawcolor, .5penwd);
enddef;
def gridrays (expr tstep) =
beginpolargrid;
tmin := tstep * ceiling (tmin/tstep);
tmax := tstep * floor (tmax/tstep);
patchrays (gridpic) (tmin, tmax, tstep, rmin, rmax);
endpolargrid (drawcolor, .5penwd);
enddef;
def polargrid (expr rstep, tstep) =
gridarcs (rstep); gridrays (tstep);
enddef;
def polargridpoints (expr dsize, rstep, tstep) =
beginpolargrid;
setpicture (gdot) setdot (griddotpath, dsize);
if rmin = 0:
picdot (gridpic, gdot, zconv (origin));
rmin := rstep;
fi
for n = ceiling (rmin/rstep) upto floor (rmax/rstep):
for m = ceiling (tmin/tstep) upto floor (tmax/tstep):
picdot ( gridpic, gdot, zconv ( polar ((n*rstep, m*tstep)) ) );
endfor
endfor
endpolargrid (pointcolor, .5dsize);
enddef;
def beginpolargrid =
begingroup;
save rmax, rmin, tmax, tmin;
getpolarbounds;
newpicture (gridpic);
enddef;
def getpolarbounds =
save p, r, t;
pair p[];
p0 := (xneg, yneg); p1 := (xneg, ypos);
p2 := (xpos, ypos); p3 := (xpos, yneg);
r0 := abs(p0); rmax := r0;
for j = 1 upto 3:
r[j] := abs(p[j]);
if rmax < r[j]: rmax := r[j]; fi
endfor
rmin := 0;
if between (xneg, xpos) (0) and between (yneg, ypos) (0):
tmin := 0; tmax := 360;
elseif (p0 = origin): tmin := 0; tmax := 90;
elseif (p1 = origin): tmin := -90; tmax := 0;
elseif (p2 = origin): tmin := -180; tmax := -90;
elseif (p3 = origin): tmin := 90; tmax := 180;
else:
tmax := tmin := t0 := angle p0;
for j = 1 upto 3:
t := t0 + anglefromto (p0, p[j]);
if tmax < t: tmax := t; fi
if tmin > t: tmin := t; fi
endfor
if between (xneg, xpos) (0):
rmin := emin (abs(yneg), abs(ypos));
elseif between (yneg, ypos) (0):
rmin := emin (abs(xneg), abs(xpos));
else:
rmin := min (r0, r1, r2, r3);
fi
fi
enddef;
def endpolargrid (expr clr, size)=
clipto (gridpic) rect ( zconv ((xneg, yneg)) - size*(1,1),
zconv ((xpos, ypos)) + size*(1,1) );
coloraddon (clr, gridpic);
endgroup
enddef;
vardef polarpatch (expr rstart, rstop, rstep, tstart, tstop, tstep) =
plrpatch (rstart, rstop, rstep, tstart, tstop, tstep);
safedraw zconv ( arcplr (origin, tstart, tstop, rstop) );
safedraw zconv ( ((rstart, 0)--(rstop, 0)) rotated tstop );
enddef;
vardef rect (expr ll, ur) =
ll--(xpart ur, ypart ll)--ur--(xpart ll, ypart ur)--cycle
enddef;
vardef triangle (expr A, B, C) = A--B--C--cycle enddef;
vardef regularpolygon (expr n) (suffix Bob) (text eqns) =
pair Bob[]; Bob := emax (round (abs (n)), 2);
eqns;
for _uncle = 1 upto Bob - 1:
(Bob1 - Bob0) rotated (360/Bob*_uncle) = Bob[_uncle+1] - Bob0;
endfor
mkpoly (true) (Bob)
enddef;
vardef altitudept expr n of t =
save A, B, C, zz; pair A, B, C, zz;
B := pnt[n + 1] (t);
C := pnt[n + 2] (t);
zz = whatever[B,C];
zz = pnt[n](t) + whatever*((C-B) rotated 90);
zz
enddef;
vardef altitude expr n of t =
(pnt[n](t))--(altitudept n of t)
enddef;
vardef medianpt expr n of t =
0.5[pnt[n + 1] (t), pnt[n + 2] (t)]
enddef;
vardef median expr n of t =
(pnt[n](t))--(medianpt n of t)
enddef;
vardef anglebisectorpt expr n of t =
save A, B, C; pair A, B, C;
A := pnt[n ] (t);
B := pnt[n + 1] (t);
C := pnt[n + 2] (t);
save zz; pair zz;
zz = whatever[B,C];
zz = A + whatever*((B-A) rotated (.5*cornerangle (A,B,C)));
zz
enddef;
vardef anglebisector expr n of t =
(pnt[n](t))--(anglebisectorpt n of t)
enddef;
vardef anglefromto (expr u, v) =
if (u = origin) or (v = origin): 0
else: angle (v rotated (-angle u))
fi
enddef;
vardef cornerangle (expr A, B, C) =
if (A = B) or (A = C) :
if (B = C) : 60
else: 90
fi
else: anglefromto (B - A, C - A)
fi
enddef;
vardef mkpath (expr smooth, tens, cyclic) (suffix pts) =
if smooth: mksmooth (tens)
else: mkpoly
fi (cyclic, pts)
enddef;
vardef mkpoly (expr cyclic) (suffix pts) =
for _i = 1 upto pts-1: pts[_i]-- endfor
pts[pts] if cyclic: -- cycle else: {0,0} fi
enddef;
vardef polyline (expr cyclic) (text t) =
setpairs (_pl) (t);
if _pl=0: NoPoints ("polyline", _pl); fi
mkpoly (cyclic, _pl)
enddef;
def NoPoints (expr s) (suffix pts) =
GBwarn s & " attempted with empty list."; pts[incr pts] := origin;
enddef;
vardef turtle (text t) =
setnumeric (_tu) 0;
setpair (_tmp) origin;
pair _tu[];
for _a = t:
_tmp := _tmp + _a;
_tu[incr _tu] := _tmp;
endfor
if _tu = 0: NoPoints("turtle", _tu); fi
mkpoly (false, _tu)
enddef;
vardef brownianpath (expr start, num, sc) =
setnumeric (_brp) 1;
setpair (_tmp) start;
pair _brp[]; _brp1 := _tmp;
for _idx := 1 upto num:
_tmp := _tmp + sc/(sqrt 2)*(normaldeviate,normaldeviate);
_brp[incr _brp] := _tmp;
endfor
mkpoly (false, _brp)
enddef;
vardef randomwalk (expr start, num, dst) =
setnumeric (_rdw) 1;
setpair (_tmp) start;
pair _rdw[]; _rdw1 := _tmp;
for _idx := 1 upto num:
_tmp := _tmp + dst*dir(uniformdeviate(360));
_rdw[incr _rdw] := _tmp;
endfor
mkpoly (false, _rdw)
enddef;
vardef browniangraph (expr num, scst) =
setnumeric (_brg) 1;
pair _tmp, _brg[]; _tmp := _brg1 := (0,0);
for _idx := 1 upto num:
_tmp := _tmp + scst*(1,normaldeviate);
_brg[incr _brg] := _tmp;
endfor
mkpoly (false, _brg)
enddef;
vardef mksmooth (expr tens, cyclic) (suffix pts) =
if pts = 1: onepointpath (cyclic, pts1)
else:
settension (_tn) tens; fixtension (_tn);
pts1 if cyclic: {pts[2]-pts[pts]} fi
for _i = 2 upto pts-1:
..tension _tn..pts[_i]{pts[_i+1]-pts[_i-1]}
endfor
..tension _tn..pts[pts]
if cyclic: {pts[1]-pts[pts-1]}..tension _tn..cycle fi
fi
enddef;
vardef mktenser (expr tens, cyclic) (suffix pts) =
if pts = 1: onepointpath (cyclic, pts1)
else:
settension (_tn) tens; fixtension (_tn);
pts1 if cyclic: {pts[2]-pts[pts]} fi
for _i = 2 upto pts-1:
..tension atleast _tn..pts[_i]{pts[_i+1]-pts[_i-1]}
endfor
..tension atleast _tn..pts[pts]
if cyclic: {pts[1]-pts[pts-1]}..tension atleast _tn..cycle fi
fi
enddef;
vardef mkconvex (expr tens, cyclic) (suffix pts) =
save _B, _d, _tmp; pair _d[];
settension (_tn) tens; fixtension (_tn);
if pts < 4: mktenser (_tn, cyclic) (pts)
else:
for _j = 2 upto pts - 1:
_B[_j] := sqrt(abs((pts[_j]-pts[_j-1])xprod(pts[_j+1]-pts[_j])));
endfor
if cyclic:
_B1 := sqrt(abs((pts1 - pts[pts])xprod(pts2 - pts1)));
_B[pts] := sqrt(abs((pts[pts]-pts[pts-1])xprod(pts1 - pts[pts])));
else:
_B1 := _B2;
_B[pts] := _B[pts-1];
fi
for _j = 2 upto pts - 1:
_tmp := _B[_j-1] + _B[_j+1];
_d[_j] :=
if _tmp = 0: origin % signal to use curl1
else:
( _B[_j+1]*(pts[_j] - pts[_j-1]) +
_B[_j-1]*(pts[_j+1] - pts[_j]) )/_tmp
fi;
endfor
if cyclic:
_tmp := _B[pts] + _B2;
_d1 :=
if _tmp = 0: origin
else:
(_B2*(pts1 - pts[pts]) + _B[pts]*(pts2 - pts1))/_tmp
fi;
_tmp := _B[pts-1] + _B1;
_d[pts] :=
if _tmp = 0: origin
else:
( _B1*(pts[pts] - pts[pts-1]) +
_B[pts-1]*(pts1 - pts[pts]) )/_tmp
fi;
else:
_d1 := origin; _d[pts] := origin;
fi
pts1
for _j = 1 upto pts-1:
{if _d[_j] = origin: curl1 else: _d[_j] fi}
..tension atleast _tn..pts[_j+1]
endfor
{if _d[pts] = origin: curl1 else: _d[pts] fi}
if cyclic: ..tension atleast _tn..cycle fi
fi
enddef;
numeric default_tension; default_tension := 1;
def curve = tcurve (default_tension) enddef;
vardef tcurve (expr tens, cyclic) (text t) =
setpairs (_tc) (t);
if _tc=0: NoPoints("curve", _tc); fi
mksmooth (tens, cyclic, _tc)
enddef;
def ccurve = tccurve (default_tension) enddef;
vardef tccurve (expr tens, cyclic) (text t) =
setuniquepairs (_tcc) (t);
if _tcc=0: NoPoints("ccurve", _tcc); fi
mkconvex (tens, cyclic, _tcc)
enddef;
vardef mkbezier (expr tens, cyclic) (suffix pts) =
settension (_tn) tens; fixtension (_tn);
pts1
for _i = 2 upto pts: ..tension _tn..pts[_i] endfor
if cyclic: ..tension _tn..cycle else: {0,0} fi
enddef;
def bezier = tbezier (default_tension) enddef;
vardef tbezier (expr tens, cyclic) (text t) =
setpairs (_tbs) (t);
if _tbs=0: NoPoints ("bezier", _tbs); fi
mkbezier (tens, cyclic) (_tbs)
enddef;
vardef mkqbezier (expr cyclic) (suffix pts) =
pts1
if pts=1: {0,0}
else:
for _i = 2 step 2 until pts - 1:
..controls 1/3[pts[_i], pts[_i-1]]
and 1/3[pts[_i], pts[_i+1]].. pts[_i+1]
endfor
if cyclic:
..controls 1/3[ pts[pts], pts[pts - 1] ]
and 1/3[ pts[pts], pts1 ]..cycle
fi
fi
enddef;
vardef qbezier (expr cyclic) (text t) =
setpairs (_qbz) (t);
if _qbz=0: NoPoints ("qbezier", _qbz);
else:
if (cyclic and odd _qbz) or (not cyclic and even _qbz):
_qbz[incr _qbz] := _qbz[_qbz-1];
fi
mkqbezier (cyclic) (_qbz)
fi
enddef;
vardef mkcbezier (expr cyclic) (suffix pts) =
pts1
if pts=1: {0,0}
else:
for _i = 1 step 3 until pts - 3:
..controls pts[_i+1] and pts[_i+2] .. pts[_i+3]
endfor
if cyclic:
..controls pts[pts - 1] and pts[pts]..cycle
fi
fi
enddef;
vardef cbezier (expr cyclic) (text t) =
setpairs (_cbz) (t);
if _cbz=0: NoPoints ("qbezier", _cbz);
else:
% Need 0 mod 3 for cyclic, otherwise 1 mod 3
setnumeric (_mdt) _cbz mod 3;
if cyclic:
if _mdt <> 0: _cbz[incr _cbz] := _cbz[_cbz-1]; fi
if _mdt = 1 : _cbz[incr _cbz] := _cbz1; fi
else: % need 1 more, duplicate next to last
if _mdt = 0:
_cbz := _cbz + 1;
_cbz[_cbz] := _cbz[_cbz-1];
_cbz[_cbz-1] := _cbz[_cbz-2];
fi
if _mdt = 2: % need 2 more, duplicate last 2.
_cbz := _cbz + 2; % add 2 slots
_cbz[_cbz] := _cbz[_cbz-2]; % fill them
_cbz[_cbz-1] := _cbz[_cbz-2]; % with last node
_cbz[_cbz-2] := _cbz[_cbz-3]; % orig last slot = orig previous.
fi
fi
mkcbezier (cyclic) (_cbz)
fi
enddef;
vardef fcncontrol (expr ftens, X, Y, Z) =
Y if (xpart(Z-Y) <> 0) and (xpart(Y-X) <> 0):
+ xpart(Z-Y)/3/xpart(Z-X)*(Z - X)/ftens fi
enddef;
vardef mkfcnpath (expr ftens) (suffix q) =
settension (_tn) ftens;
if _tn <= 0: _tn := 1; fi
for _i = 1 upto q - 1:
q[_i]..controls fcncontrol (_tn) (q[_i-1], q[_i], q[_i+1])
and fcncontrol (_tn) (q[_i+2], q[_i+1], q[_i])..
endfor
q[q]{0,0}
enddef;
def fcncurve = functioncurve (default_tension) enddef;
def tfcncurve = functioncurve enddef;
vardef functioncurve (expr ftens) (text t) =
settension (_ftens) ftens; if _ftens < 1/3: _ftens := 1/3; fi
setuniquepairs (_fc) (t);
if _fc=0: NoPoints ("functioncurve", _fc); fi
if _fc > 1: _fc0 := _fc1; _fc[_fc+1] := _fc[_fc]; fi
mkfcnpath (_ftens) (_fc)
enddef;
def openqbs = qspline (false) enddef;
def closedqbs = qspline (true) enddef;
vardef mkqbs (suffix b) =
0.5[ b1, b2]
if b<3: {0,0}
else:
for _i = 2 upto b-1:
..controls 1/6[ b[_i], b[_i-1] ] and 1/6[ b[_i], b[_i+1] ]..
0.5[ b[_i], b[_i+1] ]
endfor
fi
enddef;
vardef qspline (expr cyclic) (text t) =
setpairs (_qs) (t);
if _qs=0: NoPoints ("qspline", _qs); fi
if _qs=1: _qs[incr _qs] := _qs1; fi
if cyclic:
_qs[incr _qs] := _qs1; _qs[incr _qs] := _qs2;
fi
mkqbs (_qs) if cyclic: & cycle fi
enddef;
vardef mkcbs (suffix b) =
(b[1]+4b[2]+b[3])/6
if b < 4: {0,0}
else:
for _i = 3 upto b-1:
..controls 1/3[ b[_i-1], b[_i] ] and 1/3[ b[_i], b[_i-1] ]
.. (b[_i-1] + 4b[_i] + b[_i+1])/6
endfor
fi
enddef;
def mkopencbs = mkcbs enddef;
vardef mkclosedcbs (suffix b) =
mkcbs (b) & opencbs (b[b-2],b[b-1],b[b], b1, b2, b3) & cycle
enddef;
def opencbs = cspline (false) enddef;
def closedcbs = cspline (true) enddef;
vardef cspline (expr cyclic) (text t) =
setpairs (_cs) (t);
if _cs=0: NoPoints ("cspline", _cs); fi
for _idx = _cs upto 2: _cs[incr _cs] := _cs[_idx]; endfor
if cyclic:
for _idx = 1 upto 3: _cs[incr _cs] := _cs[_idx]; endfor
fi
mkcbs (_cs) if cyclic: & cycle fi
enddef;
def init_spline_eqns (suffix pts) =
save _spl_pre, _spl_post;
pair _spl_pre[], _spl_post[];
for j= 2 upto pts - 1:
_spl_post[j] + _spl_pre[j] = 2pts[j];
_spl_pre[j+1]+2_spl_pre[j] = 2_spl_post[j]+_spl_post[j-1];
endfor
enddef;
def closed_spline_eqns (suffix pts) =
_spl_post1 + _spl_pre1 = 2pts1;
_spl_post[pts] + _spl_pre[pts] = 2pts[pts];
_spl_pre2 + 2_spl_pre1 = 2_spl_post1 + _spl_post[pts];
_spl_pre1+2_spl_pre[pts] = 2_spl_post[pts]+_spl_post[pts-1];
enddef;
def relaxed_spline_eqns (suffix pts) =
_spl_pre2 + pts1 = 2_spl_post1;
pts[pts] + _spl_post[pts-1] = 2_spl_pre[pts];
enddef;
vardef mksplinepath (expr closed) (suffix pts) =
pts1..controls _spl_post1 and
for j = 2 upto pts if not closed: -1 fi:
_spl_pre[j]..pts[j]..controls _spl_post[j] and
endfor
if closed: _spl_pre1..cycle else: _spl_pre[pts]..pts[pts] fi
enddef;
def mkspline (expr closed) (suffix pts) =
init_spline_eqns (pts);
if closed: closed_spline_eqns (pts);
else: relaxed_spline_eqns (pts);
fi
mksplinepath (closed) (pts)
enddef;
vardef dospline (expr closed) (text the_list) =
setpairs (_sp) (the_list);
if _sp=0: NoPoints ("dospline", _sp); fi
if _sp=1: _sp[incr _sp] := _sp1; fi
mkspline (closed) (_sp)
enddef;
def init_fcnspl_eqns (suffix pts) =
save _dx, _sl; numeric _dx[], _sl[];
_dx1 := xpart (pts2 - pts1);
for j = 2 upto pts - 1:
_dx[j] := xpart (pts[j+1] - pts[j]);
_sl[j + 1]*_dx[j] + _sl[j-1]*_dx[j-1] + 2_sl[j]*(_dx[j] + _dx[j-1])
= 3*ypart(pts[j+1] - pts[j-1]);
endfor
enddef;
def periodic_fcnspl_eqns (suffix pts) =
_sl1 = _sl[pts];
_sl2*_dx1 + 2_sl1*_dx1 + 2_sl[pts]*_dx[pts-1] + _sl[pts-1]*_dx[pts-1]
= 3 * ypart (pts[2] - pts[pts-1]);
enddef;
def relaxed_fcnspl_eqns (suffix pts) =
_sl2*_dx1 + 2_sl1*_dx1 = 3 * ypart(pts2 - pts1);
_sl[pts-1]*_dx[pts-1] + 2_sl[pts]*_dx[pts-1]
= 3 * ypart(pts[pts] - pts[pts-1]);
enddef;
vardef mkfcnsplpath (suffix pts) =
pts1..controls (pts1 + (1, _sl1)/3*_dx1) and
for j = 2 upto pts - 1:
(pts[j] - (1, _sl[j])/3*_dx[j-1]) ..pts[j]..
controls (pts[j] + (1,_sl[j])/3*_dx[j]) and
endfor
(pts[pts] - (1,_sl[pts])*_dx[pts-1]/3)..pts[pts]
enddef;
vardef mkfcnspline (expr periodic) (suffix pts) =
init_fcnspl_eqns (pts);
if periodic: periodic_fcnspl_eqns (pts);
else: relaxed_fcnspl_eqns (pts);
fi
mkfcnsplpath (pts)
enddef;
vardef fcnspline (expr periodic) (text the_list) =
setpairs (_fs) (the_list);
if _fs<2:
if _fs=0: NoPoints ("fcnspline", _fs); fi
onepointpath (false, _fs1)
else:
mkfcnspline (periodic) (_fs)
fi
enddef;
vardef mkarc (expr center, begpt, endpt, sweep) =
if (sweep = 0): begpt--endpt
else:
setnumeric (n) ceiling (abs(sweep)/45);
setpair (d) (begpt - center) rotated (signof (sweep) 90);
begpt{d}
for j = 1 upto n-1:
..(begpt rotatedabout (center, j/n*sweep)){d rotated (j/n*sweep)}
endfor ..endpt{d rotated sweep}
fi
enddef;
vardef arc (expr center, begpt, sweep) =
if (center = begpt) or (sweep = 0): begpt--begpt
else:
mkarc (center, begpt, begpt rotatedabout (center, sweep), sweep)
fi
enddef;
def arccps = arc enddef;
vardef arcpps (expr begpt, endpt, sweep) =
if (begpt = endpt) or (sweep = 0): begpt--endpt
else:
setpair (cd) unitvector (endpt-begpt);
if abs(sweep) <= 45:
begpt{cd rotated (-sweep/2)}..endpt{cd rotated (sweep/2)}
elseif abs(sweep) <= 90:
save m; pair m;
m = begpt + whatever*( cd rotated (-sweep/4));
m = 0.5[begpt, endpt] + whatever*(cd rotated 90);
begpt{cd rotated (-sweep/2)}..m{cd}..endpt{cd rotated (sweep/2)}
else:
setnumeric (ang) 90 - ((sweep/2) mod 180);
if abs(ang) = 90:
GBwarn "undefined arc. A line segment will be used instead.";
begpt--endpt
else:
save c; pair c;
c = begpt + whatever*(cd rotated ang);
c = if abs(ang) < 30:
(0.5)[begpt, endpt] + whatever*(cd rotated 90)
else:
endpt + whatever*(-cd rotated -ang)
fi;
mkarc (c, begpt, endpt, sweep)
fi
fi
fi
enddef;
vardef arcpp (expr small, begpt, endpt, rad) =
save full, diam, chord, ang;
full := signof (rad) 360;
diam := 2rad;
chord := abs(endpt-begpt);
if chord < abs(diam):
ang := if not small: full - fi 2*asin (chord/diam);
else: ang := signof (rad) 180;
fi
arcpps (begpt, endpt, ang)
enddef;
def arcppr (expr begpt, endpt, rad, small) =
arcpp (small, begpt, endpt, rad)
enddef;
vardef arcplr (expr center, frtheta, totheta, rad) =
if rad = 0: center--center
else:
mkarc (center, center + rad*dir frtheta,
center + rad*dir totheta, totheta - frtheta)
fi
enddef;
vardef arcalt (expr center, radius, frtheta, totheta) =
arcplr (center, frtheta, totheta, radius)
enddef;
vardef arcppp (expr first, second, third) =
arcpps (first, second, 2*cornerangle (third, first, second)) &
arcpps (second, third, 2*cornerangle (first, second, third))
enddef;
vardef ellipse (expr center, radx, rady, angle) =
fullcircle xscaled (2*radx) yscaled (2*rady) rotated angle
shifted center
enddef;
vardef circle (expr center, rad) =
fullcircle scaled (2*rad) shifted center
enddef;
vardef circlecp (expr center, point) =
mkarc (center, point, point, 360) & cycle
enddef;
vardef circleppp (expr one, two, three) =
arcpps (one, two, 2*cornerangle (three, one, two))
& arcpps (two, three, 2*cornerangle (one, two, three))
& arcpps (three, one, 2*cornerangle (two, three, one))
& cycle
enddef;
vardef circlepps (expr one, two, sweep) =
save ang, full;
full := signof (sweep) 360;
ang := sweep mod full;
arcpps (one, two, ang) & arcpps (two, one, full - ang) & cycle
enddef;
vardef circlepp (expr small, one, two, rad) =
arcpp (small, one, two, rad) & arcpp (not small, two, one, rad)
& cycle
enddef;
def circleppr (expr one, two, rad, small) =
circleppr (one, two, rad, small)
enddef;
vardef quarterellipse(expr A,B,C) =
save T_;
transform T_;
(1,0) transformed T_ = A;
(1,1) transformed T_ = B;
(0,1) transformed T_ = C;
quartercircle scaled 2 transformed T_
enddef;
vardef halfellipse (expr A,B,C) =
save P_; pair P_;
P_ = (C - A)/2;
quarterellipse (A, B - P_, B) & quarterellipse (B, B + P_, C)
enddef;
vardef fullellipse (expr C, A, B) =
save P_; pair P_;
P_ := 2[A,C];
halfellipse (A,B,P_) & halfellipse (P_,2[B,C],A) & cycle
enddef;
vardef pathcenter expr p =
save a, cntr, n; pair cntr, a[];
n := length p;
a1 = pnt 0 (p);
a3 = pnt [n/2] (p);
if cycle p:
a2 = pnt [ n/4] (p);
a4 = pnt [3n/4] (p);
else:
a2 := a3;
a4 := pnt[n] (p);
fi
cntr = .5[a1, a3] + whatever*((a3 - a1) rotated 90);
cntr = .5[a2, a4] + whatever*((a4 - a2) rotated 90);
cntr
enddef;
vardef circumcircle expr t =
circleppp (pnt0 (t), pnt1 (t), pnt2 (t))
enddef;
vardef incircle expr t =
save A, B, C; pair A, B, C;
A := pnt0 (t);
B := pnt1 (t);
C := pnt2 (t);
save a, b, c, D, E, F;
D := abs (B-A) = a + b;
E := abs (C-B) = b + c;
F := abs (A-C) = a + c;
circleppp ((a/D)[A,B], (b/E)[B,C], (c/F)[C,A])
enddef;
vardef excircle expr n of t =
save A, B, C; pair A, B, C;
A := pnt[n] (t);
B := pnt[n + 1] (t);
C := pnt[n + 2] (t);
save a, b, c, D, E, F;
D := abs (B-A) = a - b;
E := abs (C-B) = b + c;
F := abs (C-A) = a - c;
circleppp ((a/D)[A,B], (b/E)[B,C], (c/F)[A,C])
enddef;
vardef ninepointcircle expr t =
circleppp (medianpt 0 of t, medianpt 1 of t, medianpt 2 of t)
enddef;
vardef pshcircle (expr disk, ctr, rad) =
if disk:
if rad >= 1 :
if rad > 1:
GBerrmsg ("Impossible radius of pseudohyperbolic circle.")
"The radius of a pseudohyperbolic circle can be at most 1.";
fi
circle ((0,0),1)
elseif abs(ctr) >= 1 :
if abs(ctr) > 1:
GBerrmsg ("Impossible center of pseudohyperbolic circle.")
"The center of this pseudohyperbolic circle must be in "
& "the unit disk.";
fi
onepointpath (true,ctr)
else:
save _r, _dnm;
_r := abs(ctr);
_dnm := 1 - _r*_r*rad*rad;
circle ((1 - rad*rad)/_dnm*ctr, rad*(1 - _r*_r)/_dnm)
fi
else:
if rad >= 1 :
GBerrmsg ("Impossible radius of pseudohyperbolic circle.")
"The radius of a pseudohyperbolic circle must be less than 1.";
onepointpath (true,ctr)
elseif ypart ctr <= 0:
if ypart ctr < 0:
GBerrmsg ("Impossible center of pseudohyperbolic circle.")
"The center of this pseudohyperbolic circle must be in "
& "the upper half-plane.";
fi
onepointpath (true,ctr)
else:
save _y, _dnm;
_y := ypart ctr;
_dnm := 1 - rad*rad;
circle ((xpart ctr, (1 + rad*rad)/_dnm * _y), 2rad/_dnm*_y)
fi
fi
enddef;
vardef UHPgeodesic (expr A, B) =
if xpart A = xpart B:
A--B
else:
save ang_, C_; pair C_;
if abs(ypart A) < abs(ypart B):
C_ := conj B;
else:
C_ := conj A;
fi
if ypart C_ = 0: % both on x-axis
ang_ := anglefromto(up, B - A);
else:
ang_ := anglefromto(A - C_, B - C_);
fi
arcpps(A, B, 2ang_)
fi
enddef;
vardef UDgeodesic (expr A, B) =
save a_, b_;
a_ := abs(A); b_ = abs(B);
if (a_ = 0) or (b_ = 0):
A--B
elseif angle A = angle B:
A--B
else: % note: A, B and B-A are all nonzero from this point
save ang_;
if a_ = 1:
ang_ := anglefromto (if b_>1: A else: -A fi, B-A)
elseif b_ = 1:
ang_ := anglefromto (A-B, if a_>1: B else: -B fi)
else:
save C_; pair C_;
% reflecting A
if a_ < eps:
C_ := unitvector A;
ang_1 := anglefromto(a_*A - C_, a_*B - C_);
else:
C_ := (1/a_)*unitvector A;
ang_1 := anglefromto(A - C_, B - C_);
fi
% reflecting B
if b_ < eps:
C_ := unitvector B;
ang_2 := anglefromto(b_*A - C_, b_*B - C_);
else:
C_ := (1/b_)*unitvector B;
ang_2 := anglefromto(A - C_, B - C_);
fi
ang_ := if abs(ang_1) < abs(ang_2): ang_1 else: ang_2 fi;
fi
arcpps(A, B, 2ang_)
fi
enddef;
vardef barycenter expr t =
save m; m := length t if not cycle t: + 1 fi;
pnt0(t)/m for k = 1 upto m - 1: + pnt[k](t)/m endfor
enddef;
vardef sector (expr center, rad, frtheta, totheta) =
center -- arcalt (center, rad, frtheta, totheta) -- cycle
enddef;
vardef mkbrace (expr S, C, E) =
save R_, U_, V_, Z_;
pair U_, V_, Z_[];
U_ := unitvector (E-S);
V_ := U_ rotated 90;
R_ := 0.5*(C-S) dotprod V_;
if R_ = 0:
S--C
else:
if R_ < 0 : V_ := -V_; R_ := -R_; fi
V_ := R_*V_; U_ := R_*U_;
Z_1 := S + V_ + U_;
Z_2 := C - V_ - U_;
Z_3 := C - V_ + U_;
Z_4 := E + V_ - U_;
S{V_}..{U_}Z_1--Z_2{U_}..{V_}C{-V_}..{U_}Z_3--Z_4{U_}..{-V_}E
fi
enddef;
vardef mkfcn (expr sm, tens) (expr bmin, bmax, bst) (text pf) =
save _p; pair _p[]; _p := 0;
save _dx, _n, _r; numeric _dx, _n, _r;
if bmax = bmin: _n := 1;
else:
_r := bmax - bmin;
_dx := max (abs(bst), nottoosmall*abs(_r), epsilon);
_n := emax (round(abs(_r)/_dx), 1);
fi
for _i = 0 upto _n: _p[incr _p] := pf(bmin + _i/_n*_r); endfor
mkpath (sm, tens, false, _p)
enddef;
def tfcn (expr sm) = mkfcn (sm, default_tension) enddef;
def parafcn (expr sm) = tparafcn (sm, default_tension) enddef;
vardef tparafcn (expr sm, tn) (expr bmin, bmax, bst) (text pf) =
save _fp; vardef _fp (expr t) = pf enddef;
mkfcn (sm, tn) (bmin, bmax, bst) (_fp)
enddef;
vardef xfcn (expr sm) (expr xmin, xmax, st) (text _fx) =
save _fp; vardef _fp (expr _x) = (_x, _fx(_x)) enddef;
mkfcn (sm, default_tension) (xmin, xmax, st) (_fp)
enddef;
def function (expr sm) = tfunction (sm, default_tension) enddef;
vardef tfunction (expr sm, tens, xmin, xmax, st) (text _fx) =
save _fp; vardef _fp (expr x) = (x, _fx) enddef;
mkfcn (sm, tens) (xmin, xmax, st) (_fp)
enddef;
def btwnfcn (expr sm) = tbtwnfcn (sm, default_tension) enddef;
vardef tbtwnfcn (expr sm, tn, xlo, xhi, st)(text _fx)(text _gx) =
tfunction (sm, tn) (xlo, xhi, st) (_fx) --
( reverse tfunction (sm, tn) (xlo, xhi, st) (_gx) ) -- cycle
enddef;
def belowfcn (expr sm) = tbelowfcn (sm, default_tension) enddef;
vardef tbelowfcn (expr sm, tn, xlo, xhi, st)(text _fx) =
(xlo,0)--(xhi,0)--
(reverse tfunction (sm, tn, xlo, xhi, st)(_fx))--cycle
enddef;
vardef rfcn (expr sm, tmin, tmax, st) (text ft) =
save _fq; vardef _fq (expr t) = (ft(t)) * (dir t) enddef;
mkfcn (sm, default_tension) (tmin, tmax, st) (_fq)
enddef;
def plrfcn (expr sm) = tplrfcn (sm, default_tension) enddef;
vardef tplrfcn (expr sm, tens, tmin, tmax, st) (text ft) =
save _fq; vardef _fq (expr t) = (ft) * (dir t) enddef;
mkfcn (sm, tens) (tmin, tmax, st) (_fq)
enddef;
def btwnplrfcn (expr sm) = tbtwnplrfcn (sm, default_tension) enddef;
vardef tbtwnplrfcn (expr sm, tn, tlo, thi, st)(text _ft)(text _gt)=
tplrfcn (sm, tn, tlo, thi, st) (_ft) --
( reverse tplrfcn (sm, tn, tlo, thi, st) (_gt) ) -- cycle
enddef;
def plrregion (expr sm) = tplrregion (sm, default_tension) enddef;
vardef tplrregion (expr sm, tn, tlo, thi, st) (text _ft) =
(0,0)--tplrfcn (sm, tn, tlo, thi, st ) (_ft)--cycle
enddef;
numeric tolerancefactor;
tolerancefactor := .02;
vardef mklevelset (expr sm, tens, X, Y, t, a, b, c, d) =
save _inside_;
vardef _inside_ (expr U, V) =
inside_levelset(U, V) and between(a, b)(U) and between(c, d)(V)
enddef;
if not _inside_ (X, Y):
GBwarn "Invalid seed point for levelset.";
pairmax((a,c), pairmin((X,Y), (b,d)))&cycle
else:
save ls, W, A, B, prev, curr, seed;
pair ls[], prev, curr, seed;
seed := (X,Y);
ls := 0; W := 0;
save _first_, _next_, get_next;
vardef _first_ (expr U) = _inside_ (U, Y) enddef;
vardef _next_ (expr ang) =
_inside_ (X_curr + t * cosd ang, Y_curr + t * sind ang)
enddef;
def get_next (expr angA, angB) =
X_curr := xpart curr; Y_curr := ypart curr;
ls[incr ls] := curr + t * dir (solve _next_ (angA, angB));
prev := curr; curr := ls[ls];
W := W + anglefromto (prev - seed, curr - seed);
enddef;
interim tolerance := t*tolerancefactor;
ls[incr ls] := (solve _first_ (X, b), Y);
curr := ls[ls];
interim tolerance := radian*tolerancefactor;
get_next (180, 0);
for n = 3 upto max_points:
A := angle (curr - prev);
get_next (A + 120, A - 120);
exitif ((abs(W) > 180) or (ls > 10))
and (abs(ls[ls] - ls1) < 1.2t);
endfor
mkpath (sm, tens, true) (ls)
fi
enddef;
numeric max_points;
max_points := 2000;
def levelset (expr s) = tlevelset (s, default_tension) enddef;
vardef tlevelset (expr smth, tens, seed, seg) (text cond) =
save inside_levelset, _t;
vardef inside_levelset (expr x, y) = cond enddef;
_t := if seg <= 0: emax (xpos-xneg, ypos-yneg)/max_points * 20
else: seg fi;
mklevelset (smth, tens, xpart seed, ypart seed, _t)
(xneg, xpos, yneg, ypos)
enddef;
def RKIV (expr sm) = tRKIV (sm, default_tension)
enddef;
vardef tRKIV (expr sm, tens, zstart, ds, N) (text _RHS_) =
save _trj, _ztr, _dz, _ztmp, _ctm;
pair _trj[], % The trajectory
_ztr, % current point
_dz[], % array[4] of displacements
_ztmp; % current point for calculating velocity
_trj := N+1; % ultimate size of _trj array
_trj1 := _ztr := zstart;
save _tt, % current time
_dt, % current time step
_th; % current time plus half a step
_tt := 0;
for _idx := 2 upto _trj:
_dt := ds/emax(1,abs(_RHS_(_tt,_ztr)));
_th := _tt + .5_dt;
_dz1 := _dt*_RHS_(_tt, _ztr); % displacement for current point
_ztmp := _ztr + .5_dz1; % 1st midpoint
% use _th instead of twice calculating (_tt + .5_dt)
_dz2 := _dt*_RHS_(_th, _ztmp); % displacement for 1st midpoint
_ztmp := _ztr + .5_dz2; % 2nd midpoint
_dz3 := _dt*_RHS_(_th, _ztmp); % displacement for 2nd midpoint
_ztmp := _ztr + _dz3; % temporary end point
% get time for next loop now since we need it in the next line:
_tt := _tt + _dt;
_dz4 := _dt*_RHS_(_tt, _ztmp); % displacement for end point
% get next point
_ztr := _ztr + (_dz1 + 2_dz2 + 2_dz3 + _dz4)/6;
_trj[_idx] := _ztr;
endfor
mkpath (sm, tens, false, _trj)
enddef;
def xyRKIV (expr sm) = txyRKIV (sm, default_tension)
enddef;
vardef txyRKIV (expr sm, tens, zstart, ds, N) (text _RHS_) =
save _fgxy, __fgxy;
vardef __fgxy (expr t, x, y) = _RHS_ enddef;
vardef _fgxy (expr t, Z) = __fgxy(t, xpart Z, ypart Z) enddef;
tRKIV (sm, tens, zstart, ds, N) (_fgxy)
enddef;
def odeRKIV (expr sm) = todeRKIV (sm, default_tension)
enddef;
vardef todeRKIV (expr sm, tens, xstart, ystart, ds, N)
(text _fxy) =
txyRKIV (sm, tens, (xstart, ystart), ds, N) ((1, _fxy))
enddef;
vardef lclosed expr f =
f
if not cycle f:
if pnt0(f) = pnt[infinity](f): & else: -- fi cycle
fi
enddef;
def sclosed = sclosedt (default_tension) enddef;
vardef sclosedt (expr t) expr f =
if cycle f: f
else: save n; n := length f;
if n = 0: f&cycle
elseif n = 1: pnt0(f)..tension t..pnt1(f)..tension t..cycle
else:
(pnt0 (f)) { (pnt1(f)) - (pnt[n] (f)) }..tension t
..(subpath (1, n-1) of f)..tension t
..(pnt[n](f)) { pnt0(f) - pnt[n-1](f) }
..tension t..cycle
fi
fi
enddef;
def bclosed = bclosedt (default_tension) enddef;
vardef bclosedt (expr t) expr f =
f
if not cycle f:
if pnt0(f) = pnt[infinity](f): & else: ..tension t.. fi cycle
fi
enddef;
def uclosed = bclosed enddef;
def uclosedt = bclosedt enddef;
def cbcontrols (suffix b, t) =
b1 := 2[t3, t2];
b2 := 2[t2, t1];
b3 := 2[b1, b2];
b4 := 2[b2, b3];
enddef;
vardef cbclosed expr f =
save n; n := length f;
if cycle f: f
elseif n = 0: f&cycle
else:
save p, q, t; pair p[], q[], t[];
t1 := pnt0(f); t2 := post0(f); t3 := pre1(f);
cbcontrols (p, t); % defines p1 to p4
t1 := pnt[n](f); t2 := pre[n](f); t3 := post[n-1](f);
cbcontrols (q, t); % defines q1 to q4
f..controls q2 and q3..opencbs (q1,q4,p4,p1)
..controls p3 and p2..cycle
fi
enddef;
vardef qbclosed expr f =
if cycle f: f
else: save n; n := length f;
if n = 0: f&cycle
else:
save p; pair p[]; p := 4;
p1 := (3/2)[pnt[n](f), pre[n](f)];
p2 := 2[p1, pnt[n](f)];
p4 := (3/2)[pnt 0 (f), post0 (f)];
p3 := 2[p4, pnt 0 (f)];
f & mkqbs (p) & cycle
fi
fi
enddef;
vardef makesector expr p = (pathcenter p)--p--cycle enddef;
vardef arccomplement expr p =
if cycle p: onepointpath (false, pnt0(p))
else:
setnumeric (nn) length p;
setpairs (pp) (pnt0(p), pnt[.5nn](p), pnt[nn](p));
arcpps (pp3,pp1,2*cornerangle(pp2,pp3,pp1))
fi
enddef;
vardef cutoffbefore (expr b) expr f =
save t, n; n := length f;
if n > 0:
for k = 1 upto n:
exitif (subpath (0,k) of f) intersects b;
endfor
if _Xtime < 0:
cuttings := pnt0 (f){0,0};
f
else:
cuttings := subpath (0,_Xtime) of f;
subpath (_Xtime, n) of f
fi
else: f
fi
enddef;
vardef cutoffafter (expr b) expr f =
setpath (g) cutoffbefore (b) reverse f;
cuttings := reverse cuttings;
reverse g
enddef;
vardef trimmedpath (expr btrim, etrim) expr f =
save g, h; path g, h;
g := invvconv (fullcircle scaled 2btrim) shifted pnt0(f);
h := invvconv (fullcircle scaled 2etrim) shifted pnt[length f] (f);
cutoffafter (h) cutoffbefore (g) f
enddef;
vardef predirection@# (expr p) =
- postdirection[length p - @#] (reverse p)
enddef;
vardef postdirection@# (expr p) =
save _n; _n := length (p);
setpair (v) __dir (subpath (@#, @# + _n) of p);
if v = origin:
v := - __dir (subpath (@#, @# - _n) of p);
fi
v
enddef;
vardef __dir (expr p) =
save v, w; pair v, w; w := pnt0 (p);
v := origin;
for n = 1 upto length (p):
v := post[n-1] (p) - w; exitif v <> origin;
v := pre [ n ] (p) - w; exitif v <> origin;
v := pnt [ n ] (p) - w; exitif v <> origin;
endfor
sgn v
enddef;
vardef trivial expr p = (__dir (p) = origin) enddef;
newinternal hdwdr, hdten;
boolean hfilled;
def headshape (expr wr, tens, fil) =
interim hdwdr := wr;
interim hdten := if tens>0: tens else: default_tension fi;
if hdten < .75: hdten := .75; fi
setboolean (hfilled) fil;
mkheadpaths;
enddef;
def mkheadpaths =
save Arrowhead, Leftharpoon, Rightharpoon;
path Arrowhead, Leftharpoon, Rightharpoon,
Arrowhead.clear, Leftharpoon.clear, Rightharpoon.clear;
Rightharpoon := (0,0){down}..tension hdten..(.5hdwdr,-1);
Rightharpoon.clear := Rightharpoon--(.5hdwdr,0)--cycle;
Leftharpoon := (reverse Rightharpoon) xscaled -1;
Leftharpoon.clear := (reverse Rightharpoon.clear) xscaled -1;
Arrowhead := Leftharpoon & Rightharpoon;
Arrowhead.clear := Leftharpoon.clear & Rightharpoon.clear & cycle;
if hfilled:
Arrowhead := Arrowhead--cycle;
Rightharpoon := Rightharpoon--(0,-1)--cycle;
Leftharpoon := Leftharpoon--(0,-1)--cycle;
fi
enddef;
headshape (1,1,false);
def head = ahead enddef;
vardef ahead (expr clr, front, back, hwr, tens, filled) =
settension (_tn) tens; fixtension (_tn);
if front <> back:
setpair (side) (hwr/2) * ((front-back) rotated 90);
setpath (f) (back + side)..tension _tn..
{front-back}front{back-front}..tension _tn..(back - side);
if clearhead:
safeunfill (back - side)--(front-side)--(front+side)--
(back+side) & f & cycle;
colorsafedraw (background) (back - side)--(front-side)--
(front+side)--(back+side) & f & cycle;
fi
if filled:
f := f--cycle;
colorsafefill (clr) f;
fi
colorsafedraw (clr) f;
fi
enddef;
def headpath = Gheadpath (false) (Arrowhead) enddef;
def headpathx = Gheadpath (true) (Arrowhead) enddef;
def colorheadpath = colorGheadpath (false) (Arrowhead) enddef;
def colorheadpathx = colorGheadpath (true) (Arrowhead) enddef;
def Gheadpath (expr trim) (suffix ah) =
colorGheadpath (trim) (ah) (headcolor)
enddef;
vardef colorGheadpath
(expr trim) (suffix ah) (expr clr, sc, rot, pos) expr f =
if (sc <> 0) and (known ah) and (path ah):
convertpath (_g) f;
setpair (_P) predirection[length _g] (_g);
if _P <> origin:
_P := _P rotated rot;
setnumeric (_ang) anglefromto (up, _P);
_P := pnt[length _g] (_g) - pos * _P;
setpair (_tip) if known ah.tip: ah.tip else: origin fi;
if trim:
if known ah.clear:
safeunfill (ah.clear shifted - _tip)
scaled sc
rotated _ang
shifted _P;
fi
setnumeric (_ys) max(bp, penwd, last_dot_size);
safeunfill cut_path
xscaled sc yscaled _ys
rotated _ang shifted _P;
fi
if cycle ah: colorsafefill else: colorsafedraw fi (clr)
(ah shifted -_tip)
scaled sc
rotated _ang
shifted _P;
fi
fi
f
enddef;
path cut_path;
cut_path := (.5,0)--(.5,.71)--(-.5,.71)--(-.5,0)--cycle;
def tailpath (suffix sh) = colortailpath (sh) (headcolor) enddef;
vardef colortailpath (suffix sh) (expr clr, sc, rot, pos) expr f =
if (sc <> 0) and (known sh) and (path sh):
convertpath (_g) f;
setpair(_P) postdirection0 (_g);
if _P <> origin:
_P := _P rotated rot;
if cycle sh: colorsafefill else: colorsafedraw fi (clr)
(sh if known sh.tip: shifted -sh.tip fi)
scaled sc
rotated anglefromto (up, _P)
shifted (pnt0 (_g) + pos * _P);
fi
fi
f
enddef;
def midpath (suffix sh) = colormidpath (sh) (headcolor) enddef;
vardef colormidpath (suffix sh) (expr clr, sc, rot, pos) expr f =
if (sc <> 0) and (known sh) and (path sh):
convertpath (_g) f;
setnumeric (_t) pathtime[pos] (_g);
setpair (_P) postdirection[_t] (_g);
if _P <> origin:
_P := _P rotated rot;
if cycle sh: colorsafefill else: colorsafedraw fi (clr)
sh scaled sc
rotated anglefromto (up, _P)
shifted (pnt[_t] (_g));
fi
fi
f
enddef;
vardef signeddeviate primary X =
(uniformdeviate 1)[-X,X]
enddef;
vardef scaledeviate (expr W, A) =
2 ** (signeddeviate W) * dir A
enddef;
vardef polardeviate primary R =
(uniformdeviate abs(R)) * dir uniformdeviate 360
enddef;
vardef xydeviate primary Z =
(signeddeviate (xpart Z), signeddeviate (ypart Z))
enddef;
vardef randompair (expr maxshift) =
if numeric maxshift: polardeviate (maxshift)
elseif pair maxshift: xydeviate (maxshift)
else: (0,0)
fi
enddef;
vardef detrivialized expr f =
save g; path p, g[]; g := 0;
for k = 1 upto length f:
p := subpath (k-1,k) of f;
if not trivial p: g[incr g] := p; fi
endfor
if g = 0: onepointpath (cycle f, pnt0(f))
else: g1 for k = 2 upto g: &g[k] endfor if cycle f: &cycle fi
fi
enddef;
vardef randompath (expr maxshift, weirdness) expr f =
save g, n; path g;
g := detrivialized f;
n := length g;
if n = 0:
f shifted randompair (maxshift)
else:
save X, U, V;
pair X[], U[], V[];
if cycle g: n := n - 1; fi
for k = 0 upto n:
X[k] := pnt[k](g);
U[k] := X[k] - pre[k](g);
V[k] := post[k](g) - X[k];
endfor
save A, B;
for k := 0 upto n:
X[k] := X[k] shifted randompair (maxshift);
A := anglefromto (U[k],V[k]);
B := signeddeviate (30weirdness);
U[k] := X[k] - (U[k] zscaled scaledeviate (weirdness,B));
B := B - A + A * (2 ** signeddeviate weirdness);
V[k] := X[k] + (V[k] zscaled scaledeviate (weirdness,B));
endfor
X0 for k = 1 upto n:
.. controls V[k-1] and U[k] .. X[k]
endfor
if cycle g:
.. controls V[n] and U0 .. cycle
fi
fi
enddef;
vardef randomlines (expr maxshift) expr f =
save g, n; path g;
g := detrivialized f;
n := length g;
if n = 0:
f shifted randompair (maxshift)
else:
if cycle g: n := n - 1; fi
(pnt0(g) shifted randompair (maxshift))
for k = 1 upto n:
-- (pnt[k](g) shifted randompair (maxshift))
endfor
if cycle g:
-- cycle
fi
fi
enddef;
vardef interpolatedpath (expr t, P) expr Q =
if not path Q:
GBerrmsg ("Improper argument to interpolatedpath.")
"The last argument to interpolatedpath must be a path.";
if pair P: onepointpath(false, P)
else:
if path P:
P
else:
onepointpath (false, origin)
fi
fi
elseif pair P:
interpolated_pair_path (t, cycle Q, P, Q)
elseif not path P:
GBerrmsg ("Improper argument to interpolatedpath.")
"The second argument to interpolatedpath must be a pair "
& "or a path.";
Q
else:
if t=0: Q
elseif t=1: P
else:
save P_, Q_; path P_, Q_;
P_ := detrivialized P;
Q_ := detrivialized Q;
if length P_ = 0:
interpolated_pair_path (t, cycle Q, pnt0(P_), Q)
elseif length Q_ = 0:
interpolated_pair_path (t, cycle Q, pnt0(Q_), P)
else:
save G, H, n, m, k, r;
path G[], H[];
G := H := 0;
n := length P_; m := length Q_;
k := gcd(n, m);
r := m/k;
for I=0 upto n-1:
for J=0 upto r-1:
G[incr G] := subpath (I+J/r, I+(J+1)/r) of P_;
endfor
endfor
r := n/k;
for I=0 upto m-1:
for J=0 upto r-1:
H[incr H] := subpath (I+J/r, I+(J+1)/r) of Q_;
endfor
endfor
for N = 1 upto G-1:
force_equal_ends(G[N], G[N+1]);
force_equal_ends(H[N], H[N+1]);
endfor
interpolated_segment (t, G1, H1)
for N = 2 upto G: & interpolated_segment (t, G[N], H[N])
endfor if (pnt0(G1)=pnt1(G[G])) and (cycle Q): & cycle fi
fi
fi
fi
enddef;
vardef interpolated_pair_path (expr t, cyclic, P, Q) =
save N; N := length Q;
if N=0: onepointpath (cyclic, (t)[pnt0(Q),P])
else:
(t)[pnt0(Q),P]..controls (t)[post0(Q),P] and
for n=1 upto N - 1:
(t)[pre[n](Q),P]..(t)[pnt[n](Q),P]..controls (t)[post[n](Q),P]
and
endfor
(t)[pre[N](Q),P].. if cyclic: cycle else: (t)[pnt[N](Q),P] fi
fi
enddef;
vardef interpolated_segment (expr t, S, T) =
(t)[ pnt0(S), pnt0(T)]..controls
(t)[ post0(S), post0(T)] and (t)[ pre1(S), pre1(T)]..
(t)[ pnt1(S), pnt1(T)]
enddef;
vardef parasegment (expr d, segs, f) =
if d = 0: f
else:
save u, v, t; pair u[], v[];
for n = 0 upto segs:
t := n/segs;
u[n] := postdirection [t] (f);
v[n] := pnt[t] (f) + (u[n] zscaled (0,d));
endfor
v0{u0}
for n = 1 upto segs: ...v[n]{u[n]} endfor
fi
enddef;
vardef parapath (expr d) expr f =
if d = 0:
f
else:
save a, g, h, p, q, s, t, u, v, w;
path g[], h, p[], q[];
numeric a, s, t;
pair u, v, w, w[];
s := emax(3, emin(segment_split, ceiling(max_points/5/length f)));
p := 0;
for i = 1 upto length f:
h := subpath (i-1, i) of f;
if not trivial h:
q[incr p] := h;
p[p] := parasegment (d, s, h);
fi
endfor
if p = 0:
f
else:
a := if d>0: - fi 180;
h := p1;
for i = 1 upto p-1:
u := predirection 1 (q[i]);
v := postdirection 0 (q[i+1]);
w1 := pnt 1 (q[i]) - (u zscaled (0,d));
w2 := pnt 0 (q[i+1]) - (v zscaled (0,d));
w3 := pnt [infinity] (h);
w4 := pnt 0 (p[i+1]);
g0 := arcpps(w3, w1, a);
g1 := h & g0;
g2 := arcpps(w2, w4, a) & p[i+1];
if (p[i] & g0) intersects reverse g2:
s := length g2 - _Ytime;
t := length h - length p[i] + _Xtime;
g1 := subpath (0, t) of g1;
g2 := subpath (s, length g2) of g2;
force_equal_ends (g1, g2);
h := g1 & g2;
else:
h := h .. p[i+1];
fi
endfor
if cycle f:
u := predirection 1 (q[p]);
v := postdirection 0 (q[1]);
w1 := pnt 1 (q[p]) - (u zscaled (0,d));
w2 := pnt 0 (q[1]) - (v zscaled (0,d));
w3 := pnt [infinity] (h);
w4 := pnt 0 (p[1]);
g3 := arcpps(w3, w1, a);
g0 := arcpps(w2, w4, a);
g1 := g0 & h & g3;
g2 := g0 & p[1];
if (p[p] & g3) intersects reverse g2:
s := length g2 - _Ytime;
t := length g0 + length h - length p[p] + _Xtime;
g1 := subpath (s, t) of g1;
force_equal_ends (g1, g1);
h := g1 & cycle;
else:
h := h..cycle;
fi
fi
h
fi
fi
enddef;
vardef turnangle@# (expr f) =
anglefromto(predirection@# (f), postdirection@#(f))
enddef;
def setdatadashes (text lst) =
save __type; __type := 0;
forsuffixes _itm = lst:
if knownnumericarray _itm :
copyarray (_itm) (__type[__type]);
next __type;
else: GBwarn "Improper dash pattern in setdatadashes.";
fi
endfor
if __type > 1:
save dashtype; dashtype := __type;
for _j = 0 upto dashtype - 1:
copyarray (__type[_j]) (dashtype[_j]);
endfor
else:
SetdataWarn "dashes";
fi
enddef;
def getdashpat expr n = dashtype[n mod dashtype] enddef;
def SetdataWarn expr s =
GBwarn "command setdata"& s &"() failed. Previous values retained.";
enddef;
numeric Solid, Simpledash, Simpledot, Dotdash, Dotdashdot, Dotdashdash;
dashpat (Solid) (0);
dashpat (Simple_dash) (3bp, 4bp);
dashpat (Simple_dot) (0, 4bp);
dashpat (Dot_dash) (0, 4bp, 3bp, 4bp);
dashpat (Dot_dash_dot) (0, 4bp, 3bp, 4bp, 0, 4bp);
dashpat (Dot_dash_dash) (0, 4bp, 3bp, 4bp, 3bp, 4bp);
numeric dashtype, dashtype[], dashtype[][];
def defaultdashes =
setdatadashes (Solid, Simple_dash, Simple_dot,
Dot_dash, Dot_dash_dot, Dot_dash_dash);
enddef;
defaultdashes;
def setdatasymbols (text lst) =
save __type; path __type[];
__type := 0;
for _itm = lst:
if (known _itm) and (path _itm):
__type[__type] := _itm;
next __type;
else:
GBwarn "Improper symbol in setdatasymbols().";
fi
endfor
if __type > 1:
save pointtype; pointtype := __type;
path pointtype[];
for _j = 0 upto pointtype - 1:
pointtype[_j] := __type[_j];
endfor
else:
SetdataWarn "symbols";
fi
enddef;
def getsymbol expr n := pointtype[n mod pointtype] enddef;
def DeclareGBSymbols (text S) =
forsuffixes _itm = S:
path _itm;
path _itm.clear;
pair _itm.tip;
endfor
enddef;
DeclareGBSymbols(
Triangle, Square, Circle, Diamond, Star, Plus, Cross,
Asterisk, Crossbar, Leftbar, Rightbar, Righthook,
Lefthook, SolidTriangle, SolidSquare, SolidCircle,
SolidDiamond, SolidStar
);
vardef undo_cycle expr f = subpath (0, length f) of f enddef;
SolidTriangle := (up--(dir 210)--(dir -30)--cycle) scaled .78;
Triangle := undo_cycle SolidTriangle;
Triangle.clear := SolidTriangle.clear :=
((dir -30)--(cosd 30,1)--(cosd 210,1)--(dir 210)--up--cycle)
scaled .78;
SolidSquare := (up--(-1,1)--(-1,-1)--(1,-1)--(1,1)--cycle) scaled .443;
Square := undo_cycle SolidSquare;
SolidCircle := fullcircle rotated 90;
Circle := undo_cycle SolidCircle;
Circle.clear := SolidCircle.clear :=
halfcircle--(-.5,.5)--(.5,.5)--cycle;
SolidDiamond := (up--left--down--right--cycle)
scaled .522 yscaled 1.44;
Diamond := undo_cycle SolidDiamond;
Diamond.clear := SolidDiamond.clear :=
(right--(1,1)--(-1,1)--left--up--cycle) scaled .522 yscaled 1.44;
Plus := ((0,0)--up--down--(0,0)--left--right) scaled .65;
Plus.clear := (right--(1,1)--(-1,1)--(left)--cycle) scaled .65;
Cross := ((0,0)--(dir 45)--(dir -135)--(0,0)--(dir -45)--(dir 135))
scaled .65;
Cross.clear :=
((0,0)--(dir -45)--dir(45)--(dir 135)--(dir -135)--cycle) scaled .65;
Asterisk := ((0,0)--up--down--(0,0)--(dir 30)--(dir -150)
--(0,0)--(dir -30)--(dir 150)) scaled .6;
Asterisk.clear := ((0,0)--(dir -30)--(cosd 30,1)--(cosd 150,1)
--(dir -150)--cycle) scaled .6;
Crossbar := ((0,0)--left--right) scaled .65;
Crossbar.clear := rect (right,(-1,.5)) scaled .65;
Leftbar := ((0,0)--left);
Rightbar := ((0,0)--right);
Leftbar.clear := rect((0,0),(-1,.5));
Rightbar.clear := rect((0,0),(1,.5));
Righthook := arcpps((0,0),(1,0),180);
Lefthook := Righthook xscaled -1;
Righthook.clear := Righthook--cycle;
Lefthook.clear := Lefthook--cycle;
vardef mkstar (expr n, m) (suffix A) =
save ang; ang := 360/n;
A1 := up; A3 := up rotated ang;
A2 = (whatever)[A1, A1 rotated ( ang*m)];
A2 = (whatever)[A3, A3 rotated (-ang*m)];
for i = 4 upto 2n:
A[i] := A[i-2] rotated ang;
endfor
A := 2n;
mkpoly (true, A)
enddef;
save _A; pair _A[];
SolidStar := mkstar (5, 2, _A) scaled .84;
Star := undo_cycle SolidStar;
Star.clear := polyline (true)
(_A9, _A10, _A1, _A2, _A3, (xpart _A3, 1), (xpart _A9, 1))
scaled .84;
SolidStar.clear := Star.clear;
forsuffixes S =
Triangle, Square, Circle, Diamond, Star, Plus, Cross,
Asterisk, Crossbar, Leftbar, Rightbar, Righthook,
Lefthook, SolidTriangle, SolidSquare, SolidCircle,
SolidDiamond, SolidStar :
S.tip := point 0 of S;
endfor
vardef gcd (expr n, m) =
save a, b, r;
a := emax (abs(m), abs(n));
b := emin (abs(m), abs(n));
if b > 0:
forever:
r := a mod b;
exitif r < 1;
a := b; b := r;
endfor
b
else:
a
fi
enddef;
vardef lcm (expr n, m) =
n/gcd(n, m)*m
enddef;
numeric pointtype; path pointtype[];
def defaultsymbols =
setdatasymbols( Circle, Cross, SolidDiamond, Square, Plus,
Triangle, SolidCircle, Star, SolidTriangle);
enddef;
defaultsymbols;
def setdatacolors (text lst) =
setnumeric (__type) 0;
% First, just count and store the known colors in the list
for _itm = lst:
if knowncolor _itm :
if __type = 0 : def _datacolors = _itm enddef;
else:
expandafter def
expandafter _datacolors
expandafter = _datacolors, _itm enddef;
fi
next __type;
else: GBwarn "Improper color in setdatacolors().";
fi
endfor
if __type > 1:
save colortype, _tmpstr;
colortype := 0;
% colortype[] is an array of strings:
string colortype[], _tmpstr;
for _itm = _datacolors: %
% Each string is the name of some color variable
_tmpstr := "colortype_" & GBromannumeral(colortype);
setcolor (scantokens(_tmpstr)) _itm;
colortype[colortype] := _tmpstr;
next colortype;
endfor
else:
SetdataWarn "colors";
fi
enddef;
def getcolor expr n = (scantokens (colortype[n mod colortype])) enddef;
numeric colortype; string colortype[];
setcolor (dRed) (1, 0, 0);
setcolor (dBlue) (.2,.2,1);
setcolor (dOrange) (1,.34,0);
setcolor (dGreen) (0,.80,0);
setcolor (dBlack) cmykblack;
if has_cmyk :
setcolor (dCyan) cyan;
setcolor (dMagenta) magenta;
setcolor (dYellow) yellow;
else: % rgb colors seem to be lighter than the cmyk equivalents.
setcolor (dCyan) cmyk(.85,0,0,.15);
setcolor (dMagenta) cmyk(0,.85,0,.15);
setcolor (dYellow) cmyk(0,0,.85,.15);
fi
def defaultcolors =
setdatacolors(dBlack, dRed, dBlue, dOrange,
dGreen, dMagenta, dCyan, dYellow);
enddef;
defaultcolors;
def computepie (suffix dat) (expr sign, ang, cent, rad) (text data) =
begingroup
save _tot, _max, _toobig;
_max := 0; dat := 0;
for _val = data:
dat[incr dat] := _val;
_max := emax (_max, _val);
endfor
if dat=0: GBwarn "piechart attempted with empty list.";
_toobig := 1;
else:
_toobig := infinity/dat;
fi
if _max > _toobig:
for _idx = 1 upto dat:
dat[_idx] := dat[_idx]/_toobig;
endfor
fi
for _idx = 2 upto dat:
dat[_idx] := dat[_idx - 1] + dat[_idx];
endfor
_tot := dat[dat];
for _idx = dat downto 2:
dat[_idx] := ang + sign*dat[_idx-1]/_tot*360;
endfor
dat1 := ang; dat[dat + 1] := ang + 360sign;
endgroup
enddef;
def piechart (expr sign, ang, cent, rad) (text data) =
save _dat;
computepie (_dat) (sign, ang, cent, rad) (data);
mkpiewedges (_dat, cent, rad);
enddef;
def mkpiewedges (suffix dat) (expr cent, rad) =
numeric piewedge, piedirection, pieangle, pieangle[];
pair piecenter, piedirection[];
path piewedge[];
piecenter := cent;
piedirection := pieangle := piewedge := dat;
for _idx = 1 upto dat:
pieangle[_idx] := dat[_idx];
piewedge[_idx] := sector (piecenter, rad, dat[_idx], dat[_idx+1]);
piedirection[_idx] := dir(0.5[ dat[_idx], dat[_idx+1] ]);
endfor
enddef;
def namedpiechart (suffix nm) (expr sign, ang, cent, rad) (text data) =
save _dat;
computepie (_dat) (sign, ang, cent, rad) (data);
setnumeric (nm) _dat;
pair nm.center, nm.direction[];
path nm.wedge[];
nm.center := cent;
for _idx = 1 upto _dat:
nm.wedge[_idx] := sector (cent, rad, _dat[_idx], _dat[_idx+1]);
nm.direction[_idx] := dir(0.5[ _dat[_idx], _dat[_idx+1] ]);
endfor
enddef;
def barchart (expr firstbar, sep, r, vert)(text data) =
numeric barbegin, barbegin[],
barend, barend[],
barlength, barlength[],
barstart, barstart[],
chartbar, barwd;
path chartbar[];
chartbar := 0; barwd := r*sep;
for _itm = data:
barend[incr chartbar]
:= if pair _itm: ypart _itm else: _itm fi;
barbegin[chartbar] := if pair _itm: xpart _itm else: 0 fi;
endfor
barbegin := barend := barlength := barstart := chartbar;
for _nn = 1 upto chartbar:
barstart[_nn] := firstbar + sep*(_nn-1);
barlength[_nn] := barend[_nn];
chartbar[_nn] := rect ((barbegin[_nn], 0), ( barend[_nn], barwd))
shifted (0, barstart[_nn]) if vert: xyswap fi;
endfor
enddef;
def namedbarchart (suffix nm) (expr first, sep, r, vert) (text data) =
save nm;
begingroup
save _bb, _ee, _ww;
path nm.bar[];
nm := 0; _ww := r*sep;
for _itm = data:
_ee := if pair _itm: ypart _itm else: _itm fi;
_bb := if pair _itm: xpart _itm else: 0 fi;
nm.bar[incr nm] := rect ((_bb, 0), ( _ee, _ww) )
shifted (0, first + sep*(nm-1)) if vert: xyswap fi;
endfor
endgroup
enddef;
picture totalpicture;
boolean totalnull, currentnull;
def clearit =
currentpicture := totalpicture := nullpicture;
currentnull := totalnull := true;
enddef;
def keepit =
addto totalpicture also currentpicture;
currentpicture := nullpicture;
totalnull := totalnull or currentnull;
currentnull := true;
enddef;
def addto_currentpicture =
currentnull := false;
addto currentpicture
enddef;
def mergeit (text do) =
if totalnull:
do currentpicture
elseif currentnull:
do totalpicture
else:
begingroup
save _v_; picture _v_;
_v_ := currentpicture;
addto _v_ also totalpicture;
do _v_
endgroup
fi
enddef;
boolean noship; noship := false;
def shipit = if noship: else: mergeit (shipout) fi enddef;
numeric gcode; gcode := 0;
input dvipsnam.mp;
% end grafbase.mp
endinput.
%%
%% End of file `grafbase.mp'.
|