File: tick.c

package info (click to toggle)
yorick 1.5.08-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 7,508 kB
  • ctags: 7,937
  • sloc: ansic: 75,604; cpp: 1,282; lisp: 1,217; sh: 1,026; makefile: 616; fortran: 19
file content (1304 lines) | stat: -rw-r--r-- 40,909 bytes parent folder | download
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
/*
 * TICK.C
 *
 * $Id: tick.c,v 1.1 1993/08/27 17:08:43 munro Exp $
 *
 * Implement ticks and labels for GIST coordinate systems
 *
 */
/*    Copyright (c) 1994.  The Regents of the University of California.
                    All rights reserved.  */

/* NOTE-- probably still a few bugs, especially with log scales.
   I wish the log scale coding were cleaner.  The only bug I've
   noticed is fairly minor:  With limits 45 to 280 (e.g.) on
   a log scale, the (unlabeled) tick at 45 is missing.  Since
   this is the bottommost tick, the scale still doesn't look
   bad.
 */

#include "gist.h"

/* Use sprintf function heavily */
#include <stdio.h>

/* The nice unit routine defined here is also used in draw.c */
extern GpReal GpNiceUnit(GpReal finest, int *base, int *power);

#define TICK_ANY (TICK_L | TICK_U | TICK_C)
#define LABEL_ANY (LABEL_L | LABEL_U)

/* WC->NDC mapping stored by GaTicks, used by Draw?Ticks
   and Draw?Labels routines */
static GpReal scalx, offx, scaly, offy;

/* The main output from FindTicks is a cumulative count of the
   number of ticks at each level, nChangeLevel[0:nLevel]
   (nLevel<TICK_LEVELS), and the floating point tick values
   themselves, ticks[0:nTotal-1] */
static long nTotal;
static int nLevel;
static int nChangeLevel[TICK_LEVELS];
static GpReal *ticks;

/* Scratch space maintained by GaGetScratchP */
extern int GaGetScratchP(long n);
extern GpReal *gaxScratch, *gayScratch;

/* Additional stuff required for sub-decade tick labels */
static GpReal subTick0[3], subUnit[3];
static int subPower[3], nSubtotal[3];
static GpReal minSubdecade;

/* FindTicks also assures that there is enough working space for
   the scratch arrays (px,py), (qx,qy) used by Draw?Ticks */
GpReal *px, *py, *qx, *qy;

static GaAltTicks *altticks= 0;
static GaAltLabel *altlabel= 0;

/* use fancy exponents if escapes recognized */
extern int gtDoEscapes;

/* ------------------------------------------------------------------------ */

extern char *strcpy(char *, const char *);

extern double floor(double);
extern double ceil(double);
extern double fabs(double);
extern double log10(double);

#ifndef NO_EXP10
  extern double exp10(double);
#else
# define exp10(x) pow(10.,x)
  extern double pow(double,double);
#endif

#define LOG2 0.301029996
#define LOG5 0.698970004
#define LOG10o9 0.0457574905
#define LOG6o5 0.0791812460
#define LOG5o4 0.0969100130

static void EvenlySpace(GpReal start, GpReal unit, GpReal stop);
static GpReal Subdivide(GpReal unit, int nDivs, GpReal lo, GpReal hi,
			GpReal *start);
static void FirstSubDecade(GpReal *unit, GpReal lo, GpReal hi,
			   GpReal zSmallest);
static int LogDivide(GpReal *unit, int *base, GpReal *fine,
		     GpReal lo, GpReal hi, GpReal zSmallest);
static void ScopeOutTicks(GpReal *loin, GpReal *hiin,
			  GaAxisStyle *style, int isLog,
			  int *useLog, GpReal *nMajor, GpReal *nMinor,
			  GpReal *jUnit, int *jBase, int *jPower,
			  GpReal *itick0);
static int FindOmitted(GpReal lo, GpReal hi);
static GpReal FindOrigin(GpReal lo, GpReal hi, GaAxisStyle *style, int isLog);
static void FindTicks(GpReal lo, GpReal hi, GaAxisStyle *style, int isLog);
static void TickEndpoints(GpReal place, GpReal len, int lo, int hi,
			  GpReal *p0, GpReal *p1);
static void DrawXTicks(GpReal place, GpReal *lens, int inout, int upper,
		       GpLineAttribs *attribs);
static void DrawYTicks(GpReal place, GpReal *lens, int inout, int upper,
		       GpLineAttribs *attribs);
static void DrawOrigin(GpReal x0, GpReal x1, GpReal y0, GpReal y1,
		       GpLineAttribs *attribs);
static void InitLabels(int nDigits, char exponent[]);
static void NextLabel(char label[]);
static void NiceLogLabel(char *label, char *format, GpReal value, GpReal tick);
static void GtText(GpReal x0, GpReal y0, const char *text);
static int DrawXLabels(int isLog, GpReal place, int upper, int nDigits,
		       GpTextAttribs *attribs);
static int DrawYLabels(int isLog, GpReal place, int upper, int nDigits,
		       GpTextAttribs *attribs);
static void DrawOverflow(GpReal x, GpReal y);

/* ------------------------------------------------------------------------ */

GpReal GpNiceUnit(GpReal finest, int *base, int *power)
{
  int b, p;
  GpReal unit;
  if (finest==0.0) finest= 1.0e-6;
  p= (int)floor(log10(fabs(finest)));
  unit= exp10((double)p);
  finest= finest/unit;
  if (finest>5.0) {
    p++;
    unit*= 10.0;
    b= 1;
  } else if (finest>2.0) b= 5;
  else if (finest>1.0) b= 2;
  else b= 1;
  *base= b;
  *power= p;
  return b*unit;
}

static void EvenlySpace(GpReal start, GpReal unit, GpReal stop)
{
  if (start<=stop) {
    GpReal v, vo;  /* avoid infinite loop if stop-start and unit  << start */
    ticks[nTotal++]= vo= v= start;
    for (v+=unit ; v<=stop && v>vo ; v+=unit) ticks[nTotal++]= vo= v;
  }
}

static GpReal Subdivide(GpReal unit, int nDivs, GpReal lo, GpReal hi,
			GpReal *start)
{
  GpReal newUnit= unit/(GpReal)nDivs;
  GpReal value= *start;
  GpReal test= lo+unit;
  int j;
  for (j=1 ; j<nDivs ; j++) {
    value+= newUnit;
    if (value>=test) {
      value-= unit;
      *start= value;
    }
    EvenlySpace(value, unit, hi);
  }
  return newUnit;
}

static GpReal zi[3]= {1.0, 2.0, 5.0};
static GpReal zf[3]= {2.0, 5.0, 10.0};
static GpReal zlogi[3]= {0.0, LOG2, LOG5};
static GpReal zlogf[3]= {LOG2, LOG5, 1.0};

#define EPS_LOG 0.00001

static GpReal zfSigh[3]= {2.0+EPS_LOG, 5.0+EPS_LOG, 10.0-EPS_LOG};

static void FirstSubDecade(GpReal *unit, GpReal lo, GpReal hi,
			   GpReal zSmallest)
{
  int ii, iiset= -1;
  GpReal z, zlog, oldUnit;
  GpReal decade= ceil(lo);
  GpReal zDecTest= lo+1.0;
  GpReal interval0or1;

  if (hi <= decade) decade-= 1.0;

  /* Outer loop is on interval (1,2], (2,5], or (5,10). */
  for (ii=0 ; ii<3 ; ii++) {
    oldUnit= unit[ii];
    if (ii>0) nSubtotal[ii-1]= nTotal;
    interval0or1= ii<2? EPS_LOG : 0.0;

  retry:
    if (zlogf[ii]+interval0or1 < lo-decade) continue;
    zlog= zlogi[ii]+decade;
    if (zlog+interval0or1 < lo) {
      /* Some ticks are present in no decades at all (this can only happen
	 if hi-lo<1.0).  Try to avoid computing log10s by jumping directly
	 to 1st tick.  */
      z= oldUnit*ceil(zSmallest/oldUnit);
    } else {
      if (zlog > hi) {
	/* This can only happen if hi-lo < 1.0. */
	if (decade < lo) goto done;
	decade-= 1.0;
	goto retry;
      }
      /* All ticks are present, but must deal with case that oldUnit
	 is larger than interval without duplicating ticks... */
      z= oldUnit*ceil(zi[ii]/oldUnit);
    }

    /* Lower boundary of all three intervals must be skipped */
    if (ii>iiset) {
      /* never want to do this after retry if some ticks placed */
      if (z<=zi[ii]) z+= oldUnit;
      subTick0[ii]= z;
      subUnit[ii]= oldUnit;
    }

    /* Next loop is on unit divisions. */
    for ( ; z<zfSigh[ii]+interval0or1 ; z+=oldUnit) {
      zlog= log10(z)+decade;
      if (zlog >= zDecTest) { /* zDecTest= lo+1.0 */
	zlog-= 1.0;
	decade-= 1.0;
      }
      if (zlog > hi) {
	/* This can only happen if hi-lo < 1.0. */
	if (decade < lo) goto done;
	decade-= 1.0;
	goto retry;
      }
      /* Inner loop is on decades. */
      EvenlySpace(zlog, 1.0, hi);
      iiset= ii;
      if (zlog < minSubdecade) minSubdecade= zlog;
    }
  }
  ii= 2;
 done:
  for ( ; ii<3 ; ii++) nSubtotal[ii]= nTotal;
  minSubdecade= floor(minSubdecade);
}

static int LogDivide(GpReal *unit, int *base, GpReal *fine,
		     GpReal lo, GpReal hi, GpReal zSmallest)
{
  int i, ii, nDivs, newBase;
  GpReal z, zu, zlog, ztop, newUnit, oldUnit;
  GpReal du, decade= ceil(lo);
  GpReal zDecTest= lo+1.0;
  GpReal zTest;
  int anyDivisions= 0;

  if (hi <= decade) decade-= 1.0;

  /* Outer loop is on interval (1,2], (2,5], or (5,10). */
  for (ii=0 ; ii<3 ; ii++) {
    oldUnit= unit[ii];
    newBase= base[ii];
    if (newBase==1) {
      /* Use halves if it can be subdivided again, else try fifths. */
      if (oldUnit>=10.0*fine[ii] || oldUnit<5.0*fine[ii]) nDivs= 2;
      else nDivs= 5;
      newBase= 7-nDivs;
    } else {
      nDivs= newBase;
      newBase= 1;
    }
    newUnit= oldUnit/nDivs;
    if (newUnit<fine[ii]) continue;
    unit[ii]= newUnit;
    base[ii]= newBase;
    anyDivisions= 1;

  retry:
    if (zlogf[ii]+decade < lo) continue;
    zlog= zlogi[ii]+decade;
    if (zlog < lo) {
      /* Some ticks are present in no decades at all (this can only happen
	 if hi-lo<1.0).  Try to avoid computing log10s by jumping directly
	 to 1st tick.  */
      z= oldUnit*ceil(zSmallest/oldUnit);
      zTest= zSmallest+oldUnit;
    } else {
      if (zlog > hi) {
	if (decade < lo) goto done;
	decade-= 1.0;
	goto retry;
      }
      /* All ticks are present, but must deal with case that oldUnit
	 is larger than interval without duplicating ticks... */
      /* z= oldUnit*ceil(zi[ii]/oldUnit); */
      z= zi[ii];
      zTest= zf[ii];
    }
    ztop= zf[ii]-EPS_LOG;  /* normally, don't tick top of interval */

    /* Obnoxius special cases... */
    if (newBase==1 && newUnit>0.5) { /* i.e.- newUnit==1.0 */
      if (ii==0) continue;
      if (ii==1) {
	if (oldUnit<3.0) ztop= 5.5;     /* i.e.- oldUnit==2.0, do tick at 5 */
      } else {
	if (oldUnit<3.0 && z<6.0) z= 6.0; /* i.e.- oldUnit==2.0, begin at 7 */
      }
    }

    /* Next loop is on subdivisions. */
    du= decade;
    for (i=1 ; i<nDivs ; i++) {
      z+= newUnit;
      if (z > zTest) { /* this can only happen on zSmallest branch above */
	z-= oldUnit;
	if (z <= zi[ii]+EPS_LOG) break; /* allows for no ticks */
      }
      decade= du;
      /* Next loop is on unit divisions. */
      for (zu=z ; zu<ztop ; zu+=oldUnit) {
	zlog= log10(zu)+decade;
	if (zlog >= zDecTest) { /* zDecTest= lo+1.0 */
	  zlog-= 1.0;
	  decade-= 1.0;
	}
	if (zlog > hi) {
	  /* This can only happen if hi-lo < 1.0. */
	  if (zu!=z) break;  /* log(z) may still be less than hi */
	  if (decade < lo) goto done;
	  decade-= 1.0;
	  goto retry;
	}
	/* Inner loop is on decades. */
	EvenlySpace(zlog, 1.0, hi);
      }
    }

  }
 done:

  return anyDivisions;
}

/* ------------------------------------------------------------------------ */

/* Things set by ScopeOutTicks() utility routine */
static int useLog;        /* set if log ticks must be generated */
static GpReal nMajor;     /* extracted from GaAxis style, adjusted if... */
static GpReal nMinor;     /* ...this is log scale reverted to linear */
static GpReal jUnit;      /* nice number just above delta/nMajor */
static int jBase, jPower; /* jUnit= jBase*10^jPower, jBase= 1, 2, or 5 */
static GpReal itick0;     /* integer "number of major unit" for 1st tick */

/* After FindTicks, first tick is at tick0, and first major tick
   has index itick0 (tick is at itick0*jUnit) */
static GpReal tick0;
static int subDecadeTicks; /* set if there are sub-decade major ticks */

static void ScopeOutTicks(GpReal *loin, GpReal *hiin,
			  GaAxisStyle *style, int isLog,
			  int *useLog, GpReal *nMajor, GpReal *nMinor,
			  GpReal *jUnit, int *jBase, int *jPower,
			  GpReal *itick0)
{
  GpReal lo= *loin;
  GpReal hi= *hiin;
  GpReal finest, delta;

  *nMajor= style->nMajor;
  *nMinor= style->nMinor;
  if (*nMinor<*nMajor) *nMinor= *nMajor;

  if (hi<lo) { GpReal tmp= lo; lo= hi; hi= tmp; }
  delta= hi-lo;
  if (delta==0.0) {
    if (lo>0.0) delta= lo;
    else if (lo<0.0) delta= -lo;
    else delta= 0.01;
  }

  /* Adjust lo and hi slightly to avoid rounding errors that can cause
     ticks and or labels falling exactly at the endpoints of the
     interval to be missed.  This is a little heavy handed, but it
     does the desirable thing in most cases.  */
  lo-= 0.0001*delta;
  hi+= 0.0001*delta;
  delta= hi-lo;

  if (isLog) {
    *useLog= delta > LOG2;
    if (!*useLog) {
      /* Revert to linear scale if full scale less than factor of 2... */
      lo= exp10(lo);
      hi= exp10(hi);
      /* ...but adjust number of divisions so that finest linear division
	 is no finer than finest log division would have been */
      *nMajor= (1.-(lo/hi))/(1.-exp10(-delta/(*nMajor)));
      *nMinor= (1.-(lo/hi))/(1.-exp10(-delta/(*nMinor)));
      delta= hi-lo;
    }
  } else {
    *useLog= 0;
  }

  finest= delta/(*nMajor);
  if (!(*useLog) || finest>1.0) *jUnit= GpNiceUnit(finest, jBase, jPower);
  else { *jUnit= 1.0;   *jBase= 1;   *jPower= 0; }

  *itick0= ceil(lo/(*jUnit));

  *loin= lo;
  *hiin= hi;
}

static int FindOmitted(GpReal lo, GpReal hi)
{
  GpReal z= itick0*jUnit;
  int nTicks= nChangeLevel[0];
  if (nTicks>0) {
    GpReal multiple= 100./(GpReal)jBase; /* multiple of jUnit ends in "00" */
    multiple*= ceil(itick0/multiple);    /* itick0 rounded up to multiple */
    nTicks--;
    if (multiple <= itick0+(GpReal)nTicks) return (int)(multiple-itick0);
    else return 0;
  } else {
    /* This is a log scale with no decades (sigh).
       Know that jUnit=1.0, but hi-lo>LOG2, since it hasn't
       reverted to a linear scale.  Therefore, either 2, 5, or 4
       must be in the interval-- take the first one that is.
       Initially, z=itick0*jUnit is the decade above the interval.  */
    int i;
    if (z+(LOG2-1.0) >= lo) z+= LOG2-1.0;         /* 2 */
    else if (z-LOG2 <= hi) z-= LOG2;              /* 5 */
    else z+= 2.0*LOG2-1.0;                        /* 4 */
    /* Since there are no decade ticks, the ticks are in strictly
       increasing order.  */
    for (i=0 ; i<nChangeLevel[1] && ticks[i]+EPS_LOG<z ; i++);
    return i;
  }
}

static GpReal FindOrigin(GpReal lo, GpReal hi, GaAxisStyle *style, int isLog)
{
  int useLog;
  GpReal nMajor, nMinor, jUnit;
  int jBase, jPower;
  GpReal itick0;

  GpReal z, multiple, origin;
  int nTicks;

  ScopeOutTicks(&lo, &hi, style, isLog, &useLog, &nMajor, &nMinor,
		&jUnit, &jBase, &jPower, &itick0);

  /* Count number of ticks using same algorithm as EvenlySpace */
  origin= z= itick0*jUnit;
  for (nTicks=0 ; z<=hi ; z+=jUnit) nTicks++;

  if (nTicks>0) {
    multiple= 100./(GpReal)jBase; /* multiple of jUnit that ends in "00" */
    multiple*= ceil(itick0/multiple);   /* itick0 rounded up to multiple */
    nTicks--;
    if (multiple <= itick0+(GpReal)nTicks) origin= multiple*jUnit;
    /* else origin remains itick0*jUnit */
  } else {
    /* This is a log scale with no decades (sigh).
       Know that jUnit=1.0, but hi-lo>LOG2, since it hasn't
       reverted to a linear scale.  Therefore, either 2, 5, or 4
       must be in the interval-- take the first one that is.
       Initially, z=itick0*jUnit is the decade above the interval.  */
    if (z+(LOG2-1.0) >= lo) origin= z+(LOG2-1.0);       /* 2 */
    else if (z-LOG2 <= hi) origin= z-LOG2;              /* 5 */
    else origin= z+(2.0*LOG2-1.0);                      /* 4 */
  }

  if (isLog && !useLog) origin= log10(origin);

  return origin;
}

static void FindTicks(GpReal lo, GpReal hi, GaAxisStyle *style, int isLog)
{
  GpReal finest, finest2, finest5, finest10;
  GpReal nUnit = 0;
  int /*nBase,*/ nPower;
  int reqdSpace;

  /* Swap limits if necessary to force lo<hi, and exponentiate if this
       scale will revert from log to linear (set useLog appropriately),
     extract nMajor, nMinor from style,
       possibly adjusting for reverted log scales,
     compute the nice major unit (jUnit, jBase, jPower),
       and itick0, the integer "unit number" of the first major tick  */
  ScopeOutTicks(&lo, &hi, style, isLog, &useLog, &nMajor, &nMinor,
		&jUnit, &jBase, &jPower, &itick0);

  /* Be sure there is enough scratch space to hold the ticks */
  reqdSpace= (int)(useLog? style->logAdjMinor*nMinor : nMinor) + 16;
  if (GaGetScratchP(3*reqdSpace)) {
    nTotal= 0;
    nLevel= 0;
    nChangeLevel[0]= 0;
    return; /* memory manager failure */
  } else {
    px= gaxScratch;
    qx= px+reqdSpace;
    py= gayScratch;
    qy= py+reqdSpace;
    ticks= qx+reqdSpace;
  }

  if (altticks && !isLog) {
    int i;
    for (i=0 ; i<TICK_LEVELS ; i++) nChangeLevel[i]= 0;
    if (!altticks(lo, hi, nMajor, nMinor, ticks, nChangeLevel)) {
      nTotal= nChangeLevel[0];
      for (nLevel=TICK_LEVELS-1 ; nLevel>0 ; nLevel--)
	if ((nTotal= nChangeLevel[nLevel])) break;
      /* attempt to set jUnit, jPower, jBase */
      if (nChangeLevel[0]>0) {
	if (nChangeLevel[0]>1) {
	  GpReal pwr;
	  jUnit= fabs(ticks[1]-ticks[0]);
	  jPower= (int)floor(log10(jUnit)+0.000001);
	  pwr= exp10((double)jPower);
	  for (i=4 ; i ; i--) {
	    jBase= (int)(jUnit/pwr+0.000001);
	    if (fabs(jBase-jUnit/pwr)<0.0001) break;
	    jPower--;
	    pwr/= 10;
	  }
	}
	/* note: itick0*jUnit must equal 1st tick value
	         *and* other ticks must be at itick0+n*jBase */
	itick0= ticks[0]/jUnit;
      }
      tick0= itick0*jUnit;
      goto done;
    }
    altticks= 0;  /* can be used as a flag later if necessary */
  }

  /* Descend hierarchy from major divisions to minor divisions.
     The minor divisions are nUnit= nBase*10^nPower.
     nLevel+1= the number of levels (major is level 0,
				     minor is level nLevel) */
  nPower= jPower;
  nUnit= jUnit;
  finest= (hi-lo)/nMinor;
  finest2= finest*2.0;
  finest5= finest*5.0;
  finest10= finest*10.0;

  tick0= itick0*jUnit;
  nLevel= 0;
  nTotal= 0;
  EvenlySpace(tick0, jUnit, hi);
  nChangeLevel[0]= nTotal;

  if (jBase==2) {
    if (nUnit < finest2) goto done;
    nUnit= Subdivide(nUnit, 2, lo, hi, &tick0);
    nChangeLevel[++nLevel]= nTotal;
  } else if (jBase==5) goto mid;

  for (;;) {  /* Loop to finer and finer decades */
    /* nUnit is 1.0eNN */
    if (nUnit<finest2 || (useLog&&nPower==0) || nLevel>=TICK_LEVELS-1) {
      /*nBase= 1;*/
      break;
    }
    nPower--;

    if (nUnit>=finest5 && nUnit<finest10) {
      /* Subdivide into 2.0eNN if and only if no further subdivisions */
      nUnit= Subdivide(nUnit, 5, lo, hi, &tick0);
      nChangeLevel[++nLevel]= nTotal;
      /*nBase= 2;*/
      break;
    }
    nUnit= Subdivide(nUnit, 2, lo, hi, &tick0);
    nChangeLevel[++nLevel]= nTotal;

  mid:
    /* nUnit is 5.0eNN */
    if (nUnit<finest5 || nLevel>=TICK_LEVELS-1) {
      /*nBase= 5;*/
      break;
    }
    nUnit= Subdivide(nUnit, 5, lo, hi, &tick0);
    nChangeLevel[++nLevel]= nTotal;
  }
 done:

  if (isLog && !useLog) {
    /* must adjust ticks to produce log scale (lo and hi were exp10'd) */
    int i;
    for (i=0 ; i<nTotal ; i++) ticks[i]= log10(ticks[i]);
  }

  /*------------ Do sub-decade log ticks if necessary -------------*/
  subDecadeTicks= 0;
  if (useLog && nUnit<1.1 && nLevel<TICK_LEVELS-1) {
    GpReal delta= hi-lo;
    GpReal logAdjMajor= style->logAdjMajor;
    GpReal logAdjMinor= style->logAdjMinor;
    GpReal fine[3];  /* 0 is interval (1,2], 1 is (2,5], 2 is (5,10) */
    GpReal unit[3];
    int base[3];
    GpReal zSmallest;

    /* Apply subdecade adjustment factors */
    nMajor*= logAdjMajor;
    nMinor*= logAdjMinor;

    /* Compute separate finest divisions for each of the 3 parts of
       a decade-- (1,2], (2,5], and (5,10).  The formulas are the same
       as for the revert-to-linear adjustment if isLog && !useLog. */
    finest= (1.-exp10(-delta/nMinor));
    fine[0]= 2.0*finest;
    fine[1]= 5.0*finest;
    fine[2]= 10.0*finest;

    /* Compute the major units from which to begin subdivision.
       The "natural" progression of subdivision is adjusted to give
       scales which "look logarithmic", i.e.- so that the tick spacing
       at each level varies more than it would if the 3 intervals
       were divided like linear scales. */

    finest= delta/nMajor;
    if (finest>LOG2) {
      finest= delta/nMinor;
      if (finest>LOG2) return;
      subDecadeTicks= 0;  /* no major sub-decade ticks */
    } else {
      subDecadeTicks= 1;  /* set if there are sub-decade major ticks */
    }
    zSmallest= hi-lo<1.0? exp10(lo-floor(lo)) : 1.0;

    if (subDecadeTicks && (delta<LOG5 || finest<=LOG10o9)) {
      /* Choose linear units best suited to each of the 3 intervals.
	 These scales tend to "look linear".  If there are to be no
	 major ticks, never need to choose this. */
      finest= (1.-exp10(-finest));  /* see note above */
      unit[0]= GpNiceUnit(2.0*finest, base+0, subPower+0);
      unit[1]= GpNiceUnit(5.0*finest, base+1, subPower+1);
      unit[2]= GpNiceUnit(10.0*finest, base+2, subPower+2);
      if (finest>LOG6o5 && base[1]==1) {
	/* log10(5/4) >= finest > log10(6/5) needs correction. */
	base[1]= 2;
	unit[1]= 2.0;
      }
    } else if (finest<=LOG5o4) {
      /* Subdecade ticks at 2, 4, 6, and 8 only. */
      unit[0]= 2; /* skips next level (subdivision into 1s) */
      unit[1]= 2; /* next level is 1s */
      unit[2]= 2; /* next level is 1s */
      base[0]= base[1]= base[2]= 2;
      subPower[0]= subPower[1]= subPower[2]= 0;
    } else {
      /* Subdecade ticks at 2 and 5 only. */
      unit[0]= 2; /* skips next level (subdivision into 1s) */
      unit[1]= 5; /* next level is 1s */
      unit[2]= 5; /* next level is 1s */
      base[0]= 2;
      base[1]= base[2]= 5;
      subPower[0]= subPower[1]= subPower[2]= 0;
    }

    minSubdecade= ceil(lo);
    FirstSubDecade(unit, lo, hi, zSmallest);
    nChangeLevel[++nLevel]= nTotal;

    /* Loop on tick level.  Each of the 3 intervals is subdivided once
       (or not at all) per pass.  */
    while (nLevel<TICK_LEVELS-1 &&
	   LogDivide(unit, base, fine, lo, hi, zSmallest))
      nChangeLevel[++nLevel]= nTotal;

    /* Correct for possibility that final pass produced no ticks.  */
    if (nLevel>0 && nChangeLevel[nLevel-1]==nChangeLevel[nLevel]) nLevel--;
  }
}

/* ------------------------------------------------------------------------ */

static void TickEndpoints(GpReal place, GpReal len, int lo, int hi,
			  GpReal *p0, GpReal *p1)
{
  if (lo) {
    if (hi) {   /* symmetric ticks */
      *p0= place-0.5*len;
      *p1= place+0.5*len;
    } else {    /* right or top justified ticks */
      *p0= place-len;
      *p1= place;
    }
  } else {    /* left or bottom justified ticks */
    *p0= place;
    *p1= place+len;
  }
}

static void DrawXTicks(GpReal place, GpReal *lens, int inout, int upper,
		       GpLineAttribs *attribs)
{
  int i, level, llev;
  GpReal y0, y1;
  int lo= upper? (inout & TICK_IN) : (inout & TICK_OUT);
  int hi= upper? (inout & TICK_OUT) : (inout & TICK_IN);

  level= llev= 0;
  TickEndpoints(place, lens[level], lo, hi, &y0, &y1);
  for (i=0 ; i<nTotal ; i++) {
    if (i==nChangeLevel[level]) {
      level++;
      if (i>0) TickEndpoints(place, lens[++llev], lo, hi, &y0, &y1);
    }
    py[i]= y0;
    qy[i]= y1;
    qx[i]= px[i]= scalx*ticks[i]+offx;
  }

  gistA.l= *attribs;
  GpDisjoint(nTotal, px, py, qx, qy);
}

static void DrawYTicks(GpReal place, GpReal *lens, int inout, int upper,
		       GpLineAttribs *attribs)
{
  int i, level, llev;
  GpReal x0, x1;
  int lo= upper? (inout & TICK_IN) : (inout & TICK_OUT);
  int hi= upper? (inout & TICK_OUT) : (inout & TICK_IN);

  level= llev= 0;
  TickEndpoints(place, lens[level], lo, hi, &x0, &x1);
  for (i=0 ; i<nTotal ; i++) {
    if (i==nChangeLevel[level]) {
      level++;
      if (i>0) TickEndpoints(place, lens[++llev], lo, hi, &x0, &x1);
    }
    px[i]= x0;
    qx[i]= x1;
    qy[i]= py[i]= scaly*ticks[i]+offy;
  }

  gistA.l= *attribs;
  GpDisjoint(nTotal, px, py, qx, qy);
}

static void DrawOrigin(GpReal x0, GpReal x1, GpReal y0, GpReal y1,
		       GpLineAttribs *attribs)
{
  gistA.l= *attribs;
  GpDisjoint(1L, &x0, &y0, &x1, &y1);
}

/* ------------------------------------------------------------------------ */

static char overflow[32];
static char fixedFormat[16];   /* e.g.- " %+05.0f" */
static int decimalPoint;
static int niceDecades;
static char *niceDecs[]=
       { "0.001", "0.01", "0.1", "1.0", "10.0", "100.0", "1000." };
static int overflowChar;
static char integerFormat[]= "% .0f";
static char decadeFormat[]= "E%+.0f";
static char nicedFormat[]= "10^%+.0f";

static GpReal iValue;  /* integer value of current label */
static int omitX, omitY;  /* indices of origin label for TICK_C */

static void InitLabels(int nDigits, char exponent[])
{
  int nLabels= nChangeLevel[0];
  GpReal maxAbs;
  int iPower, xPower;

  if (nLabels<=0) {
    int subDigits= (int)minSubdecade + subPower[0];
    /* log axis with no decade labels only (know jBase=1) */
    if (subDigits>=-3 && itick0<=3.0) {
      exponent[0]= '\0';
      niceDecades= 1;
    } else {
      sprintf(exponent, "E%+.0f", itick0-1.0);
      niceDecades= 0;
    }
    return;
  }

  /* iValue is the INTEGER (not normalized) value of the first label
     -- the first tick is at iValue*10^jPower */
  iValue= itick0*((GpReal)jBase);

  /* iPower is the decade of the largest integer in the list of tick values
     -- the decade of the largest label is iPower+jPower */
  maxAbs= fabs(itick0+(GpReal)(nLabels-1));
  if (nLabels>1 && maxAbs<fabs(itick0)) maxAbs= fabs(itick0);
  iPower= (int)log10(maxAbs*((GpReal)jBase)+0.5);
  xPower= iPower+jPower;

  if (nDigits<2) nDigits= 2;
  decimalPoint= 0;
  exponent[0]= '\0';
  niceDecades= 0;
  overflow[0]= '\0';
  overflowChar= 0;

  if (useLog) {
    /* Labels are decades on a log scale */
    int subDigits= (int)minSubdecade + subPower[0];
    int jp= jPower;
    GpReal fValue= iValue + (GpReal)((nLabels-1)*jBase);
    while (jp--) { iValue*= 10.0;  jBase*= 10; }
    if (subDecadeTicks) niceDecades= (subDigits>=-3 && fValue<=3.0);
    else niceDecades= (iValue>=-3.0 && fValue<=3.0);

  } else if ((xPower>3 || jPower<-3 || iPower>3) && iPower<nDigits) {
    /* Labels which are 10,000 and up, or which involve units less
       than 0.001, or which require more than 4 digits, will be printed
       in scientific notation */
    sprintf(exponent, "E%+d", xPower);
    sprintf(fixedFormat, " %%+0%d.0f", 2+iPower);
    decimalPoint= 2;  /* sign and leading digit move left for decimal pt */

  } else if (jPower>=0 && (iPower<nDigits || xPower<3)) {
    /* Labels which are multiples of 1.0 up to 9999 will be
       printed as integers */
    /* Make iValue, jBase the actual integer values */
    int value= (int)iValue;
    while (jPower--) {                /* WARNING- clobbers jBase, jPower */
      value*= 10;
      jBase*= 10;
    }
    iValue= (GpReal)value;

  } else if (iPower<nDigits || (jPower>-3 && iPower<3)) {
    /* Labels with units less than 1.0 but greater than 0.001, which
       can be written with 4 digits or fewer will be printed in
       fixed point notation */
    int iDigits= xPower<0? -jPower : iPower;
    sprintf(fixedFormat, " %%+0%d.0f", 2+iDigits);
    decimalPoint= 2+iDigits+jPower;

  } else {
    /* These labels are too wide to fit within the nDigit field */
    GpReal origin= 100.0*ceil(iValue/100.0); /* round iValue up to 100s */
    sprintf(fixedFormat, "%%+0%d.0f", 2+iPower);
    overflowChar= iPower;   /* first iPower-1 digits are overflow, last
			       two are used on individual labels */
    if (origin-iValue > (GpReal)(jBase*(nLabels-1))) origin= iValue;
    /* Assume origin has same number of digits as maxAbs (iPower) */
    sprintf(overflow, "x0= %+.0fE%+d", origin, xPower);
    /* Want, e.g.-  "x0=-1.2345??E+12"   (iPower=6) */
    if (origin<0.0) overflow[3]= '-';
    overflow[4]= overflow[5];
    overflow[5]= '.';
    overflow[4+iPower]= overflow[5+iPower]= '?';
  }
}

static GpReal positiveZero= 0.0; /* Yecch */

static void NextLabel(char label[])
{
  if (iValue==0.0) iValue= positiveZero; /* if sign bit set, prints "-0" */
  if (decimalPoint) {
    /* scientific or fixed point (starts with - or blank) */
    int i;
    sprintf(label, fixedFormat, iValue);
    for (i=0 ; i<decimalPoint ; i++) label[i]= label[i+1];
    label[decimalPoint]= '.';
    /* get rid of leading zero sometimes */
    for (i=0 ; i<decimalPoint-1 ; i++) {
      if (label[i]<'0' || label[i]>'9') continue;
      if (label[i]=='0') {
	do {
	  label[i]= label[i+1];
	  i++;
	} while (label[i]);
      }
      break;
    }
  } else if (overflowChar) {
    /* overflow (beyond nDigits) */
    sprintf(label, fixedFormat, iValue);
    label[0]= '?';
    label[1]= label[overflowChar];
    label[2]= label[overflowChar+1];
    label[3]= '\0';
  } else if (niceDecades) {
    /* All decades between 0.001 and 1000. inclusive */
    strcpy(label, niceDecs[3+(int)iValue]);
  } else if (useLog) {
    /* E-12 style decade */
    sprintf(label, gtDoEscapes? nicedFormat:decadeFormat, iValue);
  } else {
    /* integer (starts with - or blank) */
    sprintf(label, integerFormat, iValue);
  }

  /* increment label value for next tick */
  iValue+= (GpReal)jBase;
}

static void NiceLogLabel(char *label, char *format, GpReal value, GpReal tick)
{
       /* "0.001", "0.01", "0.1", "1.0", "10.0", "100.0", "1000." */
  int decade= (int)floor(tick);
  char *src, *dst, scratch[32];

  sprintf(scratch, format, value);
  src= scratch;
  dst= label;

  if (decade<0) {
    *dst++= '0';   *dst++= '.';
    while (++decade) *dst++= '0';
    while ((*dst++= *src++)) if (*src=='.') src++;
    if (dst[-2]=='0') dst[-2]= '\0';

  } else {  /* decade>=0 */
    int digits= 0;
    while ((*dst++= *src++)) {
      if (*src=='.') src++;
      if (digits==decade) *dst++= '.';
      digits++;
    }
    if (digits<=decade) {
      dst--;
      while (digits<=decade) { *dst++= '0';  digits++; }
      *dst++= '.';
      *dst= '\0';
    }
  }
}

static void GtText(GpReal x0, GpReal y0, const char *text)
{
  if (*text=='+') text++;
  GpText(x0, y0, text);
}

static int DrawXLabels(int isLog, GpReal place, int upper, int nDigits,
		       GpTextAttribs *attribs)
{
  int i, nLabel= nChangeLevel[0];
  char label[32], expspace[16], *exponent;
  GpReal tick0= 0.0;
  int altflag;

  gistA.t= *attribs;
  gistA.t.alignH= TH_CENTER;
  gistA.t.alignV= upper? TV_BASE : TV_CAP;

  exponent= expspace+2;
  InitLabels(nDigits, exponent); /* also sets overflow */
  if (gtDoEscapes && exponent[0]) {
    exponent= expspace;
    exponent[0]= '1';
    exponent[1]= '0';
    exponent[2]= '^';
  }

  altflag= (altlabel && !overflow[0] && !isLog);
  for (i=0 ; altflag && i<nLabel ; i++)
    altflag= !altlabel((char *)0, ticks[i]);

  for (i=0 ; i<nLabel ; i++) {
    if (!altflag) NextLabel(label);
    else altlabel(label, ticks[i]);
    if (i==0 && nLabel==1) {
      tick0= scalx*ticks[i]+offx;
      if (useLog && subDecadeTicks) omitX= -1;
    }
    if (i!=omitX) GtText(scalx*ticks[i]+offx, place, label);
  }

  if (useLog && subDecadeTicks) {
    GpReal value, unit, test= 0.0;
    int j;
    for (j=0 ; j<3 ; j++) if (nSubtotal[j]>nLabel) break;
    sprintf(fixedFormat, "%%.%df", -subPower[j]);
    nLabel= nSubtotal[2];   /* actual total count of labels */
    for (j=0 ; j<3 ; j++) { /* loop on (1,2] (2,5] (5,10) intervals */
      value= subTick0[j];
      unit= subUnit[j];
      if (i<nSubtotal[j]) {
	if (niceDecades) NiceLogLabel(label, fixedFormat, value, ticks[i]);
	else sprintf(label, fixedFormat, value);
	test= ticks[i];
      }
      for ( ; i<nSubtotal[j] ; i++) {
	if (test > ticks[i]+EPS_LOG) { /* i.e.- test!=ticks[i] */
	  value+= unit;
	  if (!niceDecades) sprintf(label, fixedFormat, value);
	  test= ticks[i];
	}
	if (niceDecades) NiceLogLabel(label, fixedFormat, value, ticks[i]);
	if (i==0 && nLabel==1) tick0= scalx*ticks[i]+offx;
	if (i!=omitX) GtText(scalx*ticks[i]+offx, place, label);
	test+= 1.0;   /* this is what EvenlySpace originally did... */
      }
    }
  }

  if (exponent[0]) {
    /* if present, exponent goes full line below X labels */
    GpReal exploc;
    GpReal nudged= place + (upper? +gistA.t.height : -gistA.t.height);
    if (nLabel>1) {
      exploc= 0.5*(ticks[nLabel-1]+ticks[nLabel-2]);
      exploc= scalx*exploc+offx;
    } else {
      if (fabs(tick0-gistT.viewport.xmin) >
	  fabs(tick0-gistT.viewport.xmax))
	exploc= 0.5*(tick0+gistT.viewport.xmin);
      else
	exploc= 0.5*(tick0+gistT.viewport.xmax);
    }
    GpText(exploc, nudged, exponent);
  }

  return overflow[0]!='\0';
}

static int DrawYLabels(int isLog, GpReal place, int upper, int nDigits,
		       GpTextAttribs *attribs)
{
  int i, nLabel= nChangeLevel[0];
  char label[32], expspace[16], *exponent;
  GpReal tick0= 0.0;
  int altflag;

  gistA.t= *attribs;
  gistA.t.alignH= upper? TH_LEFT : TH_RIGHT;
  gistA.t.alignV= TV_HALF;

  exponent= expspace+2;
  InitLabels(nDigits, exponent); /* also sets overflow */
  if (gtDoEscapes && exponent[0]) {
    exponent= expspace;
    exponent[0]= '1';
    exponent[1]= '0';
    exponent[2]= '^';
  }

  if (overflow[0]) {
    overflow[0]= 'y';
    /* put special marker where exponent would go --
       this is on y-axis only, since overflow text is likely to be
       "far away" from the y-axis labels, but "close to" x-axis labels */
    strcpy(exponent, "y0+?");
  }

  altflag= (altlabel && !overflow[0] && !isLog);
  for (i=0 ; altflag && i<nLabel ; i++)
    altflag= !altlabel((char *)0, ticks[i]);

  for (i=0 ; i<nLabel ; i++) {
    if (!altflag) NextLabel(label);
    else altlabel(label, ticks[i]);
    if (i==0 && nLabel==1) {
      tick0= scaly*ticks[i]+offy;
      if (useLog && subDecadeTicks) omitY= -1;
    }
    if (i!=omitY) GtText(place, scaly*ticks[i]+offy, label);
  }

  if (useLog && subDecadeTicks) {
    GpReal value, unit, test= 0.0;
    int j;
    for (j=0 ; j<3 ; j++) if (nSubtotal[j]>nLabel) break;
    sprintf(fixedFormat, "%%.%df", -subPower[j]);
    nLabel= nSubtotal[2];   /* actual total count of labels */
    for (j=0 ; j<3 ; j++) { /* loop on (1,2] (2,5] (5,10) intervals */
      value= subTick0[j];
      unit= subUnit[j];
      if (i<nSubtotal[j]) {
	if (niceDecades) NiceLogLabel(label, fixedFormat, value, ticks[i]);
	else sprintf(label, fixedFormat, value);
	test= ticks[i];
      }
      for ( ; i<nSubtotal[j] ; i++) {
	if (test > ticks[i]+EPS_LOG) { /* i.e.- test!=ticks[i] */
	  value+= unit;
	  if (!niceDecades) sprintf(label, fixedFormat, value);
	  test= ticks[i];
	}
	if (niceDecades) NiceLogLabel(label, fixedFormat, value, ticks[i]);
	if (i==0 && nLabel==1) tick0= scalx*ticks[i]+offx;
	if (i!=omitY) GtText(place, scaly*ticks[i]+offy, label);
	test+= 1.0;   /* this is what EvenlySpace originally did... */
      }
    }
  }

  if (exponent[0]) {
    /* if present, exponent goes slightly left of Y labels */
    GpReal exploc;
    GpReal nudged= place + (upper? +0.4 : -0.4)*gistA.t.height;
    if (nLabel>1) {
      exploc= 0.5*(ticks[nLabel-1]+ticks[nLabel-2]);
      exploc= scaly*exploc+offy;
    } else {
      if (fabs(tick0-gistT.viewport.ymin) >
	  fabs(tick0-gistT.viewport.ymax))
	exploc= 0.5*(tick0+gistT.viewport.ymin);
      else
	exploc= 0.5*(tick0+gistT.viewport.ymax);
    }
    GpText(nudged, exploc, exponent);
  }

  return overflow[0]!='\0';
}

static void DrawOverflow(GpReal x, GpReal y)
{
  /* Just finished drawing labels (DrawXLabels, DrawYLabels), so text
     attributes should all be set properly except for alignment */
  gistA.t.alignH= TH_NORMAL;
  gistA.t.alignV= TV_NORMAL;
  GpText(x, y, overflow);
}

/* ------------------------------------------------------------------------ */

static GpTransform saveMap;
static GpTransform unitMap= {{0., 2., 0., 2.}, {0., 2., 0., 2.}};

int GaTicks(GaTickStyle *ticks, int xIsLog, int yIsLog)
{
  return GaAltTick(ticks, xIsLog, yIsLog,
		   (GaAltTicks *)0, (GaAltLabel *)0,
		   (GaAltTicks *)0, (GaAltLabel *)0);
}

int GaAltTick(GaTickStyle *ticks, int xIsLog, int yIsLog,
	      GaAltTicks *xtick, GaAltLabel *xlabel,
	      GaAltTicks *ytick, GaAltLabel *ylabel)
{
       /* Draws a system of tick marks and labels for the current
	  transformation (gistT), according to the specified
	  tick style.  */

  GpReal xmin= gistT.viewport.xmin;
  GpReal xmax= gistT.viewport.xmax;
  GpReal ymin= gistT.viewport.ymin;
  GpReal ymax= gistT.viewport.ymax;

  GpReal wxmin= gistT.window.xmin;
  GpReal wxmax= gistT.window.xmax;
  GpReal wymin= gistT.window.ymin;
  GpReal wymax= gistT.window.ymax;

  scalx= (xmax-xmin)/(wxmax-wxmin);
  offx= xmin - scalx*wxmin;
  scaly= (ymax-ymin)/(wymax-wymin);
  offy= ymin - scaly*wymin;

  if (xmin>xmax) { GpReal tmp= xmin; xmin= xmax; xmax= tmp; }
  if (ymin>ymax) { GpReal tmp= ymin; ymin= ymax; ymax= tmp; }

  saveMap= gistT;
  GpSetTrans(&unitMap);
  gistClip= 0;

  if (ticks->horiz.flags & TICK_ANY) {
    altticks= (ticks->horiz.flags & ALT_TICK)? xtick : 0;
    altlabel= (ticks->horiz.flags & ALT_LABEL)? xlabel : 0;
    FindTicks(wxmin, wxmax, &ticks->horiz, xIsLog);

    if (ticks->horiz.flags & TICK_C) {
      /* Axis and ticks to be drawn in middle of viewport */
      GpReal yOrigin= FindOrigin(wymin, wymax, &ticks->vert, yIsLog);
      yOrigin= scaly*yOrigin+offy;

      DrawXTicks(yOrigin, ticks->horiz.tickLen, TICK_IN | TICK_OUT, 0,
		 &ticks->horiz.tickStyle);

      if ((ticks->horiz.flags & LABEL_ANY)) {
	omitX= FindOmitted(wxmin, wxmax);
	if (DrawXLabels(xIsLog, yOrigin-ticks->horiz.labelOff, 0,
		      ticks->horiz.nDigits, &ticks->horiz.textStyle))
	  DrawOverflow(ticks->horiz.xOver, ticks->horiz.yOver);
      }

    } else {
      /* Axis and ticks to be drawn around edges of viewport */
      int ovfl, jp, jb;

      if (ticks->horiz.flags & TICK_L)
	DrawXTicks(ymin-ticks->horiz.tickOff, ticks->horiz.tickLen,
		   ticks->horiz.flags, 0, &ticks->horiz.tickStyle);
      if (ticks->horiz.flags & TICK_U)
	DrawXTicks(ymax+ticks->horiz.tickOff, ticks->horiz.tickLen,
		   ticks->horiz.flags, 1, &ticks->horiz.tickStyle);

      omitX= -1;
      jp= jPower;  jb= jBase;
      ovfl= ((ticks->horiz.flags & LABEL_L) &&
	     DrawXLabels(xIsLog, ymin-ticks->horiz.labelOff, 0,
			 ticks->horiz.nDigits, &ticks->horiz.textStyle));
      jPower= jp;  jBase= jb;
      ovfl|= ((ticks->horiz.flags & LABEL_U) &&
	      DrawXLabels(xIsLog, ymax+ticks->horiz.labelOff, 1,
			  ticks->horiz.nDigits, &ticks->horiz.textStyle));
      if (ovfl)
	DrawOverflow(ticks->horiz.xOver, ticks->horiz.yOver);
    }
  }

  if (ticks->horiz.flags & GRID_F) {
    GpReal gridLen[TICK_LEVELS];
    GpReal delta= ymax-ymin;
    int i;
    if (nLevel>ticks->horiz.gridLevel)
      nTotal= nChangeLevel[ticks->horiz.gridLevel];
    for (i=0 ; i<=nLevel ; i++) gridLen[i]= delta;
    DrawXTicks(ymin, gridLen, TICK_IN, 0, &ticks->horiz.gridStyle);
  } else if (ticks->horiz.flags & GRID_O) {
    GpReal xOrigin= FindOrigin(wxmin, wxmax, &ticks->horiz, xIsLog);
    xOrigin= scalx*xOrigin+offx;
    DrawOrigin(xOrigin, xOrigin, ymin, ymax, &ticks->horiz.gridStyle);
  }

  if (ticks->vert.flags & TICK_ANY) {
    altticks= (ticks->horiz.flags & ALT_TICK)? ytick : 0;
    altlabel= (ticks->horiz.flags & ALT_LABEL)? ylabel : 0;
    FindTicks(wymin, wymax, &ticks->vert, yIsLog);

    if (ticks->vert.flags & TICK_C) {
      /* Axis and ticks to be drawn in middle of viewport */
      GpReal xOrigin= FindOrigin(wxmin, wxmax, &ticks->horiz, xIsLog);
      xOrigin= scalx*xOrigin+offx;

      DrawYTicks(xOrigin, ticks->vert.tickLen, TICK_IN | TICK_OUT, 0,
		 &ticks->vert.tickStyle);

      if ((ticks->vert.flags & LABEL_ANY)) {
	omitY= FindOmitted(wymin, wymax);
	if (DrawYLabels(yIsLog, xOrigin-ticks->vert.labelOff, 0,
			ticks->vert.nDigits, &ticks->vert.textStyle))
	  DrawOverflow(ticks->vert.xOver, ticks->vert.yOver);
      }

    } else {
      /* Axis and ticks to be drawn around edges of viewport */
      int ovfl, jp, jb;

      if (ticks->vert.flags & TICK_L)
	DrawYTicks(xmin-ticks->vert.tickOff, ticks->vert.tickLen,
		   ticks->vert.flags, 0, &ticks->vert.tickStyle);
      if (ticks->vert.flags & TICK_U)
	DrawYTicks(xmax+ticks->vert.tickOff, ticks->vert.tickLen,
		   ticks->vert.flags, 1, &ticks->vert.tickStyle);

      omitY= -1;
      jp= jPower;  jb= jBase;
      ovfl= ((ticks->vert.flags & LABEL_L) &&
	     DrawYLabels(xIsLog, xmin-ticks->vert.labelOff, 0,
			 ticks->vert.nDigits, &ticks->vert.textStyle));
      jPower= jp;  jBase= jb;
      ovfl|= ((ticks->vert.flags & LABEL_U) &&
	      DrawYLabels(xIsLog, xmax+ticks->vert.labelOff, 1,
			  ticks->vert.nDigits, &ticks->vert.textStyle));
      if (ovfl)
	DrawOverflow(ticks->vert.xOver, ticks->vert.yOver);
    }
  }

  if (ticks->vert.flags & GRID_F) {
    GpReal gridLen[TICK_LEVELS];
    GpReal delta= xmax-xmin;
    int i;
    if (nLevel>ticks->vert.gridLevel)
      nTotal= nChangeLevel[ticks->vert.gridLevel];
    for (i=0 ; i<=nLevel ; i++) gridLen[i]= delta;
    DrawYTicks(xmin, gridLen, TICK_IN, 0, &ticks->vert.gridStyle);
  } else if (ticks->vert.flags & GRID_O) {
    GpReal yOrigin= FindOrigin(wymin, wymax, &ticks->vert, yIsLog);
    yOrigin= scaly*yOrigin+offy;
    DrawOrigin(xmin, xmax, yOrigin, yOrigin, &ticks->vert.gridStyle);
  }

  if (ticks->frame) {
    GpReal x0[4], y0[4], x1[4], y1[4];
    x0[0]= x1[2]= x0[3]= x1[3]= xmin-ticks->vert.tickOff;
    x1[0]= x0[1]= x1[1]= x0[2]= xmax+ticks->vert.tickOff;
    y0[0]= y1[0]= y0[1]= y1[3]= ymin-ticks->horiz.tickOff;
    y1[1]= y0[2]= y1[2]= y0[3]= ymax+ticks->horiz.tickOff;
    gistA.l= ticks->frameStyle;
    GpDisjoint(4L, x0, y0, x1, y1);
  }

  GpSetTrans(&saveMap);
  return 0;
}

/* ------------------------------------------------------------------------ */