pygame-ce
pygame-ce copied to clipboard
New draw.aaline width algorithm
trafficstars
This PR adds completely new implementation for draw.aaline width. Previous implementation (#3140) uses Bresenham's algorithm, and because of that, line cant be drawn at float coordinates (they are converted to ints instead).
New implementation uses modified Xiaolin Wu's algorithm that allows drawing at float coordinates, and is faster.
Old width implementation is removed for pgce 2.5.2 in #3192
How it works
Original draw_aaline code is used, heavily modified:
- 2 antialiased pixels (2 are drawn for each point) are moved, leaving gap between, the size of width
- that gap is filled either with horizontal or vertical lines, depending on steepines
- Same thing is done to both endpoints
- Clipping is also modified to support width
- Using
drawhorzlineclipboundingand newdrawvertlineclipboundingto handle special cases when drawing lines outside surface.
Sample code
import pygame
pygame.init()
screen = pygame.display.set_mode((300, 300))
clock = pygame.time.Clock()
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
screen.fill("black")
pygame.draw.aaline(screen, "white", (120, 100), (220, 160), 16)
pygame.draw.aaline(screen, "white", (180, 170), (100, 260), 16)
pygame.draw.aaline(screen, "white", (70.7, 240.2), (150.2, 150.6), 7)
pygame.display.flip()
clock.tick(60)
pygame.quit()