use the proprer line rendering algorithm

The proper Bresenham setup used by the fx-92 SC+.
This commit is contained in:
Lephe 2019-10-01 18:06:05 +02:00
parent cd2936c582
commit eaaee18bc7
Signed by: Lephenixnoir
GPG Key ID: 1BBA026E13FC0495
1 changed files with 31 additions and 2 deletions

View File

@ -76,11 +76,40 @@ class Window:
SDL_SetRenderDrawColor(self.r, *color)
SDL_RenderClear(self.r)
def line(self, x1, x2, y1, y2, color):
def line(self, x1, y1, x2, y2, color):
"""Draw a straight line."""
SDL_SetRenderDrawColor(self.r, *color)
SDL_RenderDrawLine(self.r, x1, x2, y1, y2)
# Bresenham line drawing algorithm
sgn = lambda x: -1 if x < 0 else (1 if x > 0 else 0)
x, y = x1, y1
dx, dy = x2 - x1, y2 - y1
sx, sy = sgn(dx), sgn(dy)
dx, dy = abs(dx), abs(dy)
cumul = 0
SDL_RenderDrawPoint(self.r, x, y)
if dx >= dy:
cumul = dx // 2
for i in range(dx):
x += sx
cumul += dy
if cumul > dx:
cumul -= dx
y += sy
SDL_RenderDrawPoint(self.r, x, y)
else:
cumul = dy // 2
for i in range(dy):
y += sy
cumul += dx
if cumul > dy:
cumul -= dy
x += sx
SDL_RenderDrawPoint(self.r, x, y)
def wait(self):
"""Wait for the window to be closed."""