From fb03164e9f0a5318d1472db2983c4e8c36b38fd9 Mon Sep 17 00:00:00 2001 From: Ross Wilson Date: Tue, 17 May 2016 09:59:03 +0700 Subject: [PATCH] Implementing 'fake' display. --- pymlac/Display.py | 59 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 56 insertions(+), 3 deletions(-) diff --git a/pymlac/Display.py b/pymlac/Display.py index 530a781..6913b34 100644 --- a/pymlac/Display.py +++ b/pymlac/Display.py @@ -126,8 +126,13 @@ class Display(object): if self.dirty: self.write() - def bresenham(self, x1, y1, x2, y2): - """Draw a straight line on the graphics array.""" + def bresenham_orig(self, x1, y1, x2, y2): + """Draw a straight line on the graphics array. + + Only works for one octant. + """ + + log('bresenham_orig: (%d,%d) to (%d,%d)' % (x1, y1, x2, y2)) # algorithm from: # http://www.idav.ucdavis.edu/education/GraphicsNotes/Bresenhams-Algorithm.pdf @@ -155,8 +160,56 @@ class Display(object): for i in range(x1, x2): self.array[j*self.ScaleMaxX + i] = 1 + log('bresenham: setting (%d,%d), sigma=%s, dy=%s' % (i, j, str(sigma), str(dy))) if sigma >= 0: j += 1 sigma -= dx - i += 1 sigma += dy + + def bresenham(self, x1, y1, x2, y2): + log('bresenham: (%d,%d) to (%d,%d)' % (x1, y1, x2, y2)) + + dx = x2 - x1 + dy = y2 - y1 + + # Determine how steep the line is + is_steep = abs(dy) > abs(dx) + + # Rotate line + if is_steep: + x1, y1 = y1, x1 + x2, y2 = y2, x2 + + # Swap start and end points if necessary and store swap state + swapped = False + if x1 > x2: + x1, x2 = x2, x1 + y1, y2 = y2, y1 + swapped = True + + # Recalculate differentials + dx = x2 - x1 + dy = y2 - y1 + + # Calculate error + error = int(dx / 2.0) + ystep = 1 if y1 < y2 else -1 + + # Iterate over bounding box generating points between start and end + y = y1 + for x in range(x1, x2 + 1): + (x, y) = (y, x) if is_steep else (x, y) + self.array[y*self.ScaleMaxX + x] = 1 + log('bresenham: setting point (%d, %d)' % (x, y)) + error -= abs(dy) + if error < 0: + y += ystep + error += dx + + +if __name__ == '__main__': + d = Display() + for v in range(0, 1024, 10): + d.draw(v, 0, v, 1023) + d.draw(0, v, 1023, v) + d.close()