author | Francesco Abbate <francesco.bbt@gmail.com> | 2010年05月11日 20:08:05 +0000 |
---|---|---|
committer | Francesco Abbate <francesco.bbt@gmail.com> | 2010年05月11日 20:08:05 +0000 |
commit | 244e78a408b401e0dea14c335b52ef55d54036db (patch) | |
tree | 95a56a8a3d7629c6f685c032163fd20c741a9a7b /agg-plot/units.cpp | |
parent | 3f94da0fce219777cbe7e875fb38ec8788cef04b (diff) | |
download | gsl-shell-244e78a408b401e0dea14c335b52ef55d54036db.tar.gz |
-rw-r--r-- | agg-plot/units.cpp | 68 |
diff --git a/agg-plot/units.cpp b/agg-plot/units.cpp new file mode 100644 index 00000000..d2db402e --- /dev/null +++ b/agg-plot/units.cpp @@ -0,0 +1,68 @@ + +#include <stdio.h> +#include <math.h> + +#include "utils.h" +#include "units.h" + +void units::init(double yinf, double ysup, double spacefact) +{ + double del; + + if (ysup == yinf) + ysup = yinf + 1.0; + + del = (ysup - yinf) / spacefact; + + order = (int) floor(log10(del)); + + double expf = pow(10, order); + double delr = del / expf; + + if (5 <= delr) + m_major = 5; + else if (2 <= delr) + m_major = 2; + else + m_major = 1; + + m_inf = (int) floor(yinf / (m_major * expf) + 1e-5); + m_sup = (int) ceil (ysup / (m_major * expf) - 1e-5); + + nb_decimals = (order < 0 ? -order : 0); + + dmajor = m_major * expf; +} + +void units::mark_label (char *lab, unsigned size, int mark) const +{ + bool minus = (m_inf < 0); + int asup = (minus ? -m_inf : m_sup); + char fmt[16]; + + if (size < 16) + return; + + if (nb_decimals == 0) + { + snprintf (lab, size, "%d", int(mark * dmajor)); + lab[size-1] = '0円'; + } + else + { + int dec = (nb_decimals < 10 ? nb_decimals : 9); + int base = (int) floor(asup * dmajor); + int space = dec + (base > 0 ? (int)log10(base): 0) + 1 \ + + (minus ? 1 : 0) + 1; + snprintf (fmt, 16, "%%%i.%if", space, dec); + fmt[15] = '0円'; + snprintf (lab, size, fmt, mark * dmajor); + lab[size-1] = '0円'; + } +} + +double units::mark_scale (double x) +{ + double xinf = m_inf * dmajor, xsup = m_sup * dmajor; + return (x - xinf) / (xsup - xinf); +} |