Skip to content
This repository was archived by the owner on Dec 22, 2018. It is now read-only.

Commit ca231e8

Browse files
authored
Merge pull request #3 from raidancampbell/patch-1
Implement `line`
2 parents 2d8f737 + b9f207c commit ca231e8

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

framebuf.py

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,33 @@ def rect(self, x, y, width, height, color):
107107
self.fill_rect(self, x, y, 1, height, color)
108108
self.fill_rect(self, x+width, y, 1, height, color)
109109

110-
def line(self):
111-
raise NotImplementedError()
112-
110+
def line(self, x0, y0, x1, y1, color):
111+
"""Bresenham's line algorithm"""
112+
dx = abs(x1 - x0)
113+
dy = abs(y1 - y0)
114+
x, y = x0, y0
115+
sx = -1 if x0 > x1 else 1
116+
sy = -1 if y0 > y1 else 1
117+
if dx > dy:
118+
err = dx / 2.0
119+
while x != x1:
120+
self.pixel(x,y,color)
121+
err -= dy
122+
if err < 0:
123+
y += sy
124+
err += dx
125+
x += sx
126+
else:
127+
err = dy / 2.0
128+
while y != y1:
129+
self.pixel(x,y,1)
130+
err -= dx
131+
if err < 0:
132+
x += sx
133+
err += dy
134+
y += sy
135+
self.pixel(x,y,1)
136+
113137
def blit(self):
114138
raise NotImplementedError()
115139

0 commit comments

Comments
 (0)