# HG changeset patch # User Chris Cannam # Date 1493883161 -3600 # Node ID b7a9edee85e0a66cfa119c912dbf6bc1679d5753 # Parent 1f0d071e7ce6b6b354754328a52ec3c6c9aea469 Change loop to something that feels more correct, though it makes no difference to the tests here. More tests, one failing. diff -r 1f0d071e7ce6 -r b7a9edee85e0 base/ScaleTickIntervals.h --- 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 #include -//#define DEBUG_SCALE_TICK_INTERVALS 1 +#define DEBUG_SCALE_TICK_INTERVALS 1 #ifdef DEBUG_SCALE_TICK_INTERVALS #include @@ -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; } } }; diff -r 1f0d071e7ce6 -r b7a9edee85e0 base/test/TestScaleTickIntervals.h --- 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 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 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 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 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 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 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 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 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 });