blob: 2959f0267ccb6116f1f8726c51c37f93057b9a75 [file] [log] [blame]
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +01001#include <stdio.h>
2#include <math.h>
3#include <malloc.h>
Stephen M. Cameron17ba3e42012-03-07 19:36:18 +01004#include <string.h>
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +01005
6/*
7 * adapted from Paul Heckbert's algorithm on p 657-659 of
8 * Andrew S. Glassner's book, "Graphics Gems"
9 * ISBN 0-12-286166-3
10 *
11 */
12
13#include "tickmarks.h"
14
15#define MAX(a, b) (((a) < (b)) ? (b) : (a))
16
17static double nicenum(double x, int round)
18{
19 int exp; /* exponent of x */
20 double f; /* fractional part of x */
21
22 exp = floor(log10(x));
23 f = x / pow(10.0, exp);
24 if (round) {
25 if (f < 1.5)
26 return 1.0 * pow(10.0, exp);
27 if (f < 3.0)
28 return 2.0 * pow(10.0, exp);
29 if (f < 7.0)
30 return 5.0 * pow(10.0, exp);
31 return 10.0 * pow(10.0, exp);
32 }
33 if (f <= 1.0)
34 return 1.0 * pow(10.0, exp);
35 if (f <= 2.0)
36 return 2.0 * pow(10.0, exp);
37 if (f <= 5.0)
38 return 5.0 * pow(10.0, exp);
39 return 10.0 * pow(10.0, exp);
40}
41
Stephen M. Cameron7175d912012-03-11 11:35:10 +010042static void shorten(struct tickmark *tm, int nticks, int *power_of_ten,
43 int use_KMG_symbols)
Stephen M. Cameron17ba3e42012-03-07 19:36:18 +010044{
Stephen M. Cameron7175d912012-03-11 11:35:10 +010045 int i, l, minshorten;
Stephen M. Cameron1e1ffcc2012-03-11 11:33:46 +010046 char *str;
47 char shorten_char = '?';
Stephen M. Cameron17ba3e42012-03-07 19:36:18 +010048
Stephen M. Cameron1e1ffcc2012-03-11 11:33:46 +010049 minshorten = 100;
50 for (i = 0; i < nticks; i++) {
51 str = tm[i].string;
52 l = strlen(str);
53
54 if (l > 9 && strcmp(&str[l - 9], "000000000") == 0) {
Stephen M. Cameron7175d912012-03-11 11:35:10 +010055 *power_of_ten = 9;
56 shorten_char = use_KMG_symbols ? 'G' : '\0';
Stephen M. Cameron1e1ffcc2012-03-11 11:33:46 +010057 } else if (6 < minshorten && l > 6 &&
58 strcmp(&str[l - 6], "000000") == 0) {
Stephen M. Cameron7175d912012-03-11 11:35:10 +010059 *power_of_ten = 6;
60 shorten_char = use_KMG_symbols ? 'M' : '\0';
Stephen M. Cameron1e1ffcc2012-03-11 11:33:46 +010061 } else if (l > 3 && strcmp(&str[l - 3], "000") == 0) {
Stephen M. Cameron7175d912012-03-11 11:35:10 +010062 *power_of_ten = 3;
63 shorten_char = use_KMG_symbols ? 'K': '\0';
Stephen M. Cameron1e1ffcc2012-03-11 11:33:46 +010064 } else {
Stephen M. Cameron7175d912012-03-11 11:35:10 +010065 *power_of_ten = 0;
Stephen M. Cameron1e1ffcc2012-03-11 11:33:46 +010066 }
67
Stephen M. Cameron7175d912012-03-11 11:35:10 +010068 if (*power_of_ten < minshorten)
69 minshorten = *power_of_ten;
Stephen M. Cameron1e1ffcc2012-03-11 11:33:46 +010070 }
71
72 if (minshorten == 0)
73 return;
74
75 for (i = 0; i < nticks; i++) {
76 str = tm[i].string;
77 l = strlen(str);
78 str[l - minshorten] = shorten_char;
79 str[l - minshorten + 1] = '\0';
Stephen M. Cameron17ba3e42012-03-07 19:36:18 +010080 }
81}
82
Stephen M. Cameron7175d912012-03-11 11:35:10 +010083int calc_tickmarks(double min, double max, int nticks, struct tickmark **tm,
84 int *power_of_ten, int use_KMG_symbols)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +010085{
86 char str[100];
87 int nfrac;
88 double d; /* tick mark spacing */
89 double graphmin, graphmax; /* graph range min and max */
90 double range, x;
91 int count, i;
92
93 /* we expect min != max */
94 range = nicenum(max - min, 0);
95 d = nicenum(range / (nticks - 1), 1);
96 graphmin = floor(min / d) * d;
97 graphmax = ceil(max / d) * d;
98 nfrac = MAX(-floor(log10(d)), 0);
99 snprintf(str, sizeof(str)-1, "%%.%df", nfrac);
100
101 count = ((graphmax + 0.5 * d) - graphmin) / d + 1;
102 *tm = malloc(sizeof(**tm) * count);
103
104 i = 0;
105 for (x = graphmin; x < graphmax + 0.5 * d; x += d) {
106 (*tm)[i].value = x;
107 sprintf((*tm)[i].string, str, x);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100108 i++;
109 }
Stephen M. Cameron7175d912012-03-11 11:35:10 +0100110 shorten(*tm, i, power_of_ten, use_KMG_symbols);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100111 return i;
112}
113
114#if 0
115
116static void test_range(double x, double y)
117{
118 int nticks, i;
119
120 struct tickmark *tm = NULL;
121 printf("Testing range %g - %g\n", x, y);
122 nticks = calc_tickmarks(x, y, 10, &tm);
123
124 for (i = 0; i < nticks; i++) {
125 printf(" (%s) %g\n", tm[i].string, tm[i].value);
126 }
127 printf("\n\n");
128 free(tm);
129}
130
131int main(int argc, char *argv[])
132{
133 test_range(0.0005, 0.008);
134 test_range(0.5, 0.8);
135 test_range(5.5, 8.8);
136 test_range(50.5, 80.8);
137 test_range(-20, 20.8);
138 test_range(-30, 700.8);
139}
140#endif