1
0
mirror of https://github.com/rzzzwilson/pymlac.git synced 2025-06-10 09:32:41 +00:00

Implementing 'fake' display.

This commit is contained in:
Ross Wilson
2016-05-17 09:59:03 +07:00
parent 2afabb4ad4
commit fb03164e9f

View File

@@ -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()