-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPScale.cxx
500 lines (455 loc) · 15.5 KB
/
PScale.cxx
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
//==============================================================================
// File: PScale.cxx
//
// Created: 10/07/98 - PH (adapted from SNOTT scale C++ object)
//
// Copyright (c) 2017, Phil Harvey, Queen's University
//==============================================================================
#include <X11/Xlib.h>
#include <string.h>
#include <stdio.h>
#include "PDrawable.h"
#include "PScale.h"
const int FIRST_POW = -15; // first symbol exponent
static char symbols[] = "fpnum\0kMG"; // symbols for exponents
//----------------------------------------------------------------------------
// PScale constructor
//
PScale::PScale(PDrawable *drawable,XFontStruct *font,int xa,int ya,int xb,int yb,int flags)
{
mDrawable = drawable;
mFont = font;
mXa = xa; mYa = ya;
mXb = xb; mYb = yb;
log_scale = flags & LOG_SCALE;
upside_down = flags & TICKS_UPSIDE_DOWN;
integer = flags & INTEGER_SCALE;
if (ya==yb) { // horizontal scale if two y coords are the same
xaxis = 1;
opos1 = pos1 = xa;
opos2 = pos2 = xb;
dpos = xb-xa;
dlbl = XTextWidth(font, "00000", 5) * 2 * mDrawable->GetScaling();
} else {
xaxis = 0;
opos1 = pos1 = yb;
opos2 = pos2 = ya;
dpos = yb-ya;
double spacing;
int font_height = (mDrawable->GetFontAscent() + mDrawable->GetFontDescent()) * mDrawable->GetScaling();
int num_rows = dpos / font_height;
if (num_rows <= 8) {
spacing = 1.5;
} else if (num_rows <= 10) {
spacing = 2.0;
} else if (num_rows <= 12) {
spacing = 2.5;
} else {
spacing = 3.0;
}
dlbl = (int)(font_height * spacing);
}
min_val = max_val = val_rng = 0;
}
/* calculate scaling factors */
void PScale::CalcScalingFactors()
{
double lmin, lmax;
if (log_scale) {
if (min_val == 0) {
offset = 1;
fscl = dpos / log(val_rng+1);
fpos = pos1 + 0.5;
} else {
offset = 0;
lmin = log(min_val);
lmax = log(max_val);
if (xaxis) fscl = dpos / (lmin-lmax);
else fscl = dpos / (lmax-lmin);
fpos = ((pos1+pos2+1)+fscl*(lmin+lmax)) / 2.0;
}
} else {
fscl = dpos / val_rng;
if (!xaxis) fscl = (-fscl);
fpos = pos1 - fscl*min_val + 0.5;
}
}
/* set range for scale */
void PScale::SetRng(double low,double high)
{
if (high!=max_val || low!=min_val) {
if (log_scale && low < 0) {
min_val = 0; // 0 is minimum for log scale
} else {
min_val = low;
}
max_val = high;
val_rng = max_val-min_val;
CalcScalingFactors();
}
Draw();
}
/*----------------------------------------------------------------------------
** get relative scale value given relative pixel displacement
*/
double PScale::GetRelVal(int pix)
{
return(pix*val_rng/dpos);
}
/* get absolute scale value */
double PScale::GetVal(int pix)
{
if (log_scale) {
return(exp((pix - fpos)/fscl) - offset);
} else {
return((pix - fpos) / fscl);
}
}
/*----------------------------------------------------------------------------
** get absolute pixel location given absolute scale value
*/
int PScale::GetPix(double val)
{
if (log_scale) return(GetLogPix(val));
else return(GetLinPix(val));
}
/*----------------------------------------------------------------------------
** get absolute pixel location given absolute scale value,
** constrained to be within the scale limits
*/
int PScale::GetPixConstrained(double val)
{
if (val <= min_val) {
return pos1;
} else if (val >= max_val) {
return pos2;
} else {
return GetPix(val);
}
}
/*----------------------------------------------------------------------------
** get absolute pixel location given absolute scale value,
** constrained to be within the scale limits. Bit 0 is set
** in "out" if val was under scale, and bit 1 is set if it was
** over scale.
*/
int PScale::GetPixConstrained(double val, int *out)
{
if (val < min_val) {
*out |= 0x01;
return pos1;
} else if (val > max_val) {
*out |= 0x02;
return pos2;
} else {
return GetPix(val);
}
}
/*----------------------------------------------------------------------------
** drawLinScale - draw linear scale
*/
void PScale::DrawLinScale()
{
int i, x, y; // general variables
double val; // true value of scale units
int ival; // integer mantissa of scale units
int sep; // mantissa of label separation
int power; // exponent of label separation
int ticks; // number of ticks per label
int sign = 1; // sign of scale range
double order; // 10^power
double step; // distance between labels (scale units)
double tstep; // distance between ticks (scale units)
double tol; // tolerance for equality (1/2 pixel)
double lim; // limit for loops
char suffix; // si suffix for number
char dec = 0; // flag to print decimal point
char buff[128];
int scaling = mDrawable->GetScaling();
if (!dpos) return; // check this to be safe - PH 12/21/99
mDrawable->SetLineWidth(0.5);
tstep = GetRelVal(dlbl);
if (tstep < 0) {
sign = -1;
tstep *= sign;
}
/*
** Old getSep() Routine: Determine axis labelling strategies.
**
** Input: tstep = number of units between optimally spaced labels
** Output: tick = number of units between ticks
** sep = number of units between labels
*/
if (tstep <= 0) {
power = 1;
} else {
power = (int)floor(log10(tstep)); // exponent part of label sep
}
i = (int)(10.0*tstep/(order = pow(10.0,(double)power))); // get first two digits
if (i >= 65) { sep = 1; ticks = 5; ++power; order*=10; }
else if (i >= 35) { sep = 5; ticks = 5; }
else if (i >= 15) { sep = 2; ticks = 4; }
else { sep = 1; ticks = 5; }
if (!power && integer) ticks = sep; // no sub-ticks for integer scales
/*
** End of old getSep() routine
*/
int pow0 = power - FIRST_POW;
if ((pow0 < 0) || (pow0/3 >= 9)) return; // just to be safe
sep *= sign;
step = sep * order;
ival = (int)floor(min_val/step);
val = ival * step; // value for first label below scale
tstep = step/ticks;
ival *= sep;
suffix = symbols[pow0/3];
switch (pow0 % 3) {
case 0:
break;
case 1:
ival *= 10;
sep *= 10;
break;
case 2:
if (suffix) { // avoid extra trailing zeros if suffix
suffix= symbols[pow0/3+1]; // next suffix
dec = 1; // use decimal point (/10)
} else {
ival *= 100;
sep *= 100;
}
break;
}
tol = GetRelVal(1)/2.00001;
lim = -tol;
val -= min_val; // subtract origin
if (val*sign < lim*sign) {
ival += sep; // get ival for first label
for (i=0; val*sign<lim*sign; ++i) {
val += tstep; // find first tick on scale
}
} else i = ticks; // first tick is at label
lim = max_val + tol; // upper limit for scale value
val += min_val;
if (xaxis) {
y = mYa + 1;
mDrawable->DrawLine(opos1,mYa,opos2,mYa);
for (;;) {
x = GetLinPix(val); // get pixel position
if (i < ticks) {
mDrawable->DrawLine(x,y,x,y+scaling);
++i;
} else {
mDrawable->DrawLine(x,y,x,y+3*scaling);
if (dec) {
if (ival<0) sprintf(buff,"-%.1d.%.1d%c",(-ival)/10,(-ival)%10,suffix);
else sprintf(buff,"%.1d.%.1d%c",ival/10,ival%10,suffix);
} else if (suffix == 'm') {
sprintf(buff,"%g",ival/1000.0);
} else {
sprintf(buff,"%d%c",ival,suffix);
}
mDrawable->DrawString(x,y+5*scaling,buff,kTextAlignTopCenter);
ival += sep;
i = 1;
}
val += tstep;
if (sign==1) {
if (val > lim) break;
} else {
if (val < lim) break;
}
}
} else {
if (upside_down) {
x = mXa + 1;
mDrawable->DrawLine(mXa,opos2,mXa,opos1);
for (;;) {
y = GetLinPix(val); // get pixel position
if (i < ticks) {
mDrawable->DrawLine(x,y,x+scaling,y);
++i;
} else {
mDrawable->DrawLine(x,y,x+3*scaling,y);
if (dec) {
if (ival<0) sprintf(buff,"-%.1d.%.1d%c",(-ival)/10,(-ival)%10,suffix);
else sprintf(buff,"%.1d.%.1d%c",ival/10,ival%10,suffix);
} else if (suffix == 'm') {
sprintf(buff,"%g",ival/1000.0);
} else {
sprintf(buff,"%d%c",ival,suffix);
}
mDrawable->DrawString(x+4*scaling, y,buff,kTextAlignMiddleLeft);
ival += sep;
i = 1;
}
val += tstep;
if (sign==1) {
if (val > lim) break;
} else {
if (val < lim) break;
}
}
} else {
x = mXb - 1;
mDrawable->DrawLine(mXb,opos2,mXb,opos1);
for (;;) {
y = GetLinPix(val); // get pixel position
if (i < ticks) {
mDrawable->DrawLine(x-scaling,y,x,y);
++i;
} else {
mDrawable->DrawLine(x-3*scaling,y,x,y);
if (dec) {
if (ival<0) sprintf(buff,"-%.1d.%.1d%c",(-ival)/10,(-ival)%10,suffix);
else sprintf(buff,"%.1d.%.1d%c",ival/10,ival%10,suffix);
} else if (suffix == 'm') {
sprintf(buff,"%g",ival/1000.0);
} else {
sprintf(buff,"%d%c",ival,suffix);
}
mDrawable->DrawString(x-5*scaling,y,buff,kTextAlignMiddleRight);
ival += sep;
i = 1;
}
val += tstep;
if (sign==1) {
if (val > lim) break;
} else {
if (val < lim) break;
}
}
}
}
mDrawable->SetLineWidth(1);
}
/*----------------------------------------------------------------------------
** Draw log scale
**
** Note: TICKS_UPSIDE_DOWN doesn't work for log scales
*/
void PScale::DrawLogScale()
{
int n,x,y,nmax;
int label_sep,tick_sep;
double val, mval, kval, base, inc;
char buff[128];
int scaling = mDrawable->GetScaling();
if (!dpos) return; // check this to be safe - PH 12/21/99
mDrawable->SetLineWidth(0.5);
val = fabs(log(11.0)*fscl);
inc = 1.0;
nmax = 10;
while (val > 300 * scaling) {
val /= 10;
inc /= 10;
nmax *= 10;
}
if (val < dlbl) { tick_sep=5; label_sep=10; }
else if (val < dlbl*1.8){ tick_sep=2; label_sep=10; }
else if (val < dlbl*5.5){ tick_sep=1; label_sep=5; }
else if (val < dlbl*10) { tick_sep=1; label_sep=2; }
else { tick_sep=1; label_sep=1; }
kval = 1e3 / inc;
mval = 1e6 / inc;
if (xaxis) {
y = mYa + 1;
mDrawable->DrawLine(opos1,mYa,opos2,mYa);
if (min_val == 0) {
mDrawable->DrawLine(pos1,y,pos1,y+3*scaling);
strcpy(buff,"0");
mDrawable->DrawString(pos1,y+5*scaling,buff,kTextAlignTopCenter);
base = 1;
val = inc;
if (label_sep == 2) n = 0; // special case to draw "1" label
else n = (int)inc;
} else {
val = base = pow(10,floor(log10(min_val)));
n = 0;
while (val < min_val) {
++n;
val += base * inc;
}
}
for (;;) {
if (!(n%tick_sep)) {
x = GetLogPix(val); // get pixel position
if ((!(n%label_sep)) || (n==2 && label_sep==5)) {
if (!n) n = (int)inc;
mDrawable->DrawLine(x,y,x,y+3*scaling);
if (val<kval) sprintf(buff,"%.4g",val);
else if (val<mval) sprintf(buff,"%.4gk",val*1e-3);
else sprintf(buff,"%.4gM",val*1e-6);
mDrawable->DrawString(x,y+5*scaling,buff,kTextAlignTopCenter);
} else {
mDrawable->DrawLine(x,y,x,y+scaling);
}
}
if (++n > nmax) {
n = nmax/10;
base *= 10;
/*
** draw tick at 15, 150, etc
*/
if (label_sep<=5 && (val=base*1.5)<=max_val) {
x = GetLogPix(val); // get pixel position
mDrawable->DrawLine(x,y,x,y+scaling);
}
val = base;
} else {
val += base * inc;
}
if (val > max_val) break;
}
} else {
x = mXb - 1;
mDrawable->DrawLine(mXb,opos2,mXb,opos1);
if (min_val == 0) {
mDrawable->DrawLine(x-3*scaling,pos1,x,pos1);
strcpy(buff,"0");
mDrawable->DrawString(x-5*scaling,pos1,buff,kTextAlignMiddleRight);
val = inc;
base = 1;
if (label_sep == 2) n = 0; // special case to draw "1" label
else n = 1;
} else {
val = base = pow(10,floor(log10(min_val)));
n = 0;
while (val < min_val) {
++n;
val += base * inc;
}
}
for (;;) {
if (!(n%tick_sep)) {
y = GetLogPix(val); // get pixel position
if ((!(n%label_sep)) || (n==2 && label_sep==5)) {
if (!n) n = 1;
mDrawable->DrawLine(x-3*scaling,y,x,y);
if (val<kval) sprintf(buff,"%.4g",val);
else if (val<mval) sprintf(buff,"%.4gk",val*1e-3);
else sprintf(buff,"%.4gM",val*1e-6);
mDrawable->DrawString(x-5*scaling,y,buff,kTextAlignMiddleRight);
} else {
mDrawable->DrawLine(x-scaling,y,x,y);
}
}
if (++n > nmax) {
n = nmax/10;
base *= 10;
/*
** draw tick at 15, 150, etc
*/
if (label_sep<=5 && (val=base*1.5)<=max_val) {
y = GetLogPix(val); // get pixel position
mDrawable->DrawLine(x-scaling,y,x,y);
}
val = base;
} else {
val += base * inc;
}
if (val > max_val) break;
}
}
mDrawable->SetLineWidth(1);
}