Skip to content

Commit

Permalink
makeDetailsMask() use ceil() (q1-2)
Browse files Browse the repository at this point in the history
  • Loading branch information
Explorer09 committed Feb 11, 2024
1 parent 0b63a7e commit ec21942
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
26 changes: 13 additions & 13 deletions Meter.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -513,26 +513,27 @@ 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) {
blanksAtEnd = (uint8_t)maxBlanks % 8;
} else if (new->nCellsPainted > new->topPoint) {
assert(new->nCellsPainted - new->topPoint < 1.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;
roundsDownInAscii = distance < 0.5;

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);

Expand Down Expand Up @@ -634,7 +635,6 @@ static void GraphMeterMode_computeColors(Meter* this, const GraphDrawContext* co

GraphColorComputeState restart = {
.valueSum = 0.0,
.topPoint = 0.0,
.nCellsPainted = 0,
.nItemsPainted = 0
};
Expand Down Expand Up @@ -689,7 +689,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;
Expand Down Expand Up @@ -732,7 +731,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) {
Expand Down Expand Up @@ -814,8 +813,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);
Expand Down
1 change: 0 additions & 1 deletion Meter.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit ec21942

Please sign in to comment.