From 88a94c7d665fd3b8c6e181fa4b71ecd7a85c032b Mon Sep 17 00:00:00 2001 From: Explorer09 Date: Thu, 15 Feb 2024 14:52:33 +0800 Subject: [PATCH] makeDetailsMask() prevTopPoint arg (q20) --- Meter.c | 27 +++++++++++++-------------- Meter.h | 1 - 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/Meter.c b/Meter.c index ab2ec19550..8a25bbb25c 100644 --- a/Meter.c +++ b/Meter.c @@ -498,7 +498,7 @@ static void GraphMeterMode_addItemAdjStack(GraphColorAdjStack* stack, double sca stack->nItems++; } -static uint16_t GraphMeterMode_makeDetailsMask(const GraphColorComputeState* prev, const GraphColorComputeState* new, double rem, int blanksAtTopCell) { +static uint16_t GraphMeterMode_makeDetailsMask(const GraphColorComputeState* prev, const GraphColorComputeState* new, double prevTopPoint, double rem, int blanksAtTopCell) { assert(new->nCellsPainted > prev->nCellsPainted); assert(rem >= 0.0); assert(rem < 1.0); @@ -513,17 +513,15 @@ static uint16_t GraphMeterMode_makeDetailsMask(const GraphColorComputeState* pre assert(blanksAtTopCell < 8); blanksAtEnd = (uint8_t)blanksAtTopCell; roundsUpInAscii = true; - } else if (prev->nCellsPainted == 0 || prev->topPoint <= prev->nCellsPainted) { + } else if (prev->nCellsPainted == 0 || prevTopPoint <= prev->nCellsPainted || rem <= 0.0) { blanksAtEnd = (uint8_t)maxBlanks % 8; - } else if (new->nCellsPainted > new->topPoint) { - assert(new->nCellsPainted - new->topPoint < 1.0); - assert(rem > 0.0); + } else { // Unlike other conditions, this one rounds to nearest for visual reason. // In case of a tie, display the dot at lower position of the graph, // i.e. MSB of the "details" data. - double distance = new->topPoint - (new->nCellsPainted - 1); - distance = distance - rem * 0.5; + double distance = prevTopPoint - prev->nCellsPainted; + distance = distance + rem * 0.5; // Tiebreaking direction that may be needed in the ASCII display mode. roundsUpInAscii = distance > 0.5; @@ -531,9 +529,11 @@ static uint16_t GraphMeterMode_makeDetailsMask(const GraphColorComputeState* pre double fraction = (uint8_t)numDots % 2 == 0 ? 0.5 : 0.0; distance = ceil(distance * 8.0 - fraction); - blanksAtEnd = 8.0 - distance - (uint8_t)numDots / 2; - } else { - blanksAtEnd = 0; + if (8.0 - distance - (uint8_t)numDots / 2 > 0) { + blanksAtEnd = 8.0 - distance - (uint8_t)numDots / 2; + } else { + blanksAtEnd = 0; + } } assert(blanksAtEnd < 8); @@ -646,7 +646,6 @@ static void GraphMeterMode_computeColors(Meter* this, const GraphDrawContext* co GraphColorComputeState restart = { .valueSum = 0.0, - .topPoint = 0.0, .nCellsPainted = 0, .nItemsPainted = 0 }; @@ -701,7 +700,6 @@ static void GraphMeterMode_computeColors(Meter* this, const GraphDrawContext* co assert(new.valueSum < DBL_MAX || prev.valueSum + value >= DBL_MAX); } - new.topPoint = (new.valueSum / scaledTotal) * (double)(int)graphHeight; double area = (value / scaledTotal) * (double)(int)graphHeight; assert(area >= 0.0); // "area" can be 0.0 when the division underflows double rem = area; @@ -744,7 +742,7 @@ static void GraphMeterMode_computeColors(Meter* this, const GraphDrawContext* co assert(restart.nItemsPainted == prev.nItemsPainted); if (!rItemIsDetermined) { - stack.startPoint = new.topPoint; + stack.startPoint = (new.valueSum / scaledTotal) * (double)(int)graphHeight; rItemMinCells = nCells; rem = 0.0; } else if (rItemHasExtraCell) { @@ -826,8 +824,9 @@ static void GraphMeterMode_computeColors(Meter* this, const GraphDrawContext* co rem = (0.125 * dotAlignment); if (nCells > 0 && new.nCellsPainted <= nCellsToPaint) { + double prevTopPoint = (prev.valueSum / scaledTotal) * (double)(int)graphHeight; int blanksAtTopCellArg = (new.nCellsPainted == nCellsToPaint) ? (int)blanksAtTopCell : -1; - uint16_t mask = GraphMeterMode_makeDetailsMask(&prev, &new, rem, blanksAtTopCellArg); + uint16_t mask = GraphMeterMode_makeDetailsMask(&prev, &new, prevTopPoint, rem, blanksAtTopCellArg); GraphColorCell* cellsStart = &valueStart[firstCellIndex + (size_t)increment * prev.nCellsPainted]; GraphMeterMode_paintCellsForItem(cellsStart, increment, prev.nItemsPainted, nCells, mask); diff --git a/Meter.h b/Meter.h index 4b38956fda..b6d37eb11c 100644 --- a/Meter.h +++ b/Meter.h @@ -143,7 +143,6 @@ typedef struct GraphDrawContext_ { /* Used in GraphMeterMode_computeColors() subroutines only */ typedef struct GraphColorComputeState_ { double valueSum; - double topPoint; unsigned int nCellsPainted; uint8_t nItemsPainted; } GraphColorComputeState;