Mercurial > hg > svcore
changeset 1412:b7a9edee85e0 scale-ticks
Change loop to something that feels more correct, though it makes no difference to the tests here. More tests, one failing.
author | Chris Cannam |
---|---|
date | Thu, 04 May 2017 08:32:41 +0100 |
parents | 1f0d071e7ce6 |
children | c6fa111b4553 |
files | base/ScaleTickIntervals.h base/test/TestScaleTickIntervals.h |
diffstat | 2 files changed, 118 insertions(+), 3 deletions(-) [+] |
line wrap: on
line diff
--- a/base/ScaleTickIntervals.h Wed May 03 18:26:26 2017 +0100 +++ b/base/ScaleTickIntervals.h Thu May 04 08:32:41 2017 +0100 @@ -20,7 +20,7 @@ #include <vector> #include <cmath> -//#define DEBUG_SCALE_TICK_INTERVALS 1 +#define DEBUG_SCALE_TICK_INTERVALS 1 #ifdef DEBUG_SCALE_TICK_INTERVALS #include <iostream> @@ -32,7 +32,7 @@ struct Range { double min; // start of value range double max; // end of value range - int n; // number of divisions (will be at most n+1 ticks) + int n; // number of divisions (approximate only) }; struct Tick { @@ -146,9 +146,15 @@ if (t.spacing < eps * 10.0) { eps = t.spacing / 10.0; } - for (double value = t.initial; value < r.max + eps; value += t.spacing) { + int n = 0; + while (true) { + double value = t.initial + n * t.spacing; value = t.roundTo * round(value / t.roundTo); + if (value >= r.max + eps) { + break; + } t.ticks.push_back(makeTick(value)); + ++n; } } };
--- a/base/test/TestScaleTickIntervals.h Wed May 03 18:26:26 2017 +0100 +++ b/base/test/TestScaleTickIntervals.h Thu May 04 08:32:41 2017 +0100 @@ -290,6 +290,115 @@ compareTicks(ticks.ticks, expected); } + void linear_sqrt2_pi_7() + { + auto ticks = ScaleTickIntervals::linear({ sqrt(2.0), M_PI, 7 }); + // This would be better in steps of 0.25, but we only round to + // integral powers of ten + vector<ScaleTickIntervals::Tick> expected { + { 1.5, "1.5" }, + { 1.7, "1.7" }, + { 1.9, "1.9" }, + { 2.1, "2.1" }, + { 2.3, "2.3" }, + { 2.5, "2.5" }, + { 2.7, "2.7" }, + { 2.9, "2.9" }, + { 3.1, "3.1" }, + }; + compareTicks(ticks.ticks, expected); + } + + void linear_pi_avogadro_7() + { + auto ticks = ScaleTickIntervals::linear({ M_PI, 6.022140857e23, 7 }); + vector<ScaleTickIntervals::Tick> expected { + { 1e+21, "1.0e+21" }, + { 8.7e+22, "8.7e+22" }, + { 1.73e+23, "1.73e+23" }, + { 2.59e+23, "2.59e+23" }, + { 3.45e+23, "3.45e+23" }, + { 4.31e+23, "4.31e+23" }, + { 5.17e+23, "5.17e+23" }, + }; + compareTicks(ticks.ticks, expected); + } + + void linear_2_3_1() + { + auto ticks = ScaleTickIntervals::linear({ 2, 3, 1 }); + vector<ScaleTickIntervals::Tick> expected { + { 2.0, "2.0" }, + { 3.0, "3.0" } + }; + compareTicks(ticks.ticks, expected); + } + + void linear_2_3_2() + { + auto ticks = ScaleTickIntervals::linear({ 2, 3, 2 }); + vector<ScaleTickIntervals::Tick> expected { + { 2.0, "2.0" }, + { 2.5, "2.5" }, + { 3.0, "3.0" } + }; + compareTicks(ticks.ticks, expected); + } + + void linear_2_3_3() + { + auto ticks = ScaleTickIntervals::linear({ 2, 3, 3 }); + vector<ScaleTickIntervals::Tick> expected { + { 2.0, "2.0" }, + { 2.3, "2.3" }, + { 2.6, "2.6" }, + { 2.9, "2.9" } + }; + compareTicks(ticks.ticks, expected); + } + + void linear_2_3_4() + { + auto ticks = ScaleTickIntervals::linear({ 2, 3, 4 }); + // This would be better in steps of 0.25, but we only round to + // integral powers of ten + vector<ScaleTickIntervals::Tick> expected { + { 2.0, "2.0" }, + { 2.3, "2.3" }, + { 2.6, "2.6" }, + { 2.9, "2.9" } + }; + compareTicks(ticks.ticks, expected); + } + + void linear_2_3_5() + { + auto ticks = ScaleTickIntervals::linear({ 2, 3, 5 }); + vector<ScaleTickIntervals::Tick> expected { + { 2.0, "2.0" }, + { 2.2, "2.2" }, + { 2.4, "2.4" }, + { 2.6, "2.6" }, + { 2.8, "2.8" }, + { 3.0, "3.0" } + }; + compareTicks(ticks.ticks, expected); + } + + void linear_2_3_6() + { + auto ticks = ScaleTickIntervals::linear({ 2, 3, 6 }); + vector<ScaleTickIntervals::Tick> expected { + { 2.0, "2.0" }, + { 2.2, "2.2" }, + { 2.4, "2.4" }, + { 2.6, "2.6" }, + { 2.8, "2.8" }, + { 3.0, "3.0" } + }; + compareTicks(ticks.ticks, expected); + } + void linear_1_1_10() { auto ticks = ScaleTickIntervals::linear({ 1, 1, 10 });