aboutsummaryrefslogtreecommitdiffstats
path: root/include/graphf.lpg
blob: 209ae7596bc05aa45f482795d68a6ac10e3a2ce3 (plain) (blame)
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
%%BeginResource: procset LoutGraphPrependGraphic
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                    %
%  PostScript @SysPrependGraphic file for @Graph (Version 1.0)       %
%                                                                    %
%  Version 1.0 by Jeffrey H. Kingston, December 1993.                %
%  swapxandy added September 2001 by JHK.                            %
%                                                                    %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

errordict begin
   /handleerror
   {
      {  /Times-Roman findfont 8 pt scalefont setfont
	 0 setgray 4 pt 4 pt moveto
	 $error /errorname get
	 dup lgraphdict exch known
	 { lgraphdict exch get }
	 { 30 string cvs } ifelse
	 show
	 (  Command: ) show
	 $error /command get 30 string cvs show
      } stopped {} if
      showpage stop
   } def
end

/lgraphdebugposy 432 def
/lgraphdebugposx 72 def

% - lgraphdebugnextline -
/lgraphdebugnextline
{
  lgraphdebugposy 72 lt
  { /lgraphdebugposx lgraphdebugposx 144 add store
    /lgraphdebugposy 432 store
  }
  {
    /lgraphdebugposy lgraphdebugposy 12 sub store
  } ifelse
  lgraphdebugposx lgraphdebugposy moveto
} def

% - lgraphdebugbeginindent -
/lgraphdebugbeginindent
{
  /lgraphdebugposx lgraphdebugposx 12 add store
} def

% - lgraphdebugendindent -
/lgraphdebugendindent
{
  /lgraphdebugposx lgraphdebugposx 12 sub store
} def

% <string> <int> lgraphdebugprint -
% must be defined outside lgraphdict since used in arbitrary places
% print <string> plus count or <int> stack entries, whichever is the smaller
/lgraphdebugprint
{
  exch
  gsave
  initgraphics
  lgraphdebugnextline
  /Times-Roman findfont 10 scalefont setfont
  0 setgray show
  lgraphdebugbeginindent
  count 1 sub 2 copy lt { pop } { exch pop } ifelse 1 sub
  0 exch 1 exch
  {
    lgraphdebugnextline
    index
    dup type (dicttype) eq
    {
      (begin dict) show
      lgraphdebugbeginindent
      {
	lgraphdebugnextline
	pop 100 string cvs show
      } forall
      lgraphdebugendindent
      lgraphdebugnextline
      (end dict) show
    }
    {
      dup type (arraytype) eq
      {
        (begin array) show
        lgraphdebugbeginindent
	{
	  lgraphdebugnextline
	  100 string cvs show
	} forall
	lgraphdebugendindent
	lgraphdebugnextline
        (end array) show
      }
      {
        100 string cvs show
      } ifelse
    } ifelse
  } for
  lgraphdebugendindent
  grestore
} def


/lgraphdict 200 dict def
lgraphdict begin

% error messages
/dictfull (dictfull error) def
/dictstackoverflow (dictstackoverflow error) def
/execstackoverflow (execstackoverflow error:  expression too complex?) def
/limitcheck (limitcheck error:  graph too complex or too large?) def
/syntaxerror (syntaxerror error:  syntax error in text of graph?) def
/typecheck (typecheck error:  syntax error in text of graph?) def
/undefined (undefined error:  unknown or misspelt symbol?) def
/rangecheck (rangecheck error:  undefined expression (e.g. divide by zero)?) def
/VMError (VMError error:  run out of memory?) def

% random number between x and y inclusive: x y dorand num
/dorand { 1 index sub 1 add rand exch mod add } def

% log to given base: base num dolog num
/dolog { ln exch ln div } def

% maximum of two numbers:  <num> <num> max <num>
/max { 2 copy gt { pop } { exch pop } ifelse } def

% add two points:  <point> <point> padd <point>
/padd { exch 3 1 roll add 3 1 roll add exch } def

% subtract first point from second:  <point> <point> psub <point>
/psub { 3 2 roll sub 3 1 roll exch sub exch } def

% distance between two points:  <point> <point> distance <length>
/distance { psub dup mul exch dup mul add sqrt } def

% point at angle and distance:  <point> <length> <angle> atangle <point>
/atangle { 2 copy cos mul 3 1 roll sin mul padd } def

% angle from one point to another:  <point> <point> angle <angle>
/angle { psub 2 copy 0 eq exch 0 eq and {pop} {exch atan} ifelse } def


% set up for line
% - linesetup <length> <dashlength>
/linesetup
{ newpath
  xcurr ycurr trpoint xprev yprev trpoint
  4 copy moveto lineto distance dashlength
} def

% set up for icon-avoiding line
% - ilinesetup <length> <dashlength>
/ilinesetup
{ newpath
  xprev yprev trpoint xcurr ycurr trpoint 4 copy
  4 copy angle symbolsize 1.5 mul exch 4 2 roll pop pop atangle
  6 2 roll 4 2 roll
  4 copy angle symbolsize 1.5 mul exch 4 2 roll pop pop atangle 
  4 copy moveto lineto distance dashlength
} def


% stroke a solid line:  <length> <dashlength> solid -
/solid
{ pop pop [] 0 setdash linewidth setlinewidth stroke
} def
  
% stroke a dashed line:   <length> <dashlength> dashed -
/dashed
{  2 copy 2 mul le 1 index 0 le or
   {  exch pop 1 pt max [ exch dup ] 0 setdash }
   {  dup [ exch 4 2 roll 2 copy div
      1 sub 2 div ceiling dup 4 1 roll
      1 add mul sub exch div ] 0 setdash
   } ifelse linewidth setlinewidth stroke
} def

% stroke a cdashed line:  <length> <dashlength> cdashed -
/cdashed
{  2 copy le 1 index 0 le or
   { exch pop 1 pt max [ exch dup ] dup 0 get 2 div setdash }
   { dup [ 4 2 roll exch 2 copy exch div
     2 div ceiling div 1 index sub
     ] exch 2 div setdash
   } ifelse linewidth setlinewidth stroke
} def

% stroke a dotted line:  <length> <dashlength> dotted -
/dotted
{  2 copy le 1 index 0 le or
   { exch pop 1 pt max [ exch 0 exch ] 0 setdash }
   { 1 index exch div ceiling div 0.99999 mul
     [ 0 3 2 roll ] 0 setdash
   } ifelse gsave 1 setlinecap linewidth setlinewidth stroke grestore newpath
} def

% stroke a noline line:  <length> <dashlength> noline -
/noline
{ pop pop
} def

% scale array elements by factor:  <array> <factor> scalearray <array>
/scalearray
{
  [ exch 3 2 roll
  { exch dup 3 1 roll mul exch } forall
  pop ]
} def

% sum array elements:  <array> sumarray <number>
/sumarray
{
  0 exch { add } forall
} def

% begin a more complex line: <length> <dashlength> linebegin -
/linebegin
{
  % (Entering linebegin) 2 debugprint
  20 dict begin
  /dashlen exch 1 pt max def
  /len exch def
  /gap dashlen def
  /halfgap dashlen 2 div def
  /dash dashlen def
  /halfdash dashlen 2 div def
  /dot 0 def
  % (Leaving linebegin) 0 debugprint
} def
% end a more complex line: <linecap> <cycle> <startpos> <stoppos> lineend -
/lineend
{
  % (Entering lineend) 3 debugprint
  /stoppos exch def
  /startpos exch def
  /cycle exch def
  /linecap exch def
  /stopposlen stoppos sumarray def
  /startposlen startpos sumarray def
  /cyclelen cycle sumarray def
  /effectivelen len startposlen add stopposlen sub def
  effectivelen 0 gt cyclelen 0 gt and
  {
    /repeats effectivelen cyclelen div ceiling def
    /factor len repeats cyclelen mul startposlen sub stopposlen add div def
    cycle factor scalearray startposlen factor mul setdash
    linecap setlinecap stroke
  } if
  end
  % (Leaving lineend) 0 debugprint
} def

% stroke a dotdashed line: <length> <dashlength> dotdashed -
/dotdashed
{
  linebegin
  1 [dash gap dot gap] [] [dash]
  lineend
} def

% stroke a dotcdashed line: <length> <dashlength> dotcdashed -
/dotcdashed
{
  linebegin
  1 [dash gap dot gap] [halfdash] [halfdash]
  lineend
} def

% stroke a dotdotdashed line: <length> <dashlength> dotdotdashed -
/dotdotdashed
{
  linebegin
  1 [dash gap dot gap dot gap] [] [dash]
  lineend
} def

% stroke a dotdotcdashed line: <length> <dashlength> dotdotcdashed -
/dotdotcdashed
{
  linebegin
  1 [dash gap dot gap dot gap] [halfdash] [halfdash]
  lineend
} def

% stroke a dotdotdotdashed line: <length> <dashlength> dotdotdotdashed -
/dotdotdotdashed
{
  linebegin
  1 [dash gap dot gap dot gap dot gap] [] [dash]
  lineend
} def

% stroke a dotdotdotcdashed line: <length> <dashlength> dotdotdotcdashed -
/dotdotdotcdashed
{
  linebegin
  1 [dash gap dot gap dot gap dot gap] [halfdash] [halfdash]
  lineend
} def


% stroke a y histogram: - yhisto -
/yhisto
{ xprev yleft trpoint yextra sub moveto
  xprev yprev trpoint lineto
  xcurr yprev trpoint lineto
  xcurr yleft trpoint yextra sub lineto 
  linewidth setlinewidth stroke
} def

% stroke an x histogram: - xhisto -
/xhisto
{ xleft yprev trpoint exch xextra sub exch moveto
  xcurr yprev trpoint lineto
  xcurr ycurr trpoint lineto
  xleft ycurr trpoint exch xextra sub exch lineto 
  linewidth setlinewidth stroke
} def

% stroke a surface y histogram: - surfaceyhisto -
/surfaceyhisto
{ firstpair
  { xprev yleft trpoint yextra sub moveto
    xprev yprev trpoint lineto
  }
  { xprev yprev trpoint moveto
  } ifelse
  xcurr yprev trpoint lineto
  lastpair
  { xcurr yleft trpoint yextra sub lineto 
  }
  { xcurr ycurr trpoint lineto
  } ifelse
  linewidth setlinewidth stroke
} def

% stroke a surface x histogram: - surfacexhisto -
/surfacexhisto
{ firstpair
  { xleft yprev trpoint exch xextra sub exch moveto
  }
  { xprev yprev trpoint moveto
  } ifelse
  xcurr yprev trpoint lineto
  xcurr ycurr trpoint lineto
  lastpair
  { xleft ycurr trpoint exch xextra sub exch lineto 
  } if
  linewidth setlinewidth stroke
} def

% % pre-texture versions
% % stroke a filled y histogram: - filledyhisto -
% /filledyhisto
% {
%   linewidth setlinewidth
%   xprev yleft trpoint exch currentlinewidth 2 div add exch yextra sub moveto
%   xprev yprev trpoint exch currentlinewidth 2 div add exch lineto
%   xcurr yprev trpoint exch currentlinewidth 2 div sub exch lineto
%   xcurr yleft trpoint exch currentlinewidth 2 div sub exch yextra sub lineto 
%   closepath fill
% } def

% % stroke a filled x histogram: - filledxhisto -
% /filledxhisto
% {
%   linewidth setlinewidth
%   xleft yprev trpoint currentlinewidth 2 div add exch xextra sub exch moveto
%   xcurr yprev trpoint currentlinewidth 2 div add lineto
%   xcurr ycurr trpoint currentlinewidth 2 div sub lineto
%   xleft ycurr trpoint currentlinewidth 2 div sub exch xextra sub exch lineto 
%   closepath fill
% } def

% stroke a filled y histogram: - filledyhisto -
/filledyhisto
{
  linewidth setlinewidth
  xprev yleft trpoint exch currentlinewidth 1.3 mul add exch yextra sub moveto
  xprev yprev trpoint exch currentlinewidth 1.3 mul add exch lineto
  xcurr yprev trpoint exch currentlinewidth 1.3 mul sub exch lineto
  xcurr yleft trpoint exch currentlinewidth 1.3 mul sub exch yextra sub lineto 
  gsave texture fill grestore stroke
} def

% stroke a filled x histogram: - filledxhisto -
/filledxhisto
{
  linewidth setlinewidth
  xleft yprev trpoint currentlinewidth 1.3 mul add exch xextra sub exch moveto
  xcurr yprev trpoint currentlinewidth 1.3 mul add lineto
  xcurr ycurr trpoint currentlinewidth 1.3 mul sub lineto
  xleft ycurr trpoint currentlinewidth 1.3 mul sub exch xextra sub exch lineto 
  gsave texture fill grestore stroke
} def


% docross: show a cross with a given symbolsize and symbollinewidth
% <x> <y> <symbolsize> <symbollinewidth> docross -
/docross
{
  setlinewidth
  /ss exch def
  newpath
  moveto
  ss neg ss neg rmoveto
  ss 2 mul ss 2 mul rlineto
  0 ss -2 mul rmoveto
  ss -2 mul ss 2 mul rlineto
  [] 0 setdash 0 setlinecap stroke
} def

% cross: show a cross
% - cross -
/cross { xcurr ycurr trpoint symbolsize symbollinewidth docross } def

% doplus: show a plus with a given symbolsize and symbollinewidth
% <x> <y> <symbolsize> <symbollinewidth> doplus -
/doplus
{
  setlinewidth
  /ss exch def
  newpath
  moveto
  ss neg 0 rmoveto
  ss 2 mul 0 rlineto
  ss neg ss neg rmoveto
  0 ss 2 mul rlineto
  [] 0 setdash 0 setlinecap stroke
} def

% plus: show a plus
% - plus -
/plus { xcurr ycurr trpoint symbolsize symbollinewidth doplus } def

% dosquare: show an open square with a given symbolsize and symbollinewidth
% NB symbolsize is reduced by half the line width to get size exactly right
% <x> <y> <symbolsize> <symbollinewidth> dosquare -
/dosquare
{
  dup setlinewidth
  0.5 mul sub 0 max /ss exch def
  newpath
  moveto
  ss neg ss neg rmoveto
  ss 2 mul 0 rlineto
  0 ss 2 mul rlineto
  ss -2 mul 0 rlineto
  closepath [] 0 setdash stroke
} def

% square: show an open square
% - square -
/square { xcurr ycurr trpoint symbolsize symbollinewidth dosquare } def

% dofilledsquare: show filled square with given symbolsize and symbollinewidth
% NB symbollinewidth is not used
% <x> <y> <symbolsize> <symbollinewidth> dofilledsquare -
/dofilledsquare
{
  pop /ss exch def
  newpath
  moveto
  ss neg ss neg rmoveto
  ss 2 mul 0 rlineto
  0 ss 2 mul rlineto
  ss -2 mul 0 rlineto
  closepath fill
} def

% filledsquare: show a filled square
% - filledsquare -
/filledsquare { xcurr ycurr trpoint symbolsize symbollinewidth dofilledsquare } def

% dodiamond: show an open diamond with a given symbolsize and symbollinewidth
% NB symbolsize is reduced by half the line width to get size exactly right
% <x> <y> <symbolsize> <symbollinewidth> dodiamond -
/dodiamond
{
  dup setlinewidth
  0.5 mul sub 0 max /ss exch def
  newpath
  moveto
  ss neg 0 rmoveto
  ss ss neg rlineto
  ss ss rlineto
  ss neg ss rlineto
  closepath [] 0 setdash stroke
} def

% diamond: show an open diamond
% - diamond -
/diamond { xcurr ycurr trpoint symbolsize symbollinewidth dodiamond } def

% dofilleddiamond: show filled diamond with given symbolsize and symbollinewidth
% NB symbollinewidth is not used
% <x> <y> <symbolsize> <symbollinewidth> dofilleddiamond -
/dofilleddiamond
{
  pop /ss exch def
  newpath
  moveto
  ss neg 0 rmoveto
  ss ss neg rlineto
  ss ss rlineto
  ss neg ss rlineto
  closepath fill
} def

% filleddiamond: show a filled diamond
% - filleddiamond -
/filleddiamond { xcurr ycurr trpoint symbolsize symbollinewidth dofilleddiamond } def

% docircle: show an open circle with a given symbolsize and symbollinewidth
% NB symbolsize is reduced by half the line width to get size exactly right
% <x> <y> <symbolsize> <symbollinewidth> docircle -
/docircle
{
  dup setlinewidth
  0.5 mul sub 0 max /ss exch def
  newpath
  ss 0 360 arc [] 0 setdash stroke
} def

% circle: show an open circle
% - circle -
/circle { xcurr ycurr trpoint symbolsize symbollinewidth docircle } def

% dofilledcircle: show filled circle with given symbolsize and symbollinewidth
% NB symbollinewidth is not used
% <x> <y> <symbolsize> <symbollinewidth> dofilledcircle -
/dofilledcircle
{
  pop /ss exch def
  newpath
  ss 0 360 arc fill
} def

% filledcircle: show a filled circle
% - filledcircle -
/filledcircle { xcurr ycurr trpoint symbolsize symbollinewidth dofilledcircle } def

% dotriangle: show an open triangle with a given symbolsize and symbollinewidth
% NB symbolsize is reduced by half the line width to get size exactly right
% <x> <y> <symbolsize> <symbollinewidth> dotriangle -
/dotriangle
{
  dup setlinewidth
  0.5 mul sub 0 max /ss exch def
  newpath
  moveto
  0 ss 1.5 mul rmoveto
  ss neg ss -2.5 mul rlineto
  ss 2 mul 0 rlineto
  closepath [] 0 setdash stroke
} def

% triangle: show an open triangle
% - triangle -
/triangle { xcurr ycurr trpoint symbolsize symbollinewidth dotriangle } def

% dofilledtriangle: show filled triangle with symbolsize and symbollinewidth
% NB symbollinewidth is not used
% <x> <y> <symbolsize> <symbollinewidth> dofilledtriangle -
/dofilledtriangle
{
  pop /ss exch def
  newpath
  moveto
  0 ss 1.5 mul rmoveto
  ss neg ss -2.5 mul rlineto
  ss 2 mul 0 rlineto
  closepath fill
} def

% filledtriangle: show a filled triangle
% - filledtriangle -
/filledtriangle { symbolsize symbollinewidth dofilledtriangle } def


%plog: like log only with a base, and protected from failing if <= 0
% base x plog res
/plog { dup 0 le { pop pop 0 } { ln exch ln div } ifelse } def

% xtr: transform one x value logarithmically if xlog > 1
% <num> xtr <num>
/xtr
{ xlog 1 gt
  { xlog exch plog
  } if
} def

% ytr: transform one y value logarithmically if ylog > 1
% <num> ytr <num>
/ytr
{ ylog 1 gt
  { ylog exch plog
  } if
} def


% trpoint: transform (x, y) in graph space into (x', y') in print space
% x y trpoint x' y'
/trpoint
{ exch xtr xdecr { trxmax exch sub } { trxmin sub } ifelse
  trxmax trxmin sub div xwidth mul xextra add

  exch ytr ydecr { trymax exch sub } { trymin sub } ifelse
  trymax trymin sub div ywidth mul yextra add
} def


% yonly: interpolate x values 1, 2, ... into data
% [ data ] yonly [ newdata ]
/yonly
{ dup /tmp exch def
  length [ exch 1 exch 1 exch
  { dup tmp exch 1 sub get
  } for
  ]
} def

% xonly: interpolate y values 1, 2, ... into data
% [ data ] yonly [ newdata ]
/xonly
{ dup /tmp exch def
  length [ exch 1 exch 1 exch
  { dup tmp exch 1 sub get exch
  } for
  ]
} def

% xandy: no interpolation of x or y values
% [ data ] xandy [ data ]
/xandy {} def

% swapxandy: swap x and y values
% [ data ] swapxandy [ data ]
/swapxandy
{ dup /tmp exch def
  length [ exch 2 exch 2 exch
  { dup tmp exch 1 sub get exch 2 sub tmp exch get
  } for
  ]
} def


% expstringwidth: calculate width of string containing optional exponent
% <string> expstringwidth <width>
/expstringwidth
{ (^) search
  { exch pop stringwidth pop exch stringwidth pop 0.7 mul add }
  { stringwidth pop }
  ifelse
} def

% expstringshow: show string containing optional exponent
% <string> expstringshow -
/expstringshow
{ (^) search
  { exch pop show 0 0.5 ft rmoveto
    gsave currentfont 0.7 scalefont setfont show grestore
  }
  { show
  }
  ifelse
} def

% concatenate two strings: <string> <string> strconcat <string>
/strconcat
{ 2 copy length exch length add string
  dup 0 4 index putinterval
  dup 3 index length 3 index putinterval
  3 1 roll pop pop
} def

% lgen: generate one label automatically
% num lgen num string
/lgen { dup 20 string cvs } def

% loglgen: generate one logarithmic label (with exponent)
% <base> <exponent> loglgen <string>
/loglgen
{ 20 string cvs exch 20 string cvs
  (^) strconcat exch strconcat
} def


% printxtick: print one x tick
% xpos printxtick -
/printxtick
{ newpath
  yleft trpoint moveto 0 yextra neg rmoveto
  0 xticklength neg rlineto [] 0 setdash stroke
} def

% printxgrid: print one x grid line
% xpos printxgrid -
/printxgrid
{ dup newpath
  yleft trpoint moveto 0 yextra neg rmoveto
  yright trpoint lineto 0 yextra rlineto [] 0 setdash stroke
} def

% printxlabel: print one x label
% (xlabel) xpos printxlabel -
/printxlabel
{ yleft trpoint moveto 0 yextra neg rmoveto
  0 xticklength neg rmoveto 0 0.9 ft neg rmoveto
  xlog 1 gt { 0 0.3 ft neg rmoveto } if
  dup expstringwidth -2 div 0 rmoveto expstringshow
} def

% printytick: print one y tick
% ypos printytick -
/printytick
{ newpath
  xleft exch trpoint moveto xextra neg 0 rmoveto
  yticklength neg 0 rlineto [] 0 setdash stroke
} def

% printygrid: print one y grid line
% ypos printygrid -
/printygrid
{ dup newpath
  xleft exch trpoint moveto xextra neg 0 rmoveto
  xright exch trpoint lineto xextra 0 rlineto [] 0 setdash stroke
} def

% printylabel: print one y label
% (ylabel) ypos printylabel -
/printylabel
{ xleft exch trpoint moveto xextra neg 0 rmoveto
  yticklength neg 0 rmoveto -0.3 ft -0.3 ft rmoveto
  dup expstringwidth neg 0 rmoveto expstringshow
} def

% printrtick: print one r tick
% ypos printrtick -
/printrtick
{ newpath
  xright exch trpoint moveto xextra 0 rmoveto
  rticklength 0 rlineto [] 0 setdash stroke
} def

% printrlabel: print one r label
% (rlabel) ypos printrlabel -
/printrlabel
{ xright exch trpoint moveto xextra 0 rmoveto
  rticklength 0 rmoveto  0.3 ft -0.3 ft rmoveto
  expstringshow
} def

% printticks: print ticks and labels
% /tickproc /labelproc [ tickandlabeldata ] min printticks -
/printticks
{ /prev exch def
  { dup type dup dup /integertype eq exch /realtype eq or
    { pop dup /prev exch def 2 index cvx exec
    }
    { /stringtype eq
      { prev 2 index cvx exec
      }
      { pop
      } ifelse
    } ifelse
  } forall
  pop pop
} def


% printxaxistick: print one x axis tick
% xpos printxaxistick -
/printxaxistick
{ newpath
  yaxis trpoint moveto 0 xticklength -2 div rmoveto
  0 xticklength rlineto [] 0 setdash stroke
} def

% printxaxislabel: print one x axis label
% (xlabel) xpos printxaxislabel -
/printxaxislabel
{ yaxis trpoint moveto
  0 xticklength -2 div rmoveto 0 0.9 ft neg rmoveto
  xlog 1 gt { 0 0.3 ft neg rmoveto } if
  dup expstringwidth -2 div 0 rmoveto expstringshow
} def

% printyaxistick: print one y axis tick
% ypos printyaxistick -
/printyaxistick
{ newpath
  xaxis exch trpoint moveto
  yticklength -2 div 0 rmoveto
  yticklength 0 rlineto [] 0 setdash stroke
} def

% printyaxislabel: print one y axis label
% (ylabel) ypos printyaxislabel -
/printyaxislabel
{ xaxis exch trpoint moveto
  yticklength -2 div 0 rmoveto -0.3 ft -0.3 ft rmoveto
  dup expstringwidth neg 0 rmoveto expstringshow
} def


% <val> minmax -
% perform minv := min(minv, val); maxv := max(maxv, val)
% allowing for the possibility of minv, maxv, val being false (undefined)
/minmax
{ dup false eq
  { pop }
  { minv false eq
    { dup /minv exch def /maxv exch def }
    { dup minv lt
      { /minv exch def }
      { dup maxv gt
	{ /maxv exch def }
	{ pop }
	ifelse
      } ifelse
    } ifelse
  } ifelse
} def

% <ticks> ticksundef <ticks> <bool>
% returns true iff the ticks array is undefined (one false entry)
/ticksundef
{ dup length 1 eq
  { dup 0 get false eq
  }
  { false }
  ifelse
} def

% <number> integral <boolean>
% true if the number has an integral value
/integral { dup round eq } def

% ticksep ticks xory alldata minval maxval axis base ticksandlimits ticks min max base
% ticksandlimits: sort out value of x or y ticks and limits and log base
/ticksandlimits
{ /base exch def
  /minv false def
  /maxv false def

  % min and max of user-supplied minval, maxval, and axis
  minmax minmax minmax

  % min and max of data points
  { 0 get dup dup length 1 sub 3 index exch 2 exch
    { get minmax dup
    } for pop pop
  } forall
  pop dup

  % min and max of tick values
  { dup type /stringtype eq
    { pop } { minmax } ifelse
  } forall

  % fix minv and maxv if undefined (false) or equal
  minv false eq
  { /minv -1 def /maxv 1 def }
  { minv maxv eq
    { minv 0 lt
      { /minv 2 minv mul def /maxv 0 def
      }
      { minv 0 eq
	{ /minv -1 def /maxv 1 def
	}
	{ /minv 0 def /maxv 2 maxv mul def
	} ifelse
      } ifelse
    } if
  } ifelse

  % invent ticks if undefined
  ticksundef
  { pop /ticksep exch def

    % if base is reasonable and minv is positive, logarithmic ticks
    base 1 gt minv 0 gt and
    {
      % get integral log of minv and maxv
      /logminv base minv plog floor   cvi def
      /logmaxv base maxv plog ceiling cvi def

      % if minv close to base, make it 1; reset minv and maxv
      logminv 1 eq logmaxv 4 ge and { /logminv 0 def } if
      /minv base logminv exp def
      /maxv base logmaxv exp def

      % ticks := [ base**logminv, ... , base**logmaxv ]
      [ logminv 1 logmaxv
	{ dup base exch exp
	  exch base exch loglgen
	} for
      ]
    }
    { % non-logarithmic ticks
      {
        % fix tick separation if undefined (0) or too small
	/base 0 def
        /delta maxv minv sub def
        ticksep delta 30 div le
        { /ticksep 10 delta log 1 sub ceiling exp def
	  ticksep delta 2 div ge
	  { /ticksep ticksep 2 div def }
	  { ticksep delta 5 div lt
	    { /ticksep 2 ticksep mul def
	    } if
	  } ifelse
        } if

        % adjust minv and maxv to be multiples of ticksep
        /minv minv ticksep div floor   ticksep mul def
        /maxv maxv ticksep div ceiling ticksep mul def
        /delta maxv minv sub def

        % if minv or maxv near zero, move to zero and redo
        minv ticksep eq
        { /minv 0 def }
        { maxv ticksep neg eq { /maxv 0 def } { exit } ifelse
        } ifelse
      } loop

      % if minv, maxv, and ticksep are all integral, set "makeint" to true
      /makeint minv integral maxv integral ticksep integral and and def

      % ticks := [ minv, minv+ticksep, ... , maxv ]
      [ 0 1 delta ticksep div round
	{ ticksep mul minv add makeint { cvi } if lgen }
	for
      ]
    } ifelse
  }
  { exch pop
  } ifelse
  minv maxv base
} def

% xset: set up all data for x axis, including limits and ticks
% xticksep xticks 0 alldata xmin xmax xlog xextra xdecr xaxis xticklength xset -
/xset
{ /xticklength exch def
  /xaxis exch def
  /xdecr exch def
  /xextra exch def
  xaxis exch ticksandlimits
  /xlog exch def /xmax exch def /xmin exch def /xticks exch def
  /xleft xdecr { xmax } { xmin } ifelse def
  /xright xdecr { xmin } { xmax } ifelse def
  /xwidth xsize xextra 2 mul sub def
  /trxmin xmin xtr def /trxmax xmax xtr def
} def

% yset: set up all data for y axis, including limits and yticks
% yticksep yticks 0 alldata ymin ymax ylog yextra ydecr yaxis yticklength yset -
/yset
{ /yticklength exch def
  /yaxis exch def
  /ydecr exch def
  /yextra exch def
  yaxis exch ticksandlimits
  /ylog exch def /ymax exch def /ymin exch def /yticks exch def
  /yleft ydecr { ymax } { ymin } ifelse def
  /yright ydecr { ymin } { ymax } ifelse def
  /ywidth ysize yextra 2 mul sub def
  /trymin ymin ytr def /trymax ymax ytr def
} def

% rset: set up all data for y axis (again), but including limits and rticks
% rticksep rticks 0 alldata ymin ymax ylog yextra ydecr yaxis rticklength rset -
/rset
{ /rticklength exch def
  /yaxis exch def
  /ydecr exch def
  /yextra exch def
  yaxis exch ticksandlimits
  /ylog exch def /ymax exch def /ymin exch def /rticks exch def
  /yleft ydecr { ymax } { ymin } ifelse def
  /yright ydecr { ymin } { ymax } ifelse def
  /ywidth ysize yextra 2 mul sub def
  /trymin ymin ytr def /trymax ymax ytr def
} def

% norset: set up data for no rticks
% - norset -
/norset
{ /rticklength 0 def
  /rticks [] def
} def

% framestyle: print a frame around the graph
/framestyle
{ 0 0 moveto xsize 0 lineto xsize ysize lineto
  0 ysize lineto closepath stroke
  /printxtick /printxlabel xticks xleft printticks
  /printytick /printylabel yticks ymin printticks
  /printrtick /printrlabel rticks ymin printticks
} def

% gridstyle: print a frame around the graph, plus a grid
/gridstyle
{ 0 0 moveto xsize 0 lineto xsize ysize lineto
  0 ysize lineto closepath stroke
  /printxgrid /printxlabel xticks xleft printticks
  /printygrid /printylabel yticks ymin printticks
  /printrtick /printrlabel rticks ymin printticks
} def

% nonestyle: print nothing around the graph
/nonestyle
{
} def

% axesstyle: print axes for the graph (unless axis values missing)
/axesstyle
{
  xaxis false eq yaxis false eq or
  { framestyle }
  { xaxis yaxis trpoint dup 0 exch moveto xsize exch lineto
    dup 0 moveto ysize lineto stroke
    /printxaxistick /printxaxislabel xticks xleft printticks
    /printyaxistick /printyaxislabel yticks ymin printticks
  } ifelse
} def

% rundata: run all data sets
/rundata
{ alldata
  { gsave
    dup dup dup dup dup
    5 get /texture exch def
    4 get /dopaint exch def
    3 get /initrun exch def
    2 get /pairs exch def
    1 get /points exch def
    0 get /data exch def
    dopaint
    { data length 4 ge
      {
	gsave
	initrun
	newpath
	data 0 get ymin trpoint yextra sub moveto
	0 2 data length 2 sub
	{ dup 1 add
	  data exch get /ycurr exch def
	  data exch get /xcurr exch def
	  xcurr ycurr trpoint lineto
	} for
	data dup length 2 sub get ymin trpoint yextra sub lineto
	closepath texture fill
	grestore
      } if
    } if
    initrun
    data length 2 ge
    {
      /xcurr data 0 get def
      /ycurr data 1 get def
      points
      data length 4 ge
      { 2 2 data length 2 sub
	{ /xprev xcurr def
          /yprev ycurr def
	  dup dup 2 eq /firstpair exch def
	  data length 2 sub eq /lastpair exch def
	  dup 1 add
          data exch get /ycurr exch def
          data exch get /xcurr exch def
	  pairs
	  points
	} for
      } if
    } if
    grestore
  } forall
} def

end
%%EndResource