-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplain_ex.mp
2104 lines (2097 loc) · 89.6 KB
/
plain_ex.mp
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
%%%% THIS FILE IS DISTRIBUTED WITH THE METATYPE1 PACKAGE
%%%%
%%%% It is an extension of plain.mp
%% \raggedbottom
%% \edef\illusname{plain\string_e\string_}
%% \--------------------------------------------------------------------
%% \centering \BF EXTENSION OF \MP{} plain FORMAT (ver. 0.47)
%% \-
%% \centering \BF ROZSZERZENIE \MP{}-owego FORMATU plain (wer. 0.47)
%% \--------------------------------------------------------------------
if known plain_ex_ver: expandafter endinput else: plain_ex_ver:=.47; fi
def killtext text t = enddef; % absent from older versions of plain.mf
%% \--------------------------------------------------------------------
%% Knuthian tradition:
%% \-
%% Knuthowa tradycja:
%% \--------------------------------------------------------------------
mm#=2.84528; pt#=1; dd#=1.07001; bp#=1.00375; cm#=28.45276; pc#=12;
cc#=12.84010; in#=72.27;
%% \--------------------------------------------------------------------
%% A patch for bugs in \MP{} |turningnumber| functionality
%% \-
%% Makra /lataj/ace wadliwe funkcjonowanie operacji |turningnumber|
%% \--------------------------------------------------------------------
let original_turningnumber = turningnumber;
vardef straighten_path(expr r) =
for k=0 upto length r - 1: point k of r -- endfor
if cycle r: cycle else: point infinity of r fi enddef;
vardef emergency_turningnumber primary r =
original_turningnumber(straighten_path(r))
enddef;
def use_emergency_turningnumber =
def turningnumber = emergency_turningnumber enddef;
enddef;
def use_original_turningnumber =
def turningnumber = original_turningnumber enddef;
enddef;
%% \--------------------------------------------------------------------
%% Loading a file optionally
%% \-
%% Opcjonalne czytanie pliku
%% \--------------------------------------------------------------------
boolean maybeinput_ok; string maybedir;
vardef maybename(text name) =
if known maybedir: maybedir & fi if string name: name else: str name fi
enddef;
def maybeinput text name =
if (readfrom maybename(name))=EOF:
message "PX: file " & maybename(name) & " cannot be read";
maybeinput_ok:=false;
else:
closefrom maybename(name); scantokens ("input " & maybename(name));
maybeinput_ok:=true;
fi
enddef;
%% \--------------------------------------------------------------------
%% A few colours more:
%% \-
%% Par/e dodatkowych kolor/ow:
%% \--------------------------------------------------------------------
color cyan, magenta, yellow; cyan=(0,1,1); magenta=(1,0,1); yellow=(1,1,0);
%% \--------------------------------------------------------------------
%% A few functions more:
%% \-
%% Par/e dodatkowych funkcji:
%% \--------------------------------------------------------------------
vardef gen_whatever(text type) = save ?; type ?; ? enddef; % 1-argument func.
vardef whatever_pair = gen_whatever(pair) enddef; % 0-argument function
vardef tand primary a = sind(a)/cosd(a) enddef;
vardef cotd primary a = cosd(a)/sind(a) enddef;
vardef signum primary x = if x>0: 1 elseif x<0: -1 else: 0 fi enddef;
primarydef w dotnorm z =
begingroup
save w_, z_, lw_, lz_; pair w_, z_;
lw_=abs(w); w_:=w if lw_>0: /lw_ fi;
lz_=abs(z); z_:=z if lz_>0: /lz_ fi;
(xpart w_ * xpart z_ + ypart w_ * ypart z_)
endgroup
enddef;
%
let ori_decimal=decimal;
def decimal primary n =
(
if path n:
for i_=0 upto length(n)-1: if i_>0: & " " & fi
decimal(point i_ of n) & " " & decimal(postcontrol i_ of n) & " " &
decimal(precontrol i_+1 of n) & " " & decimal(point i_+1 of n)
endfor
elseif color n: ori_decimal(redpart(n)) & " " &
ori_decimal(greenpart(n)) & " " & ori_decimal(bluepart(n))
elseif pair n: ori_decimal(xpart(n)) & " " & ori_decimal(ypart(n))
else: ori_decimal(n) fi
)
enddef;
%% \--------------------------------------------------------------------
%% \descriptioncomments
% The definition of |postdir| and |predir| given below is
% based on the following observation, being the consequence
% of the de~l'H\^ospital's rule: consider a~B\'ezier segment
% |a..controls b and c..d|; normally, the vector $\vec{ab}$
% determines the ``post'' direction at node~$a$; if $b$
% coincides with $a$, then the vector $\vec{ac}$ determines
% the direction; if also $c$ coincides coincides with~$a$,
% then the last resort is the vector $\vec{ad}$; if even $d$
% coincides with~$a$, the B\'ezier segment is degenerated,
% and can be removed (a~similar argumentation can be provided
% for the ``pre'' direction at node~$d$).
%% \-
%% \descriptioncomments
% Definicje makr |postdir| i~|predir| wykorzystuj/a nast/epuj/ac/a
% obserwacj/e, b/ed/ac/a prost/a konsekwencj/a regu/ly de~l'H\^ospitala:
% rozwa/zmy segment |a..controls b and c..d|; w~normalnej sytuacji
% kierunek ,,post'' w~punkcie~$a$ jest okre/slony przez
% wektor $\vec{ab}$; je/zeli punkty $a$ i~$b$ si/e pokrywaj/a,
% kierunek ,,post'' w~punkcie~$a$ jest okre/slony przez
% wektor $\vec{ac}$; je/zeli tak/ze punkty $a$ i~$c$ si/e pokrywaj/a,
% ostatni/a szans/a na wyznaczenie kierunku ,,post'' w~punkcie~$a$
% jest wektor $\vec{ad}$; je/zeli wszystkie punkty $a$, $b$, $c$, i~$d$
% si/e pokrywaj/a, to oznacza, /ze segment jest zdegenerowany i~mo/ze
% by/c w zasadzie usuni/ety (analogiczn/a argumentacj/e mo/zna przedstawi/c
% dla kierunku ,,pre'' w punkcie~$d$).
%% \--------------------------------------------------------------------
% Previous, insufficiently robust definitions:
% |vardef predir expr t of p = (point t of p)-(precontrol t of p) enddef;|
% |vardef postdir expr t of p = (postcontrol t of p)-(point t of p) enddef;|
% |vardef udir expr t of p = unitvector(direction t of p) enddef;|
% New, more general definitions:
vardef gendir expr t of p =
predir t of p + postdir t of p % |direction|-compatible definition
enddef;
vardef predir expr t of p =
save a_,b_,c_,d_,s_,t_; pair a_,b_,c_,d_; path s_; t_:=t;
if not cycle p: if t<0: t_:=0; elseif t>length(p): t_:=length(p); fi fi
s_=subpath (ceiling t_-1,t_) of p;
a_=point 0 of s_;
b_=postcontrol 0 of s_; % |b_<>postcontrol t-1 of p| for |t=0|
c_=precontrol 1 of s_;
d_=point 1 of s_;
if d_<>c_: d_-c_
elseif d_<>b_: d_-b_
elseif d_<>a_: d_-a_
else: (0,0)
fi
enddef;
vardef postdir expr t of p =
save a_,b_,c_,d_,s_,t_; pair a_,b_,c_,d_; path s_; t_:=t;
if not cycle p: if t<0: t_:=0; elseif t>length(p): t_:=length(p); fi fi
s_=subpath (t_,floor t_+1) of p;
a_=point 0 of s_;
b_=postcontrol 0 of s_;
c_=precontrol 1 of s_; % |c_<>precontrol t+1 of p| for |t=length p|
d_=point 1 of s_;
if a_<>b_: b_-a_
elseif a_<>c_: c_-a_
elseif a_<>d_: d_-a_
else: (0,0)
fi
enddef;
%% \--------------------------------------------------------------------
% Definitions related to ``pre-'' and ``post-'':
%% \-
% Definicje jako/s zwi/azane z~,,pre-'' i~,,post-'':
%% \--------------------------------------------------------------------
vardef udir expr t of p = unitvector(gendir t of p) enddef;
vardef upredir expr t of p = unitvector(predir t of p) enddef;
vardef upostdir expr t of p = unitvector(postdir t of p) enddef;
vardef pos_subpath expr z of p =
if not cycle p: subpath z of p else:
if xpart(z)<=ypart(z): subpath z of p
else: subpath (xpart(z),ypart(z)+length(p)) of p fi
fi
enddef;
%
vardef posttension expr t of p = % ``The \MF{}book'', ex. 14.15
save q_; path q_;
q_=point t of p {direction t of p}..{direction t+1 of p} point t+1 of p;
length(postcontrol 0 of q_ - point 0 of q_)/
length(postcontrol t of p - point t of p)% doesn't work for ``straight lines''
enddef;
vardef pretension expr t of p = % ditto
save q_; path q_;
q_=point t-1 of p {direction t-1 of p}..{direction t of p} point t of p;
length(precontrol 1 of q_ - point 1 of q_)/
length(precontrol t of p - point t of p)% doesn't work for ``straight lines''
enddef;
%% \--------------------------------------------------------------------
%% \descriptioncomments
% The two macros below, |path_eq| and |inside| macros, might have been
% primitives. The macro |path_eq| is obvious; |a inside b| returns true
% if the bounding box of |a| is inside the bounding box of |b|, which
% may be misleading; think, for example of:
% |fullcircle inside unitsquare shifted (-1/2,-1/2) scaled .9 rotated 45|.
% For most curves occuring in fonts, however, one can safely infere
% that if |a inside b| holds, then |a| is inside |b|.
%% \-
%% \descriptioncomments
% Poni/zsze dwa makra mog/lyby by/c w~zasadzie instrukcjami podstawowymi.
% Makro |path_eq| jest oczywiste. Wynikiem operacji |a inside b| jest
% ,,true'' je/zeli prostok/at ograniczaj/acy |a| le/zy wewn/atrz prostok/ata
% ograniczaj/acego |b|, co mo/ze dawa/c wynik myl/acy, np.~w~wypadku
% |fullcircle inside unitsquare shifted (-1/2,-1/2) scaled. 9 rotated 45|.
% Jednak/ze w~wi/ekszo/sci sytuacji wyst/epuj/acych fontach
% mo/zna /smia/lo zak/lada/c, /ze z~|a inside b| wynika, i/z |a| le/zy
% wewn/atrz~|b|.
%% \--------------------------------------------------------------------
vardef path_eq(expr a,b)=
save i_,l_,r_; boolean r_;
r_:=(length(a)=length(b)) and (cycle a= cycle b);
if r_:
i_:=0; l_:=length(a) if cycle a: -1 fi;
forever:
r_:=(point i_ of a = point i_ of b); exitif not r_;
r_:=(precontrol i_ of a = precontrol i_ of b); exitif not r_;
r_:=(postcontrol i_ of a = postcontrol i_ of b); exitif not r_;
exitif incr i_>l_;
endfor fi
r_
enddef;
%
tertiarydef a inside b =
if path a: % |and path b|
(xpart llcorner b < xpart llcorner a) and
(xpart urcorner b > xpart urcorner a) and
(ypart llcorner b < ypart llcorner a) and
(ypart urcorner b > ypart urcorner a)
else: % |numeric a and pair b|
(a>=xpart b) and (a<=ypart b)
fi
enddef;
%% \--------------------------------------------------------------------
% Two convenient macros more:
%% \-
% Jeszcze dwa wygodne makra:
%% \--------------------------------------------------------------------
vardef x_time expr x of p = % obsolete
xpart(p intersectiontimes ((x,-infinity)--(x,infinity)))
enddef;
vardef y_time expr y of p = % obsolete
xpart(p intersectiontimes ((-infinity,y)--(infinity,y)))
enddef;
vardef xtime expr x of p = % preferable alias
xpart(p intersectiontimes ((x,-infinity)--(x,infinity)))
enddef;
vardef ytime expr y of p = % preferable alias
xpart(p intersectiontimes ((-infinity,y)--(infinity,y)))
enddef;
%% \--------------------------------------------------------------------
%% A few figures more:
%% \-
%% Par/e dodatkowych figur:
%% \--------------------------------------------------------------------
vardef triangle =
(0,-1/2)--(0.866,0)--(0,1/2)--cycle % |1/2sqrt(3)| $\approx$ |0.866025...|
enddef;
vardef vpolygon(expr n) =
for i:=0 upto n-1: (1/2right rotated ((360/n)*(i+1/2))) -- endfor cycle
enddef;
%% \--------------------------------------------------------------------
%% \descriptioncomments
% A method, entangled a bit and not particularly robust, of testing whether
% a parameter is a {\it string\/} expression or a {\it suffix}.
% (Remark: |is_suffix((a))| or |is_suffix(a+b)| returns |true|;
% |is_suffix(((a)))| causes \MP{} to report an error).
%% \-
%% \descriptioncomments
% Nieco pokr/etna i niezbyt og/olna metoda sprawdzania, czy parametr
% jest wyra/zeniem typu {\it string\/} czy {\it sufiksem}.
% (Uwaga: |is_suffix((a))| b/ad/x |is_suffix(a+b)| zwraca |true|,
% za/s |is_suffix(((a)))| powoduje, /ze \MP{} zg/lasza b/l/ad).
%% \--------------------------------------------------------------------
vardef is_suffix(text suffix_or_not_suffix) =
save the_suffix_; string the_suffix_; is_suffix_ suffix_or_not_suffix;
the_suffix_<>""
enddef;
def is_suffix_ suffix $ = the_suffix_:= str $; killtext enddef;
%% \--------------------------------------------------------------------
%% \descriptioncomments
% The macro |&&| is to be used instead of the |&| operator if the respective
% ends of paths coincide only approximately; using |..| instead would add
% unwanted tiny B\'ezier segments. The macro is somewhat ``left-handed,''
% i.e., it does not consider the expression that follow the macro, therefore,
% it can be used before the `|cycle|' command; if the argument |p| of the
% macro |amp_amp_| is a |pair|, it is just ignored which may be
% considered hardly intuitive.
%% \-
%% \descriptioncomments
% Makra |&&| nale/zy u/zywa/c zamiast operatora |&| je/sli ko/nce /scie/zek
% nie pokrywaj/a si/e idealnie; u/zycie operatora |..| zamiast |&|
% spowodowa/loby dodanie do /scie/zki zb/ednych (ma/lych) segment/ow
% B\'eziera. Makro to jest nieco ,,lewor/eczne'', tzn.~nie analizuje
% wyra/zenia, kt/ore si/e po nim pojawi, dzi/eki czemu mo/ze by/c u/zyte
% przed operatorem ,|cycle|'; argument |p| makra |amp_amp_| b/ed/acy
% punktem (|pair|) jest pomijany, co mo/zna uzna/c za zachowanie ma/lo
% intuicyjne.
%% \--------------------------------------------------------------------
def && = amp_amp_ whatever enddef;
tertiarydef p amp_amp_ q =
if not pair p:
(subpath(0,length(p)-1) of p)..controls (postcontrol length(p)-1 of p)
and (precontrol length(p) of p)..
fi
enddef;
%% \--------------------------------------------------------------------
%% A few postfix and infix path operators:
%% \-
%% Par/e postfiksowych i~infiksowych operator/ow /scie/zkowych:
%% \--------------------------------------------------------------------
primarydef a mirr b = a reflectedabout(origin,b) enddef;
%
def store_prec_obj = store_prec_obj_ whatever enddef;
primarydef a store_prec_obj_ b = hide(def prec_obj = a enddef) enddef;
%
primarydef a sub b =
if path a: (pos_subpath b of a) elseif string a: (substring b of a) fi
enddef;
%
def node = store_prec_obj node_ enddef;
vardef node_@# primary a =
if str @#="x": xpart(point a of prec_obj)
elseif str @#="y": ypart(point a of prec_obj)
elseif str @#="": point a of prec_obj
else:
errhelp "The operator `node' works only with `x', `y' or an empty suffixes.";
errmessage "PX: improper usage of `node'";
fi
enddef;
%
def first suffix $ =
if str $="at": % moves the first point of a path to a specified location
store_prec_obj prec_obj shifted -(point 0 of prec_obj) shifted
else: node$(0) fi
enddef;
def last suffix $ =
if str $="at": % moves the last point of a path to a specified location
store_prec_obj prec_obj shifted
-(point if cycle prec_obj: 0 else: infinity fi of prec_obj) shifted
else: node$(if cycle prec_obj: 0 else: infinity fi) fi
enddef;
%
% node-governed flipping:
def nflipped = nflipped_ whatever enddef;
primarydef a nflipped_ b =
if cycle a: a
else: reverse(a reflectedabout (point 0 of a, point infinity of a))
fi
enddef;
%
def xflipped = xflipped_ whatever enddef;
primarydef a xflipped_ b =
reverse(a reflectedabout
(1/2[llcorner a, lrcorner a], 1/2[ulcorner a, urcorner a]))
enddef;
%
def yflipped = yflipped_ whatever enddef;
primarydef a yflipped_ b =
reverse(a reflectedabout
(1/2[llcorner a, ulcorner a], 1/2[lrcorner a, urcorner a]))
enddef;
%
% node-governed rotating (infix operator):
primarydef a nrotated b =
if cycle a: a
else: a rotatedaround(1/2[point 0 of a,point infinity of a], b)
fi
enddef;
%
% center-governed rotating (infix operator):
primarydef a crotated b =
a rotatedaround(1/2[llcorner a, urcorner a], b)
enddef;
%% \--------------------------------------------------------------------
%% Neat macros excerpted from John D. Hobby's boxes.mp macro package:
%% \-
%% Zgrabne makra zaczerpni/ete z zestawu makr boxes.mp Johna D. Hobby'ego:
%% \--------------------------------------------------------------------
% Find the length of the prefix of string |s| for which |cond| is true for
% each character |c| of the prefix
vardef generisize_prefix(expr s)(text cond) =
save i_, c_; string c_;
i_ = 0;
forever:
c_ := substring (i_,i_+1) of s;
exitunless cond; exitif incr i_=length s;
endfor
i_
enddef;
%
% Take a string returned by the |str| operator and return the same string
% with explicit numeric subscripts replaced by generic subscript symbols []:
vardef generisize(expr s) =
save res_, s_, l_; string res_, s_;
res_=""; % result so far
s_ =s; % left to process
forever: exitif s_="";
l_:=generisize_prefix(s_, (c_<>"[") and ((c_<"0") or (c_>"9")));
res_:=res_ & substring (0,l_) of s_;
s_:=substring (l_,infinity) of s_;
if s_<>"":
res_ := res_ & "[]";
l_ :=if s_>="[": 1+generisize_prefix(s_, c_<>"]")
else: generisize_prefix(s_, (c_=".") or ("0"<=c_) and (c_<="9")) fi;
s_:=substring(l_,infinity) of s_;
fi
endfor
res_
enddef;
%%\vfill\eject
%% \--------------------------------------------------------------------
%% \descriptioncomments
% The macro |extrapolate| computes a ``superpath'' (as opposed to
% ``subpath'') for a single B\'ezier segment in such a way
% that the following identity holds
% (for 0${}\le{}$|t1|${}\le{}$|t2|${}\le{}$1):
%% \-
%% \descriptioncomments
% Makro |extrapolate| wyznacza ,,nad/scie/zk/e'' (w~odr/o/znieniu od
% ,,pod/scie/zki'') dla pojedynczego /luku B\'eziera w~taki spos/ob,
% /ze poni/zsza r/owno/s/c jest spe/lniona (dla
% 0${}\xle{}$|t1|${}\xle{}$|t2|${}\xle{}$1):
%% %
%% \LINE{%
%% \descriptioncomments
% |subpath (t1,t2) of (extrapolate (t1,t2) of b) = b|
%% \unskip}
%% %
%% \descriptioncomments
% Below, there are the results of the command |extrapolate (.3,.7) of p|
% for three similarly defined paths. The black line denotes the source
% path, the gray one---its~extrapolation.
%% \-
%% \descriptioncomments
% Poni/zsza ilustracja przedstawia wynik polecenia |extrapolate (.3,.7) of p|
% dla trzech podobnie zdefiniowanych /scie/zek. Czarn/a lini/a zaznaczona
% zosta/la /scie/zka oryginalna, szar/a~--~ekstrapolowana.
%% %
%% \LINE{\valign{#\vss\cr
%% \hbox{\epsfbox{\illusname.100}}\cr\noalign{\hfil}
%% \hbox{\epsfbox{\illusname.101}}\cr\noalign{\hfil}
%% \hbox{\epsfbox{\illusname.102}}\cr
%% }}
%% %
%% \descriptioncomments
% Exercise 1. What happens if the relation
% 0${}\le{}$|t1|${}\le{}$|t2|${}\le{}$1
% is not fulfilled? (Hint: there are~a~few
% possible cases.)\vadjust{\smallskip}\break
% Exercise 2. True or false:
%% \-
%% \descriptioncomments
% Zadanie 1. Co by si/e sta/lo, gdyby warunek
% 0${}\xle{}$|t1|${}\xle{}$|t2|${}\xle{}$1 nie by/l spe/lniony?
% (Wskaz/owka: mo/zliwych jest kilka r/o/znych
% przypadk/ow.)\vadjust{\smallskip}\break
% Zadanie 2. Prawda czy fa/lsz:
%% %
%% \LINE{%
%% \descriptioncomments
% |point 1 of (extrapolate (t.a,t) of b) = point 1 of (extrapolate (t.b,t) of b)|
% \quad for\quad |t.a<>t.b|
%% \unskip}
%% %
%% \descriptioncomments
% Exercise 3. Try to imagine the result of the extrapolation for
% such weird (yet trivial) paths~as:%
%% \-
%% \descriptioncomments
% Zadanie 3. Spr/obuj przewidzie/c wynik ekstrapolacji dla tak dziwnych
% (chocia/z trywialnych) /scie/zek~jak:%
%% \LINE{%
%% \descriptioncomments
% |(0,0)..controls (0,0) and (100,0)..(100,0)| \quad or\quad
% |(0,0)..controls (100,0) and (0,0)..(100,0)|
%% \unskip}
%% \--------------------------------------------------------------------
vardef extrapolate expr t of b = % |t| pair, |b| B\'ezier segment
clearxy;
Casteljau(xpart(t)) = point 0 of b;
Casteljau(1/3[xpart(t),ypart(t)]) = point 1/3 of b;
Casteljau(2/3[xpart(t),ypart(t)]) = point 2/3 of b;
Casteljau(ypart(t)) = point 1 of b;
z0..controls z1 and z2..z3
enddef;
%
def Casteljau(expr t) =
t[t[t[z0,z1], t[z1,z2] ], t[t[z1,z2], t[z2,z3] ] ]
enddef;
%
vardef elongation_to_times(expr ea,eb) =
% negative parameter values are admissible; they are meant for |pen_stroke|
(if ea<0: - fi 1/(abs(ea)+1), eb/(abs(eb)+1))
enddef;
%% \--------------------------------------------------------------------
%% \descriptioncomments
% A~numerical function `|point_line_dist|' takes as a~parameter
% three |pair| expressions and returns a~(signed) value of the distance
% of the first parameter from the line defined by the other two.
% It is referred to in the `|is_line|' function.
%% \-
%% \descriptioncomments
% Funkcja ,|point_line_dist|', zale/zna od trzech wyra/ze/n typu |pair|,
% oblicza odleg/l/o/s/c (dodatni/a lub ujemn/a) pierwszego z~parametr/ow
% od linii okre/slonej przez dwa pozosta/le parametry. Do funkcji
% ,|point_line_dist|' odwo/luje si/e funkcja ,|is_line|'.
%% \--------------------------------------------------------------------
vardef point_line_dist(expr a,b,c) =
clearxy; save d_; d_=sqrt(length(b-c));
z0=a/d_; z1=b/d_; z2=c/d_;
(x2-x1)*(y1-y0)-(x1-x0)*(y2-y1)
enddef;
%% \--------------------------------------------------------------------
%% The following code (its idea is due to Dan Luecking and Larry Siebenmann)
%% computes the area surrounded by a cyclic path.
%% \-
%% Poni/zszy kod (pomys/l podsun/eli Dan Luecking i~Larry Siebenmann)
%% oblicza pole obszaru ograniczonego zamkni/et/a krzyw/a B\'eziera.
%% \--------------------------------------------------------------------
newinternal area_scale;
area_scale:=1; % decrease if the result is going to be too large
vardef area(expr p) = % |p| is a B\'ezier segment; result = $\int y\, dx$
save xa, xb, xc, xd, ya, yb, yc, yd;
(xa,20ya)=point 0 of p;
(xb,20yb)=postcontrol 0 of p;
(xc,20yc)=precontrol 1 of p;
(xd,20yd)=point 1 of p;
area_scale*(xb-xa)*(10ya + 6yb + 3yc + yd)
+area_scale*(xc-xb)*( 4ya + 6yb + 6yc + 4yd)
+area_scale*(xd-xc)*( ya + 3yb + 6yc + 10yd)
enddef;
vardef Area(expr P) = % |P| is a cyclic path
area(subpath (0,1) of P)
for t=1 upto length(P)-1: + area(subpath (t,t+1) of P) endfor
enddef;
%% \--------------------------------------------------------------------
%% The idea of calculation of a turning angle
%% between two vectors, employed in the definition of the function
%% `turn_ang,' is based on the following observation:
%% \-
%% Idea obliczania k/ata (skierowanego) mi/edzy dwoma wektorami,
%% wykorzystana w~funkcji ,turn_ang', zasadza si/e na nast/epuj/acej
%% obserwacji:
%% %
%% \LINE{%
%% \descriptioncomments
% |z reflectedabout(origin,right)=1/z|
%% \unskip}
%% %
%% for a complex number $z$ such that $\vbar z\vbar=1$;
%% recall also that multiplication of complex numbers
%% (`zscaled' operation) implies addition of their angle arguments.
%% \-
%% dla liczby zespolonej $z$ takiej, /ze $\vbar z\vbar=1$; przypomnijmy
%% tak/ze, /ze mno/zeniu liczb zespolonych (operacja ,zscaled')
%% odpowiada dodawanie argument/ow k/atowych.
%% \--------------------------------------------------------------------
vardef turn_ang(expr za,zb) =
if (abs(za)>=1/1000) and (abs(zb)>=1/1000): % |eps| may be not enough
angle(unitvector(za) zscaled (unitvector(zb) reflectedabout (origin,right)))
else: whatever fi
enddef;
%% \--------------------------------------------------------------------
%% \descriptioncomments
% A~Boolean function `|is_line|' checks whether a~given B\'ezier segment
% is a~straight line. For large segments (fonts) it makes sense to specify
% a~numerical parameter |is_line_off>=0|; it defines a~maximal acceptable
% distance of the control points of a~B\'ezier arc from its secant
% (which corresponds to the distance between the arc and the secant
% circa |3/4is_line_off| for a symmetric, inflexionless arcs).
%% \-
%% \descriptioncomments
% Boole'owska funkcja, ,|is_line|' sprawdza, czy dany segment B\'eziera
% jest lini/a prost/a. Dla du/zych segment/ow (fonty) mo/ze mie/c sens zadanie
% parametru numerycznego |is_line_off>=0|; okre/sla on maksymalne dopuszczalne
% odchylenie naci/ag/ow krzywej B\'eziera od siecznej (co odpowiada
% odleg/lo/sci krzywej od siecznej wynosz/acej ok.~|3/4is_line_off|
% dla symetrycznych /luk/ow bez punkt/ow przegi/ecia).
%% \--------------------------------------------------------------------
vardef is_line(expr B) =
save r_; boolean r_;
if known is_line_off:
save a_;
a_=length((point 1 of B)-(point 0 of B));
r_=(-a_+arclength(B))<=(a_/infinity);
if r_:
r_:=(is_line_off>=abs(point_line_dist(
postcontrol 0 of B, point 0 of B, point 1 of B))) and
(is_line_off>=abs(point_line_dist(
precontrol 1 of B, point 0 of B, point 1 of B)));
fi
else: % backward compatibility
save a_,b_,c_,d_;
a_=length((point 1 of B)-(point 0 of B));
b_=length((postcontrol 0 of B)-(point 0 of B));
c_=length((precontrol 1 of B)-(postcontrol 0 of B));
d_=length((point 1 of B)-(precontrol 1 of B));
r_=(-a_+b_+c_+d_ <= a_/infinity);
fi
r_
enddef;
%% \--------------------------------------------------------------------
%% Abbreviations for a few simple yet useful phrases:
%% \-
%% Skr/oty dla kilku prostych acz przydatnych fraz:
%% \--------------------------------------------------------------------
def xyscaled primary p = xscaled xpart(p) yscaled ypart(p) enddef;
def yxscaled primary p = yscaled xpart(p) xscaled ypart(p) enddef;
primarydef a xscaledto b =
hide(lastscale:=b/(xpart(urcorner(a))-xpart(llcorner(a))))
a xscaled lastscale
enddef;
primarydef a xyscaledto b =
hide(lastscale:=b/(xpart(urcorner(a))-xpart(llcorner(a))))
a scaled lastscale
enddef;
primarydef a yscaledto b =
hide(lastscale:=b/(ypart(urcorner(a))-ypart(llcorner(a))))
a yscaled lastscale
enddef;
primarydef a yxscaledto b =
hide(lastscale:=b/(ypart(urcorner(a))-ypart(llcorner(a))))
a scaled lastscale
enddef;
%
pair lastshift;
primarydef a llshiftedto b =
hide(lastshift:=-llcorner(a)+b) a shifted lastshift
enddef;
primarydef a lrshiftedto b =
hide(lastshift:=-lrcorner(a)+b) a shifted lastshift
enddef;
primarydef a urshiftedto b =
hide(lastshift:=-urcorner(a)+b) a shifted lastshift
enddef;
primarydef a ulshiftedto b =
hide(lastshift:=-ulcorner(a)+b) a shifted lastshift
enddef;
primarydef a ccshiftedto b =
hide(lastshift:=-center(a)+b) a shifted lastshift
enddef;
%% \--------------------------------------------------------------------
%% Joining two paths at their intersection point:
%% \-
%% /L/aczenie dw/och scie/zek w punkcie ich przeci/ecia:
%% \--------------------------------------------------------------------
tertiarydef a intersection_join b = % like |softjoin|
begingroup save t_;
(t_1,t_2)=a intersectiontimes b; a.sub(0,t_1)&&b.sub(t_2,infinity)
endgroup
enddef;
%% \--------------------------------------------------------------------
%% Changing locally non-internal variables (sometimes we want to set locally
%% not only numeric variables):
%% \-
%% Lokalna zmiana warto/sci zwyk/lych zmiennych (czasami zachodzi potrzeba
%% lokalnej zmiany nie tylko zmiennych numerycznych):
%% \--------------------------------------------------------------------
def local suffix s =
begingroup
save local_stack_value_, local_stack_name_;
if pair s: pair local_stack_value_; fi
if path s: path local_stack_value_; fi
if picture s: picture local_stack_value_; fi
if string s: string local_stack_value_; fi
if color s: color local_stack_value_; fi
local_stack_value_ = s; def local_stack_name_ = s enddef;
local_
enddef;
def local_ expr x = local_stack_name_:=x enddef;
def endlocal = local_stack_name_:=local_stack_value_; endgroup; enddef;
%% \--------------------------------------------------------------------
%% \descriptioncomments
% The following abbreviation is roughly equivalent to the Knuthian
% {\it of-the-way function}, namely, to the |whatever[z1,z2]|
% operator; observe, however, that this construction requires that
% both |z1| and |z2| should be known, while |z1| the in
% the construction |z1^z2| can be unknown.
%% \-
%% \descriptioncomments
% Poni/zszy skr/ot jest z grubsza r/ownowa/zny Knuthowej funkcji
% {\it w-p/o/l-drogi}, tzn. operatorowi |whatever[z1,z2]|;
% odnotujmy wszak/ze, /ze konstrukcja ta wymaga, by zar/owno
% |z1| jak i~|z2| by/ly znane, natomiast konstrukcja |z1^z2|
% pozwala, by |z1| by/lo nieznane.
%% \--------------------------------------------------------------------
primarydef a ^ b = a + whatever * b enddef;
%% \--------------------------------------------------------------------
%% \descriptioncomments
% The macro `leg' computes the leg of a right-angled triangle,
% given a hypotenuse (vector, parameter~|c|) and a length
% of one leg (parameter~|b|).
%% \-
%% \descriptioncomments
% Makro ,leg' oblicza przyprostok/atn/a tr/ojk/ata prostok/atnego
% przy za/lo/zeniu, /ze znana jest przeciwprostok/atna (wektor,
% parametr~|c|) i~przyprostok/atna (parametr~|b|).
%% \--------------------------------------------------------------------
primarydef c leg b =
begingroup save a_; pair a_;
a_+b/(length(c)+-+b)*(a_ rotated -90)=c; % |(length(c)+-+b)=length(a_)|
a_
endgroup
enddef;
%% \--------------------------------------------------------------------
%% This extremely simple macro is particularly
%% useful for constructing sloped objects. Assume, for example, that
%% we want to draw the following parallelogram:
%% \-
%% To pro/sciutkie makro jest szczeg/olnie u/zyteczne przy konstruowaniu
%% uko/snych obiekt/ow. Za/l/o/zmy, /ze chcemy narysowa/c nast/epuj/acy
%% r/ownoleg/lobok:
%% %
%% \LINE{\epsfbox{\illusname.103}}
%% %
%% \descriptioncomments
% (given |h|, |w| and |b|). This is exactly the situation, where
% the macro `leg' comes handy in. Given |z0| and |z2|, the remaining
% points |z1| and |z3| can be easily determined from the following
% relations:
%% \-
%% \descriptioncomments
% dla zadanych wielko/sci |h|, |w| i~|b|. To jest w/la/snie
% ta~sytuacja, w~kt/orej makro ,leg' okazuje si/e u/zyteczne.
% Maj/ac dane punkty |z0| i~|z2|, punkty |z1| i~|z3| wyznaczy/c
% mo/zna z~relacji:
%% %
%% \LINE{%
%% \descriptioncomments
% |z1=z0+whatever*((z2-z0) leg (-b));|\quad
% |y1=y2;|\quad |z1-z2=z0-z3;|
%% \unskip}
%% %
%% Note the minus preceding the second argument to~the~`leg';
%% the positive value means ``leftwards,'' negative---``rightwards''
%% (with respect to hypotenuse vector).
%% \-
%% Odnotujmy obecno/s/c minusa przed drugim argumentem makra ,leg';
%% warto/s/c dodatnia oznacza ,,po~lewej'', ujemna~--~,,po prawej''
%% (wzgl/edem wektora przeciwprostok/atnej).
%% \--------------------------------------------------------------------
%% \medskip
%% \--------------------------------------------------------------------
%% {\descriptioncomments
% The macro |quicksort| sorts |@#||s.i...@#||s.j| along with
% |@#|.|$i...@#|.|$j| for |$| $\in$ |t|, using Tony Hoare's ``quick sort''
% method; suffix |s| must must not occur in the |t| list (no checking
% is performed); if both |s| and |t| are empty, |t| is ignored.
%% }
%% \bull Remark 1: the algorithm has no explicit recursion, because of
%% \MF{}//\MP{} limits on recursion level.
%% \bull Remark 2: the algorithm is not stable, i.e., it does not
%% preserve the order of equal items.
%% \bull {\descriptioncomments
% Sample usage: |quicksort| |A(1,100)()(x,y)| will sort |A1|,
% |A2,...|$\,$|,A100| (comparing |A.i| with |A.j|) and, moreover,
% |A.x1, A.y1,...|$\,$|,A.x100, A.y100| will be reordered simultaneously.
%% }
%% \-
%% {\descriptioncomments
% Makro |quicksort| sortuje |@#||s.i...@#||s.j| wraz
% z~|@#|.|$i...@#|.|$j| dla |$| $\in$ |t|, z~u/zyciem algorytmu
% ``quick sort'', kt/orego autorem jest Tony Hoare;
% sufiks |s| nie mo/ze wyst/api/c w~li/scie |t| list (za/lo/zenie to
% nie jest sprawdzane); je/sli parametry |s| i~|t| s/a r/ownocze/snie
% puste, parametr |t| ignorowany.
%% }
%% \bull Uwaga 1: algorytm nie korzysta z~jawnej rekursji ze wzgl/edu
%% na ograniczenia implementacyjne \MF{}//\MP{}.
%% \bull Uwaga 2: algorytm jest niestabilny, tzn. nie zachowuje
%% kolejno/sci r/ownych obiekt/ow.
%% \bull {\descriptioncomments
% Przyk/ladowe u/zycie: |quicksort| |A(1,100)()(x,y)| posortuje |A1|,
% |A2,...|$\,$|,A100| (por/ownuj/ac |A.i| z~|A.j|), ponadto
% |A.x1, A.y1,...|$\,$|,A.x100, A.y100| zostan/a r/ownocze/snie
% odpowiednio przestawione.
%% }
%% \--------------------------------------------------------------------
vardef quicksort@#(expr i,j)(suffix s)(text t) =
save i_,j_,k_,l_,cell_,stack_,incl_t_; boolean incl_t_;
pair stack_[\\]; stack_.lev:=0; stack_[incr stack_.lev]:=(i,j);
i_:=0; for $:=t: i_:=i_+1; endfor % ``measure'' |t|-list
incl_t_:=(str s <> "") or ((str s = "") and (i_<>0));
forsuffixes $:= s if incl_t_: , t fi:
if numeric @#.$[i]: numeric cell_.$;
elseif string @#.$[i]: string cell_.$;
elseif boolean @#.$[i]: boolean cell_.$;
fi
endfor
forever:
exitif stack_.lev<=0;
numeric i_,j_; (i_,j_)=stack_[stack_.lev]; stack_.lev:=stack_.lev-1;
if i_<j_:
forsuffixes $:= s if incl_t_: , t fi: cell_.$:=@#.$[i_]; endfor
l_:=i_;
for k_:=i_+1 upto j_:
if is_less(@#.s[k_],cell_.s):
forsuffixes $:=s if incl_t_: , t fi:
@#.$[l_]:=@#.$[k_]; @#.$[k_]:=@#.$[l_+1];
endfor
l_:=l_+1;
fi
endfor
forsuffixes $:= s if incl_t_: , t fi: @#.$[l_]:=cell_.$; endfor
stack_[incr stack_.lev]:=(i_,l_-1); stack_[incr stack_.lev]:=(l_+1,j_);
fi
endfor
enddef;
vardef is_less(expr a,b) = (a<b) enddef;
%% \--------------------------------------------------------------------
%% \descriptioncomments
% The macro |soften_path| rounds all corners of a path |p|;
% |r| is the radius. Use |soften_nodes| for rounding corners at
% a given set of nodes; its text parameter |t| is a comma-separated
% list of either numbers or pairs: a number means the number
% of a node, a pair means the number of a node and a local
% radius, to be used instead of~|r|; prior to insertion
% the list of nodes is sorted.
%% \-
%% \descriptioncomments
% Macro |soften_path| zaokr/agla naro/za /scie/zki |p|;
% |r|~oznacza promie/n. Aby zaokr/agli/c naro/za w~wybranych w/ez/lach
% nale/zy u/zy/c makra |soften_nodes|; parametr tekstowy~|t| tego
% makra jest list/a (separowan/a przecinkami) liczb lub par:
% liczba oznacza numer w/ez/la, para~-- numer w/ez/la i~lokalny
% promie/n zaokr/aglenia (zamiast~|r|); lista w/ez/l/ow przed
% wstawieniem jest sortowana.
%% \--------------------------------------------------------------------
vardef soften_node(expr p,r,t) = % path, radius, node (i.e., time)
save q_; path q_; interim join_radius:=r;
if cycle p:
q_=(subpath (t-1,t) of p) softjoin (subpath (t,t+length(p)-1) of p) & cycle;
(subpath(1-t, 1+length(q_)-t) of q_) & cycle % re-position origin
else: (subpath (0,t) of p) softjoin (subpath (t,length(p)) of p) fi
enddef;
%
vardef soften_nodes(expr p,r) (text t) =
save j_, n_, p_, r_, t_; path p_; p_:=p;
t_:=0; for i_:=t: (n_[incr t_],r_[t_])=if pair i_: i_ else: (i_,r) fi; endfor
quicksort(1,t_)(n_)(r_);
j_:=-1; for i_:=1 upto t_: p_:=soften_node(p_,r_[i_],n_[i_]+incr j_); endfor;
p_
enddef;
%
vardef soften_path(expr p,r) = % path, radius
save p_; path p_; p_:=p;
if r>0:
for i_:=if cycle p: 0 else: 1 fi step 2 until 2(length(p)-1):
p_:=soften_node(p_,r,i_);
endfor;
fi
p_
enddef;
%% \--------------------------------------------------------------------
%% \descriptioncomments
% The macro |insert_nodes| inserts additional nodes at given non-integer
% non-repeating times~|t| into a given path |p|.
% The code would be a bit longer without `|arclength|' and `|arctime|.'
% The macro can be useful in some cases in the context of finding
% the envelopes of pen-stroked paths (avoiding inflection
% points---see below).
%% \-
%% \descriptioncomments
% Macro |insert_nodes| wstawia w~/scie/zce~|p| dodatkowe
% w/ez/ly w~punktach odpowiadaj/acych czasom~|t| (nieca/lkowitym,
% niepowtarzaj/acym si/e). Bez funkcji ,|arclength|' i~,|arctime|'
% kod by/lby nieco d/lu/zszy. Makro to mo/ze by/c przydatne przy
% wyznaczaniu obrysu pi/orka (unikanie punkt/ow przegi/ecia -- p.~ni/zej).
%% \--------------------------------------------------------------------
vardef insert_nodes(expr p)(text t) =
save j_, p_, s_, t_; path p_; p_:=p;
t_:=0;
for i_:=t:
if round(i_)<>i_: % ignore integer times
t_[incr t_]=arclength(subpath(0,i_ mod length(p_)) of p_);
fi
endfor
for i_:=1 upto t_:
s_:=arctime t_[i_] of p_;
if abs(round(s_)-s_)>eps: % ignore repeating times; is |eps| OK?
p_:=(subpath (0, s_) of p_) && (subpath (s_,length p_) of p_)
if cycle p_: & cycle fi;
fi
endfor;
p_
enddef;
%% \--------------------------------------------------------------------
%% \descriptioncomments
% The macro |delete_nodes| removes, as the name suggests, selected nodes
% from a given (cyclic) path. The macro can be useful for removing
% superfluous nodes, e.g., in case of improving fonts converted
% from PFB to \MP{}. The nodes can be removed in three possible
% ways: with both control nodes, with the precontrol node, or with
% the postcontrol one.
% During the process of removal, additional control
% nodes may be removed. More precisely: given a path |p|, let
% |z_[i_]| denothes its |i_|-th node, |z_[i_]b|---the respective
% precontrol node (``before''), |z_[i_]a|---the respective postcontrol
% node (``after''). Macro |delete_nodes| removes elements (by assigning
% an undefined value) from the tables |z_[\\]|, |z_[\\]b| and |z_[\\]a|
% in such a way that the following invariant holds for the main loop:
% for any pair of consecutive known nodes |z_[i_]| and |z_[j_]|,
% |i_<j_|, there exist exactly two indices |u_| and |v_| such that
% |i_<=u_<v_<=j_| and both |z_[u_]a| and |z_[v_]b| are known; moreover,
% |z_[w_]a| and |z_[w_]b| are unknown for the remaining indices |w_|,
% |i_<=w_<=j_|. The resulting path is constructed from the elements
% that remain in the table.\break
% Nodes to be removed are passed as the list of pairs
% (text parameter |node_list|):
% |(index,kind)|, where |index| denotes the time of
% the path corresponding to a given node, and |kind|
% is a number. If |kind=0|, both control nodes are to be removed;
% if~|kind<0|, a~precontrol node is to be removed;
% if~|kind>0|, a~postcontrol node is to be removed.\break
% The figure below shows the results of the |delete_nodes|
% applied to a sample path |p| (|p=fullcircle|): the topmost picture depicts
% the source path and the numbering of nodes, the left lower picture
% is the path returned by
% |delete_nodes(p)((1,-1), (3,-1), (5,-1), (7,-1))|
% middle lower one---by
% |delete_nodes(p)((1,0), (3,0), (5,0), (7,0))|,
% right lower one---by
% |delete_nodes(p)((1,1), (3,1), (5,1), (7,1))|.
%% \-
%% \descriptioncomments
% Makro |delete_nodes|, jak sama nazwa wskazuje, usuwa wskazane w/ez/ly
% z~danej (cyklicznej) /scie/zki. Makro mo/ze si/e przyda/c do usuwania
% zb/ednych w/ez/l/ow, na przyk/lad przy poprawianiu font/ow zamienionych
% z~postaci PFB na posta/c \MP{}-ow/a. W/ez/ly s/a usuwane na trzy mo/zliwe
% sposoby: wraz z~obydwoma przyleg/lymi naci/agami, z~poprzedzaj/acym
% lub z~nast/epuj/acym naci/agiem. W~ka/zdym z~przypadk/ow mog/a zosta/c
% te/z usuni/ete s/asiaduj/ace naci/agi.
% Dok/ladniej: dla danej /scie/zki |p| niech |z_[i_]| oznacza |i_|-ty w/eze/l,
% |z_[i_]b| -- jego naci/ag poprzedzaj/acy, |z_[i_]a| -- jego naci/ag
% nast/epuj/acy. Makro |delete_nodes| usuwa elementy (poprzez nadanie
% warto/sci nieokre/slonej) z~tablic |z_[\\]|, |z_[\\]b|
% i~|z_[\\]a| w~taki spos/ob, by zachowany by/l nast/epuj/acy
% niezmiennik: dla dowolnej pary s/asiaduj/acych w/ez/l/ow
% |z_[i_]| i~|z_[j_]|, |i_<j_|, istniej/a dok/ladnie dwa indeksy
% |u_| i~|v_|, takie /ze |i_<=u_<v_<=j_| i~zar/owno |z_[u_]a|,
% jak i~|z_[v_]b| s/a znane; ponadto |z_[w_]a| i~|z_[w_]b| s/a
% nieokre/slone dla pozosta/lych indeks/ow~|w_|, |i_<=w_<=j_|.
% Z element/ow, kt/ore pozostaj/a w~tablicy, budowana jest
% /scie/zka wynikowa.\break
% W/ez/ly, kt/ore maj/a by/c usuni/ete, s/a podawane jako lista par
% (parametr tekstowy |node_list|): |(indeks,rodzaj)|, gdzie |indeks| oznacza
% czas odpowiadaj/acy danemu w/ez/lowi na /scie/zce, a~|rodzaj|
% jest liczb/a; je/sli |rodzaj=0|, to usuwane s/a oba naci/agi,
% je/sli |rodzaj<0|, to usuwany jest naci/ag poprzedzaj/acy,
% je/sli |rodzaj>0|, to usuwany jest naci/ag nast/epuj/acy.\break
% Efekt dzia/lania makra dla przyk/ladowej /scie/zki |p| (|p=fullcircle|)
% jest przedstawiony na poni/zszym rysunku: g/orna ilustracja przedstawia
% /scie/zk/e /xr/od/low/a z~numeracj/a w/ez/l/ow, lewa dolna ilustracja
% to rezultat u/zycia operacji
% |delete_nodes(p)((1,-1), (3,-1), (5,-1), (7,-1))|
% /srodkowa dolna -- operacji
% |delete_nodes(p)((1,0), (3,0), (5,0), (7,0))|,
% prawa dolna -- operacji
% |delete_nodes(p)((1,1), (3,1), (5,1), (7,1))|.
%% %
%% \LINE{\vbox{\kern3mm\hbox{\epsfbox{\illusname.104}}}}
%% \--------------------------------------------------------------------
vardef delete_nodes(expr p)(text node_list) = % |p| is cyclic
save o_,i_,j_,j__,l_,n_,z_; pair z_[\\],z_[\\]a,z_[\\]b;
n_=length(p);
for i_:=0 upto n_:
z_[i_]:=point i_ of p;
z_[i_]a:=postcontrol i_ of p;
z_[i_]b:=precontrol i_ of p;
endfor
for l_:=node_list:
% here the above-mentioned invariant holds (the result may depend
% on the order of the nodes to be deleted, if neighbouring nodes
% are being deleted)
i_:=xpart(l_) mod n_; % to |mod| or not to |mod|?
% to ignore (with a |message|) or to apply the |mod| operation?
z_[i_]:=whatever_pair;
if ypart(l_)=0: % delete predecessing |b|-node and successing |a|-node
j_:=i_; forever: exitif known z_[j_]b; j_:=(j_-1) mod n_; endfor
z_[j_]b:=whatever_pair;
j_:=i_; forever: exitif known z_[j_]a; j_:=(j_+1) mod n_; endfor
z_[j_]a:=whatever_pair;
elseif ypart(l_)>0: % delete successing |a|-node and |b|-node
j_:=i_; forever: exitif known z_[j_]a; j_:=(j_+1) mod n_; endfor
z_[j_]a:=whatever_pair;
j_:=i_; forever: j_:=(j_+1) mod n_; exitif known z_[j_]b; endfor
z_[j_]b:=whatever_pair;
else: % |ypart(l_)<0|; delete predecessing |a|-node and |b|-node
j_:=i_; forever: j_:=(j_-1) mod n_; exitif known z_[j_]a; endfor
z_[j_]a:=whatever_pair;
j_:=i_; forever: exitif known z_[j_]b; j_:=(j_-1) mod n_; endfor
z_[j_]b:=whatever_pair;
fi
endfor
i_:=0; forever: exitif known z_[i_]; i_:=i_+1; endfor; o_:=i_;
for i_:=o_ upto n_-1+o_:
hide(j_:=i_ mod n_; j__:=(i_+1) mod n_)
if known z_[j_]: z_[j_] fi
if known z_[j_]a:..controls z_[j_]a fi
if known z_[j__]b: and \\ z_[j__]b..fi
endfor \\ z_[o_] & cycle
enddef;
%% \--------------------------------------------------------------------