-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlabel.c
More file actions
133 lines (111 loc) · 3.02 KB
/
label.c
File metadata and controls
133 lines (111 loc) · 3.02 KB
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
/*
* label.c: nice graph labeling
* adapted from from:
* "Graphic Gems in C"
* Paul Heckbert 2 Dec 88
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "points.h"
#define MAX(a,b) (((a)>(b))?(a):(b))
static double nicenum();
// nticks corresponds to maxxdiv, maxydiv in autoplot
void loose_label(PLOTDAT *pd, double *min, double *max, int nticks, int axis, int dolabel,
int logmode, int dryrun) {
char str[6], temp[20];
double d; /* tick mark spacing */
double graphmin, graphmax;
double range, x;
if (logmode != 1) { // just do linear scaling
range = nicenum(*max-*min, 0); /* we expect min!=max */
// d = nicenum(range/(double)(nticks+4),1);
d = nicenum(range/(double)(nticks),1);
if (dryrun) {
graphmin = floor(*min/d)*d;
graphmax = ceil(*max/d)*d;
} else {
graphmin = *min;
graphmax = *max;
}
// int nfrac;
// nfrac = MAX(3-floor(log10(range/d)),0); /* # frac digits to show */
// sprintf(str,"%%.%dg", nfrac); /* simplest axis labels */
sprintf(str,"%%g");
// printf("range = %g d = %g, nfrac=%d\n", range, d, nfrac);
if (!dryrun) {
// printf("graphmin=%g graphmax=%g increment=%g\n", graphmin, graphmax, d);
for (x=graphmin+d; x<graphmax-0.5*d; x+=d) {
gridline(pd, (x-graphmin)/(graphmax-graphmin), axis);
}
for (x=graphmin; x<graphmax+0.5*d; x+=d) {
x = (d/100.0)*rint(x/(d/100.0));
sprintf(temp, str, x);
if (dolabel) {
gridlabel(pd, temp, (x-graphmin)/(graphmax-graphmin), axis);
}
}
}
*min = graphmin;
*max = graphmax;
} else { // log mode, so do scaling by decades
if (dryrun) {
graphmin = floor(*min);
graphmax = ceil(*max);
} else {
graphmin = *min;
graphmax = *max;
}
if (!dryrun) {
for (x=graphmin+1; x<graphmax-0.5; x++) {
gridline(pd, (x-graphmin)/(graphmax-graphmin), axis);
}
for (x=graphmin; x<graphmax+0.5; x++) {
sprintf(temp, "%g", pow(10.0,x));
if (dolabel) {
gridlabel(pd, temp, (x-graphmin)/(graphmax-graphmin), axis);
}
}
}
*min = graphmin;
*max = graphmax;
}
}
/*
* nicenum: find a "nice" number approximately equal to x.
* Round the number if round=1, take ceiling if round=0
*/
static double nicenum(x,round)
double x;
int round;
{
int exp; /* exponent of x */
double f; /* fractional part of x */
double nf; /* nice, rounded fraction */
exp = floor(log10(x));
f = x/pow(10.0, (double) exp); /* between 1 and 10 */
if (round) {
if (f<1.5) nf = 1.0;
else if (f<3.0) nf = 2.0;
else if (f<7.0) nf = 5.0;
else nf = 10.0;
} else {
if (f<1.0) nf = 1.0;
else if (f<2.0) nf = 2.0;
else if (f<5.0) nf = 5.0;
else nf = 10.0;
}
return nf*pow(10.0, (double) exp);
}
#define NTICK 9 /* desired number of tick marks */
void testmain(int argc, char ** argv) {
double min, max;
PLOTDAT foo;
if (argc!=3) {
fprintf(stderr,"usage: label <min> <max>\n");
exit(1);
}
min = atof(argv[1]);
max = atof(argv[2]);
loose_label(&foo, &min,&max,NTICK,1,1,0,0);
}