mirror of
https://github.com/UpsilonNumworks/Upsilon.git
synced 2026-03-29 11:39:58 +02:00
[apps/graph/graph] In graphWindow, use constexpr to compute grid units
Change-Id: I4cb42e1eb13d6d16ffb7d6e373c6bf12d8ecbdbe
This commit is contained in:
@@ -51,23 +51,23 @@ float GraphWindow::yGridUnit() {
|
||||
void GraphWindow::setXMin(float xMin) {
|
||||
m_xMin = xMin;
|
||||
computeYaxes();
|
||||
computeXGridUnit();
|
||||
computeGridUnit(Axis::X);
|
||||
}
|
||||
|
||||
void GraphWindow::setXMax(float xMax) {
|
||||
m_xMax = xMax;
|
||||
computeYaxes();
|
||||
computeXGridUnit();
|
||||
computeGridUnit(Axis::X);
|
||||
}
|
||||
|
||||
void GraphWindow::setYMin(float yMin) {
|
||||
m_yMin = yMin;
|
||||
computeYGridUnit();
|
||||
computeGridUnit(Axis::Y);
|
||||
}
|
||||
|
||||
void GraphWindow::setYMax(float yMax) {
|
||||
m_yMax = yMax;
|
||||
computeYGridUnit();
|
||||
computeGridUnit(Axis::Y);
|
||||
}
|
||||
|
||||
void GraphWindow::setYAuto(bool yAuto) {
|
||||
@@ -104,7 +104,7 @@ bool GraphWindow::computeYaxes() {
|
||||
m_yMax = max + 1;
|
||||
}
|
||||
}
|
||||
computeYGridUnit();
|
||||
computeGridUnit(Axis::Y);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -123,11 +123,11 @@ void GraphWindow::zoom(float ratio) {
|
||||
float yMax = m_yMax;
|
||||
m_xMin = (xMax+xMin)/2.0f - ratio*fabsf(xMax-xMin);
|
||||
m_xMax = (xMax+xMin)/2.0f + ratio*fabsf(xMax-xMin);
|
||||
computeXGridUnit();
|
||||
computeGridUnit(Axis::X);
|
||||
m_yAuto = false;
|
||||
m_yMin = (yMax+yMin)/2.0f - ratio*fabsf(yMax-yMin);
|
||||
m_yMax = (yMax+yMin)/2.0f + ratio*fabsf(yMax-yMin);
|
||||
computeYGridUnit();
|
||||
computeGridUnit(Axis::Y);
|
||||
}
|
||||
|
||||
void GraphWindow::centerAxisAround(Axis axis, float position) {
|
||||
@@ -136,13 +136,13 @@ void GraphWindow::centerAxisAround(Axis axis, float position) {
|
||||
m_xMin = position - range/2.0f;
|
||||
m_xMax = position + range/2.0f;
|
||||
computeYaxes();
|
||||
computeXGridUnit();
|
||||
computeGridUnit(Axis::X);
|
||||
} else {
|
||||
m_yAuto = false;
|
||||
float range = m_yMax - m_yMin;
|
||||
m_yMin = position - range/2.0f;
|
||||
m_yMax = position + range/2.0f;
|
||||
computeYGridUnit();
|
||||
computeGridUnit(Axis::Y);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -151,23 +151,23 @@ void GraphWindow::translateWindow(Direction direction) {
|
||||
if (direction == Direction::Up) {
|
||||
m_yMin = m_yMin + m_yGridUnit;
|
||||
m_yMax = m_yMax + m_yGridUnit;
|
||||
computeYGridUnit();
|
||||
computeGridUnit(Axis::Y);
|
||||
}
|
||||
if (direction == Direction::Down) {
|
||||
m_yMin = m_yMin - m_yGridUnit;
|
||||
m_yMax = m_yMax - m_yGridUnit;
|
||||
computeYGridUnit();
|
||||
computeGridUnit(Axis::Y);
|
||||
}
|
||||
if (direction == Direction::Left) {
|
||||
m_xMin = m_xMin - m_xGridUnit;
|
||||
m_xMax = m_xMax - m_xGridUnit;
|
||||
computeXGridUnit();
|
||||
computeGridUnit(Axis::X);
|
||||
computeYaxes();
|
||||
}
|
||||
if (direction == Direction::Right) {
|
||||
m_xMin = m_xMin + m_xGridUnit;
|
||||
m_xMax = m_xMax + m_xGridUnit;
|
||||
computeXGridUnit();
|
||||
computeGridUnit(Axis::X);
|
||||
computeYaxes();
|
||||
}
|
||||
}
|
||||
@@ -175,11 +175,11 @@ void GraphWindow::translateWindow(Direction direction) {
|
||||
void GraphWindow::setTrigonometric() {
|
||||
m_xMin = -10.5f;
|
||||
m_xMax = 10.5f;
|
||||
computeXGridUnit();
|
||||
computeGridUnit(Axis::X);
|
||||
m_yAuto = false;
|
||||
m_yMin = -1.6f;
|
||||
m_yMax = 1.6f;
|
||||
computeYGridUnit();
|
||||
computeGridUnit(Axis::Y);
|
||||
}
|
||||
|
||||
void GraphWindow::roundAbscissa() {
|
||||
@@ -187,7 +187,7 @@ void GraphWindow::roundAbscissa() {
|
||||
float xMax = m_xMax;
|
||||
m_xMin = roundf((xMin+xMax)/2) - 160.0f;
|
||||
m_xMax = roundf((xMin+xMax)/2) + 159.0f;
|
||||
computeXGridUnit();
|
||||
computeGridUnit(Axis::X);
|
||||
computeYaxes();
|
||||
}
|
||||
|
||||
@@ -198,17 +198,17 @@ void GraphWindow::normalize() {
|
||||
float yMax = m_yMax;
|
||||
m_xMin = (xMin+xMax)/2 - 5.3f;
|
||||
m_xMax = (xMin+xMax)/2 + 5.3f;
|
||||
computeXGridUnit();
|
||||
computeGridUnit(Axis::X);
|
||||
m_yAuto = false;
|
||||
m_yMin = (yMin+yMax)/2 - 3.1f;
|
||||
m_yMax = (yMin+yMax)/2 + 3.1f;
|
||||
computeYGridUnit();
|
||||
computeGridUnit(Axis::Y);
|
||||
}
|
||||
|
||||
void GraphWindow::setDefault() {
|
||||
m_xMin = -10.0f;
|
||||
m_xMax = 10.0f;
|
||||
computeXGridUnit();
|
||||
computeGridUnit(Axis::X);
|
||||
setYAuto(true);
|
||||
}
|
||||
|
||||
@@ -218,63 +218,51 @@ void GraphWindow::panToMakePointVisible(float x, float y, float xMargin, float y
|
||||
if (x < m_xMin + xMargin) {
|
||||
m_xMin = x - xMargin;
|
||||
m_xMax = m_xMin + xRange;
|
||||
computeXGridUnit();
|
||||
computeGridUnit(Axis::X);
|
||||
computeYaxes();
|
||||
}
|
||||
if (x > m_xMax - xMargin) {
|
||||
m_xMax = x + xMargin;
|
||||
m_xMin = m_xMax - xRange;
|
||||
computeXGridUnit();
|
||||
computeGridUnit(Axis::X);
|
||||
computeYaxes();
|
||||
}
|
||||
if (y < m_yMin + yMargin) {
|
||||
m_yMin = y - yMargin;
|
||||
m_yMax = m_yMin + yRange;
|
||||
computeYGridUnit();
|
||||
computeGridUnit(Axis::Y);
|
||||
}
|
||||
if (y > m_yMax - yMargin) {
|
||||
m_yMax = y + yMargin;
|
||||
m_yMin = m_yMax - yRange;
|
||||
computeYGridUnit();
|
||||
computeGridUnit(Axis::Y);
|
||||
}
|
||||
}
|
||||
|
||||
void GraphWindow::computeXGridUnit() {
|
||||
void GraphWindow::computeGridUnit(Axis axis) {
|
||||
int a = 0;
|
||||
int b = 0;
|
||||
float d = m_xMax - m_xMin;
|
||||
if (floorf(log10f(d/90.0f)) != floorf(log10f(d/35.0f))) {
|
||||
b = floorf(log10f(d/35.0f));
|
||||
a = 5;
|
||||
float maxNumberOfUnits = k_maxNumberOfXGridUnits;
|
||||
float minNumberOfUnits = k_minNumberOfXGridUnits;
|
||||
if (axis == Axis::Y) {
|
||||
d = m_yMax - m_yMin;
|
||||
maxNumberOfUnits = k_maxNumberOfYGridUnits;
|
||||
minNumberOfUnits = k_minNumberOfYGridUnits;
|
||||
}
|
||||
if (floorf(log10f(d/36.0f)) != floorf(log10f(d/14.0f))) {
|
||||
b = floorf(log10f(d/14.0f));
|
||||
a = 2;
|
||||
float units[3] = {k_oneUnit, k_twoUnit, k_fiveUnit};
|
||||
for (int k = 0; k < 3; k++) {
|
||||
float unit = units[k];
|
||||
if (floorf(log10f(d/(unit*maxNumberOfUnits))) != floorf(log10f(d/(unit*minNumberOfUnits)))) {
|
||||
b = floorf(log10f(d/(unit*minNumberOfUnits)));
|
||||
a = unit;
|
||||
}
|
||||
}
|
||||
if (floorf(log10f(d/18.0f)) != floorf(log10f(d/7.0f))) {
|
||||
b = floorf(log10f(d/7.0f));
|
||||
a = 1;
|
||||
if (axis == Axis::X) {
|
||||
m_xGridUnit = a*powf(10,b);
|
||||
} else {
|
||||
m_yGridUnit = a*powf(10,b);
|
||||
}
|
||||
m_xGridUnit = a*powf(10,b);
|
||||
}
|
||||
|
||||
void GraphWindow::computeYGridUnit() {
|
||||
int a = 0;
|
||||
int b = 0;
|
||||
float d = m_yMax - m_yMin;
|
||||
if (floorf(log10f(d/65.0f)) != floorf(log10f(d/25.0f))) {
|
||||
b = floorf(log10f(d/25.0f));
|
||||
a = 5;
|
||||
}
|
||||
if (floorf(log10f(d/26.0f)) != floorf(log10f(d/10.0f))) {
|
||||
b = floorf(log10f(d/10.0f));
|
||||
a = 2;
|
||||
}
|
||||
if (floorf(log10f(d/13.0f)) != floorf(log10f(d/5.0f))) {
|
||||
b = floorf(log10f(d/5.0f));
|
||||
a = 1;
|
||||
}
|
||||
m_yGridUnit = a*powf(10,b);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -45,8 +45,14 @@ public:
|
||||
void setDefault();
|
||||
void panToMakePointVisible(float x, float y, float xMargin, float yMargin);
|
||||
private:
|
||||
void computeXGridUnit();
|
||||
void computeYGridUnit();
|
||||
constexpr static float k_minNumberOfXGridUnits = 7.0f;
|
||||
constexpr static float k_maxNumberOfXGridUnits = 18.0f;
|
||||
constexpr static float k_minNumberOfYGridUnits = 5.0f;
|
||||
constexpr static float k_maxNumberOfYGridUnits = 13.0f;
|
||||
constexpr static float k_oneUnit = 1.0f;
|
||||
constexpr static float k_twoUnit = 2.0f;
|
||||
constexpr static float k_fiveUnit = 5.0f;
|
||||
void computeGridUnit(Axis axis);
|
||||
float m_xMin;
|
||||
float m_xMax;
|
||||
float m_yMin;
|
||||
|
||||
Reference in New Issue
Block a user