Skip to content

Commit

Permalink
Fix DrawRect utility function endpoint bug
Browse files Browse the repository at this point in the history
  • Loading branch information
ty-porter committed Mar 17, 2024
1 parent 42085ee commit 22c7859
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
8 changes: 4 additions & 4 deletions rewrite/screens/components/out.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from driver import graphics

from utils.graphics import DrawRect

class Out:
Expand All @@ -17,15 +15,17 @@ def render(self):
self.__render_outline(coords, color)


# TODO: The size coordinates for these boxes are off-by-one because they fail to account for endpoints.
# i.e. `size` config of 2 renders a 3x3 box instead of 2x2 because endpoints for graphics.DrawLine are inclusive.
def __render_outline(self, coords, color):
x, y, size = coords.x, coords.y, coords.size

DrawRect(self.canvas, x, y, size, size, color, filled=False)
DrawRect(self.canvas, x, y, size + 1, size + 1, color, filled=False)

def __render_out(self, coords, color):
x, y, size = coords.x, coords.y, coords.size

DrawRect(self.canvas, x, y, size, size, color, filled=True)
DrawRect(self.canvas, x, y, size + 1, size + 1, color, filled=True)

@property
def game(self):
Expand Down
12 changes: 8 additions & 4 deletions rewrite/utils/graphics.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@


def DrawRect(canvas, x, y, width, height, color, filled=True):
# Account for graphics.DrawLine rendering endpoints inclusive
width -= 1
height -= 1

if filled:
_DrawFilledRect(canvas, x, y, width, height, color)
else:
Expand All @@ -14,11 +18,11 @@ def _DrawFilledRect(canvas, x, y, width, height, color):
Chooses the smallest dimension as the render direction to prevent extra draw calls.
"""
if width > height:
for offset in range(0, height):
graphics.DrawLine(canvas, x, y + offset, x + width - 1, y + offset, color)
for offset in range(0, height + 1):
graphics.DrawLine(canvas, x, y + offset, x + width, y + offset, color)
else:
for offset in range(0, width):
graphics.DrawLine(canvas, x + offset, y, x + offset, y + height - 1, color)
for offset in range(0, width + 1):
graphics.DrawLine(canvas, x + offset, y, x + offset, y + height, color)

def _DrawUnfilledRect(canvas, x, y, width, height, color):
# Top horizontal
Expand Down

0 comments on commit 22c7859

Please sign in to comment.