From d54ab24d9728e09965e7ed04fa3502a8420ba748 Mon Sep 17 00:00:00 2001 From: Explorer09 Date: Thu, 21 Jan 2016 10:11:54 +0800 Subject: [PATCH 1/2] New macro GRAPH_HEIGHT for Graph Meter height (Cherry-picked from e93028d7fa0c5f00b5dc3336fd28abaf905cd572, the experimental graph coloring branch) Currently GRAPH_HEIGHT=4 . This prevents hard-coding the height of the graph meters, and allows user to change it at compile-time. --- Meter.c | 16 +++++++++------- Meter.h | 2 ++ 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/Meter.c b/Meter.c index 1159f5e5..444d8f60 100644 --- a/Meter.c +++ b/Meter.c @@ -25,6 +25,8 @@ in the source distribution for its full text. #define GRAPH_DELAY (DEFAULT_DELAY/2) +#define GRAPH_HEIGHT 4 /* Unit: rows (lines) */ + /*{ #include "ListItem.h" @@ -406,14 +408,14 @@ static void GraphMeterMode_draw(Meter* this, int x, int y, int w) { } for (int i = nValues - (w*2) + 2, k = 0; i < nValues; i+=2, k++) { - const double dot = (1.0 / (GraphMeterMode_pixPerRow * 4)); - int v1 = CLAMP(data->values[i] / dot, 1, GraphMeterMode_pixPerRow * 4); - int v2 = CLAMP(data->values[i+1] / dot, 1, GraphMeterMode_pixPerRow * 4); + const double dot = (1.0 / (GraphMeterMode_pixPerRow * GRAPH_HEIGHT)); + int v1 = CLAMP(data->values[i] / dot, 1, GraphMeterMode_pixPerRow * GRAPH_HEIGHT); + int v2 = CLAMP(data->values[i+1] / dot, 1, GraphMeterMode_pixPerRow * GRAPH_HEIGHT); int colorIdx = GRAPH_1; - for (int line = 0; line < 4; line++) { - int line1 = CLAMP(v1 - (GraphMeterMode_pixPerRow * (3 - line)), 0, GraphMeterMode_pixPerRow); - int line2 = CLAMP(v2 - (GraphMeterMode_pixPerRow * (3 - line)), 0, GraphMeterMode_pixPerRow); + for (int line = 0; line < GRAPH_HEIGHT; line++) { + int line1 = CLAMP(v1 - (GraphMeterMode_pixPerRow * (GRAPH_HEIGHT - 1 - line)), 0, GraphMeterMode_pixPerRow); + int line2 = CLAMP(v2 - (GraphMeterMode_pixPerRow * (GRAPH_HEIGHT - 1 - line)), 0, GraphMeterMode_pixPerRow); attrset(CRT_colors[colorIdx]); mvaddstr(y+line, x+k, GraphMeterMode_dots[line1 * (GraphMeterMode_pixPerRow + 1) + line2]); @@ -501,7 +503,7 @@ static MeterMode TextMeterMode = { static MeterMode GraphMeterMode = { .uiName = "Graph", - .h = 4, + .h = GRAPH_HEIGHT, .draw = GraphMeterMode_draw, }; diff --git a/Meter.h b/Meter.h index 18a6b1e0..7b728eeb 100644 --- a/Meter.h +++ b/Meter.h @@ -13,6 +13,8 @@ in the source distribution for its full text. #define GRAPH_DELAY (DEFAULT_DELAY/2) +#define GRAPH_HEIGHT 4 /* Unit: rows (lines) */ + #include "ListItem.h" #include From 040613db3315aeb992208d30789807285316f3b9 Mon Sep 17 00:00:00 2001 From: Explorer09 Date: Thu, 21 Jan 2016 14:06:11 +0800 Subject: [PATCH 2/2] Change variable 'dot' to avoid division by reciprocal. (Cherry-picked from d56bcd8e0d8d6a177fc2e40db32fc73ea4588684, the experimental graph coloring branch) The variable 'dot' in GraphMeterMode_draw now means "maximum number of dots per value (column) in graph". The old meaning was "amount of value that is to be represented by a dot" and was always a fraction. Due to a limitation in floating point computing, if GRAPH_HEIGHT were not a power of 2, then rounding errors will occur on numbers like (1.0/3). (Currently GRAPH_HEIGHT is 4 and so no precision loss.) 'dot' was used as a divisor, and it's "division by a reciprocal". We change that to simple multiplication. --- Meter.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Meter.c b/Meter.c index 444d8f60..781d45c7 100644 --- a/Meter.c +++ b/Meter.c @@ -408,9 +408,9 @@ static void GraphMeterMode_draw(Meter* this, int x, int y, int w) { } for (int i = nValues - (w*2) + 2, k = 0; i < nValues; i+=2, k++) { - const double dot = (1.0 / (GraphMeterMode_pixPerRow * GRAPH_HEIGHT)); - int v1 = CLAMP(data->values[i] / dot, 1, GraphMeterMode_pixPerRow * GRAPH_HEIGHT); - int v2 = CLAMP(data->values[i+1] / dot, 1, GraphMeterMode_pixPerRow * GRAPH_HEIGHT); + int pix = GraphMeterMode_pixPerRow * GRAPH_HEIGHT; + int v1 = CLAMP(data->values[i] * pix, 1, pix); + int v2 = CLAMP(data->values[i+1] * pix, 1, pix); int colorIdx = GRAPH_1; for (int line = 0; line < GRAPH_HEIGHT; line++) {