Mercurial > hg > svcore
comparison base/ScaleTickIntervals.h @ 1408:f89365917d02 scale-ticks
These tests now pass, but there's still a lot to be done
author | Chris Cannam |
---|---|
date | Wed, 03 May 2017 17:02:01 +0100 |
parents | 25ed6dde2ce0 |
children | 21ba60008200 |
comparison
equal
deleted
inserted
replaced
1407:25ed6dde2ce0 | 1408:f89365917d02 |
---|---|
59 if (inc == 0) { | 59 if (inc == 0) { |
60 Ticks t { r.min, 1.0, r.min, false, 1, {} }; | 60 Ticks t { r.min, 1.0, r.min, false, 1, {} }; |
61 explode(r, t); | 61 explode(r, t); |
62 return t; | 62 return t; |
63 } | 63 } |
64 | 64 |
65 double ilg = log10(inc); | 65 double digInc = log10(inc); |
66 int prec = int((ilg > 0.0) ? round(ilg) : trunc(ilg)) - 1; | 66 double digMax = log10(fabs(r.max)); |
67 double digMin = log10(fabs(r.min)); | |
68 | |
69 int precInc = int(trunc(digInc)) - 1; | |
70 | |
71 bool fixed = false; | |
72 if (precInc > -4 && precInc < 4) { | |
73 fixed = true; | |
74 } else if ((digMax >= -3.0 && digMax <= 2.0) && | |
75 (digMin >= -3.0 && digMin <= 3.0)) { | |
76 fixed = true; | |
77 } | |
78 | |
79 int precRange = int(ceil(digMax - digInc)); | |
80 | |
81 int prec = 1; | |
82 | |
83 if (fixed) { | |
84 prec = precInc; | |
85 if (prec < 0) { | |
86 prec = -prec; | |
87 } | |
88 } else { | |
89 prec = precRange; | |
90 } | |
91 | |
92 std::cerr << "\nmin = " << r.min << ", max = " << r.max << ", n = " << r.n | |
93 << ", inc = " << inc << std::endl; | |
94 std::cerr << "digMax = " << digMax << ", digInc = " << digInc | |
95 << std::endl; | |
96 std::cerr << "fixed = " << fixed << ", inc = " << inc | |
97 << ", precInc = " << precInc << ", precRange = " << precRange | |
98 << ", prec = " << prec << std::endl; | |
99 | |
100 // int prec = int((ilg > 0.0) ? round(ilg) : trunc(ilg)) - 1; | |
101 /* | |
67 int dp = 0, sf = 0; | 102 int dp = 0, sf = 0; |
68 bool fixed = false; | 103 bool fixed = false; |
69 if (prec < 0) { | 104 if (prec < 0) { |
70 dp = -prec; | 105 dp = -prec; |
71 sf = 1; // was 2, but should probably vary | 106 sf = 1; // was 2, but should probably vary |
76 sf = 1; | 111 sf = 1; |
77 } | 112 } |
78 if (prec > -4 && prec < 4) { | 113 if (prec > -4 && prec < 4) { |
79 fixed = true; | 114 fixed = true; |
80 } | 115 } |
116 */ | |
117 /* bool fixed = true; | |
118 int dp = sig; | |
119 int sf = prec; | |
120 */ | |
121 double roundTo = pow(10.0, precInc); | |
81 | 122 |
82 double roundTo = pow(10.0, prec); | 123 std::cerr << "roundTo = " << roundTo << std::endl; |
124 | |
83 inc = round(inc / roundTo) * roundTo; | 125 inc = round(inc / roundTo) * roundTo; |
126 if (inc < roundTo) inc = roundTo; | |
127 | |
84 double min = ceil(r.min / roundTo) * roundTo; | 128 double min = ceil(r.min / roundTo) * roundTo; |
85 if (min > r.max) min = r.max; | 129 if (min > r.max) min = r.max; |
86 | 130 |
87 Ticks t { min, inc, roundTo, fixed, fixed ? dp : sf, {} }; | 131 Ticks t { min, inc, roundTo, fixed, prec, {} }; |
88 explode(r, t); | 132 explode(r, t); |
89 return t; | 133 return t; |
90 } | 134 } |
91 | 135 |
92 private: | 136 private: |
100 snprintf(buffer, buflen, | 144 snprintf(buffer, buflen, |
101 t.fixed ? "%.*f" : "%.*e", | 145 t.fixed ? "%.*f" : "%.*e", |
102 t.precision, value); | 146 t.precision, value); |
103 return Tick({ value, std::string(buffer) }); | 147 return Tick({ value, std::string(buffer) }); |
104 }; | 148 }; |
105 for (double value = t.initial; value <= r.max; value += t.spacing) { | 149 for (double value = t.initial; |
150 value < r.max + t.spacing/2; | |
151 value += t.spacing) { | |
106 value = t.roundTo * round(value / t.roundTo); | 152 value = t.roundTo * round(value / t.roundTo); |
107 t.ticks.push_back(makeTick(value)); | 153 t.ticks.push_back(makeTick(value)); |
108 } | 154 } |
109 } | 155 } |
110 }; | 156 }; |