From b9f207ce4cb0a0470e73edbd2807b043b8611627 Mon Sep 17 00:00:00 2001 From: "R. Aidan Campbell" Date: Wed, 27 Dec 2017 12:35:26 -0500 Subject: [PATCH] Implement `line` arguments and order matches the C implementation here: https://github.com/micropython/micropython/blob/master/extmod/modframebuf.c#L408 --- framebuf.py | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/framebuf.py b/framebuf.py index fc8fd61..b07097d 100755 --- a/framebuf.py +++ b/framebuf.py @@ -107,9 +107,33 @@ def rect(self, x, y, width, height, color): self.fill_rect(self, x, y, 1, height, color) self.fill_rect(self, x+width, y, 1, height, color) - def line(self): - raise NotImplementedError() - + def line(self, x0, y0, x1, y1, color): + """Bresenham's line algorithm""" + dx = abs(x1 - x0) + dy = abs(y1 - y0) + x, y = x0, y0 + sx = -1 if x0 > x1 else 1 + sy = -1 if y0 > y1 else 1 + if dx > dy: + err = dx / 2.0 + while x != x1: + self.pixel(x,y,color) + err -= dy + if err < 0: + y += sy + err += dx + x += sx + else: + err = dy / 2.0 + while y != y1: + self.pixel(x,y,1) + err -= dx + if err < 0: + x += sx + err += dy + y += sy + self.pixel(x,y,1) + def blit(self): raise NotImplementedError()