Skip to content

Commit 7f28ae0

Browse files
committed
gridify: round from zero
python3 round() differs from python2's in a way that improves its statistical distribution (round to even), but results in undesirable visual effects when dealing with 0.5s in a grid context.
1 parent 6581d34 commit 7f28ae0

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

kipart/kipart.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,11 @@ def gridify(value, grid_spacing=GRID_SPACING, policy="round"):
122122
elif policy == "down":
123123
return math.floor(value / grid_spacing) * grid_spacing
124124
elif policy == "round":
125-
return round(value / grid_spacing) * grid_spacing
125+
# Round from zero (round() does round-to-even which looks bad)
126+
if value > 0:
127+
return math.floor(value / grid_spacing + 0.5) * grid_spacing
128+
else:
129+
return math.ceil(value / grid_spacing - 0.5) * grid_spacing
126130
else:
127131
raise ValueError(
128132
f"Invalid gridify policy '{policy}'. Use 'up', 'down', or 'round'."

0 commit comments

Comments
 (0)