summaryrefslogtreecommitdiff
path: root/utils/hp2ps/Axes.c
blob: adc3c45146394ca0d3d07a8500290921dfb8f2ba (plain)
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
#include "Main.h"
#include <stdio.h>
#include <string.h>
#include "Curves.h"
#include "Defines.h"
#include "Dimensions.h"
#include "HpFile.h"
#include "Utilities.h"

/* own stuff */
#include "Axes.h"

typedef enum {MEGABYTE, KILOBYTE, BYTE} mkb; 

static void XAxis PROTO((void)); /* forward */
static void YAxis PROTO((void)); /* forward */

static void XAxisMark PROTO((floatish, floatish));      /* forward */
static void YAxisMark PROTO((floatish, floatish, mkb)); /* forward */

static floatish Round PROTO((floatish)); /* forward */

void
Axes(void)
{
    XAxis();
    YAxis();
}

static void
XAxisMark(floatish x, floatish num)
{
    /* calibration mark */
    fprintf(psfp, "%f %f moveto\n", xpage(x), ypage(0.0));
    fprintf(psfp, "0 -4 rlineto\n");
    fprintf(psfp, "stroke\n");

    /* number */
    fprintf(psfp, "HE%d setfont\n", NORMAL_FONT);
    fprintf(psfp, "(%.1f)\n", num);
    fprintf(psfp, "dup stringwidth pop\n");
    fprintf(psfp, "2 div\n");
    fprintf(psfp, "%f exch sub\n", xpage(x));
    fprintf(psfp, "%f moveto\n", borderspace);
    fprintf(psfp, "show\n");
}


#define N_X_MARKS   	 7
#define XFUDGE   	15	

extern floatish xrange;
extern char *sampleunitstring;

static void
XAxis(void)
{
    floatish increment, i; 
    floatish t, x;
    floatish legendlen;
 
    /* draw the x axis line */
    fprintf(psfp, "%f %f moveto\n", xpage(0.0), ypage(0.0));
    fprintf(psfp, "%f 0 rlineto\n", graphwidth);
    fprintf(psfp, "%f setlinewidth\n", borderthick);
    fprintf(psfp, "stroke\n"); 

    /* draw x axis legend */
    fprintf(psfp, "HE%d setfont\n", NORMAL_FONT);
    fprintf(psfp, "(%s)\n", sampleunitstring);
    fprintf(psfp, "dup stringwidth pop\n");
    fprintf(psfp, "%f\n", xpage(0.0) + graphwidth);
    fprintf(psfp, "exch sub\n");
    fprintf(psfp, "%f moveto\n", borderspace);
    fprintf(psfp, "show\n");


    /* draw x axis scaling */

    increment = Round(xrange / (floatish) N_X_MARKS);

    t = graphwidth / xrange;
    legendlen = StringSize(sampleunitstring) + (floatish) XFUDGE;
 
    for (i = samplemap[0]; i < samplemap[nsamples - 1]; i += increment) {
        x = (i - samplemap[0]) * t;  
 
        if (x < (graphwidth - legendlen)) { 
            XAxisMark(x,i);
        } 
    } 
}

static void
YAxisMark(floatish y, floatish num, mkb unit)
{
    /* calibration mark */
    fprintf(psfp, "%f %f moveto\n", xpage(0.0), ypage(y));
    fprintf(psfp, "-4 0 rlineto\n");
    fprintf(psfp, "stroke\n");
 
    /* number */
    fprintf(psfp, "HE%d setfont\n", NORMAL_FONT);

    switch (unit) {
    case MEGABYTE :
	fprintf(psfp, "(");
	CommaPrint(psfp, (intish) (num / 1e6 + 0.5));
	fprintf(psfp, "M)\n");
	break;
    case KILOBYTE :
	fprintf(psfp, "(");
	CommaPrint(psfp, (intish) (num / 1e3 + 0.5));
	fprintf(psfp, "k)\n");
	break;
    case BYTE:
	fprintf(psfp, "(");
	CommaPrint(psfp, (intish) (num + 0.5));
	fprintf(psfp, ")\n");
	break;
    }

    fprintf(psfp, "dup stringwidth\n");
    fprintf(psfp, "2 div\n");
    fprintf(psfp, "%f exch sub\n", ypage(y));

    fprintf(psfp, "exch\n");
    fprintf(psfp, "%f exch sub\n", graphx0 - borderspace);

    fprintf(psfp, "exch\n");
    fprintf(psfp, "moveto\n");
    fprintf(psfp, "show\n");
}

#define N_Y_MARKS 	 7	
#define YFUDGE 		15 

extern floatish yrange;
extern char *valueunitstring;

static void
YAxis(void)
{
    floatish increment, i;
    floatish t, y;
    floatish legendlen;
    mkb unit;

    /* draw the y axis line */
    fprintf(psfp, "%f %f moveto\n", xpage(0.0), ypage(0.0));
    fprintf(psfp, "0 %f rlineto\n", graphheight);
    fprintf(psfp, "%f setlinewidth\n", borderthick);
    fprintf(psfp, "stroke\n");

    /* draw y axis legend */
    fprintf(psfp, "gsave\n");
    fprintf(psfp, "HE%d setfont\n", NORMAL_FONT);
    fprintf(psfp, "(%s)\n", valueunitstring);
    fprintf(psfp, "dup stringwidth pop\n");
    fprintf(psfp, "%f\n", ypage(0.0) + graphheight);
    fprintf(psfp, "exch sub\n");
    fprintf(psfp, "%f exch\n", xpage(0.0) - borderspace);
    fprintf(psfp, "translate\n");
    fprintf(psfp, "90 rotate\n");
    fprintf(psfp, "0 0 moveto\n");
    fprintf(psfp, "show\n");
    fprintf(psfp, "grestore\n");

    /* draw y axis scaling */
    increment = max( yrange / (floatish) N_Y_MARKS, 1.0);
    increment = Round(increment);

    if (increment >= 1e6) {
	unit = MEGABYTE;
    } else if (increment >= 1e3) {
	unit = KILOBYTE;
    } else {
	unit = BYTE;
    }	

    t = graphheight / yrange; 
    legendlen = StringSize(valueunitstring) + (floatish) YFUDGE; 

    for (i = 0.0; i <= yrange; i += increment) {
        y = i * t;

        if (y < (graphheight - legendlen)) {
            YAxisMark(y, i, unit);
        }
    } 
}


/*
 *      Find a "nice round" value to use on the axis.
 */

static floatish OneTwoFive PROTO((floatish)); /* forward */

static floatish
Round(floatish y)
{
    int i;

    if (y > 10.0) {
	for (i = 0; y > 10.0; y /= 10.0, i++)
	    ;
	y = OneTwoFive(y);
	for ( ; i > 0; y = y * 10.0, i--) 
	    ;

    } else if (y < 1.0) {
	for (i = 0; y < 1.0; y *= 10.0, i++) 
	    ;
        y = OneTwoFive(y);
        for ( ; i > 0; y = y / 10.0, i--) 
            ;
 
    } else {
	y = OneTwoFive(y);
    }

    return (y);
}


/*
 * OneTwoFive() -- Runciman's 1,2,5 scaling rule. Argument  1.0 <= y <= 10.0.
 */

static floatish
OneTwoFive(floatish y)
{
    if (y > 4.0) {
	return (5.0);
    } else if (y > 1.0) {
	return (2.0);
    } else {
	return (1.0);
    }   
}