pygame-community / pygame-ce

🐍🎮 pygame - Community Edition is a FOSS Python library for multimedia applications (like games). Built on top of the excellent SDL library.
https://pyga.me
939 stars 155 forks source link

Fix thick draw.aaline with float coordinates #3159

Closed mzivic7 closed 1 month ago

mzivic7 commented 1 month ago

draw.aaline with width argument has 2 problems: First: I used floats to detect steepines, but draw.line converts values to ints before math. That's why added aalines, when width > 1, are drawn at wrong position. (left on the image) Second: I used floats for original line coordinates to calculate positions for antialiased lines. But draw.line converts values to ints, so antialiased lines got a little too far and created gaps. (right on the image) badcode

Sample code ```py 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((0, 0, 0)) pygame.draw.aaline(screen, "white", (140, 259.93), (150, 269.47), 20) pygame.draw.aaline(screen, "white", (170, 259.93), (190, 269.47), 20) pygame.display.flip() clock.tick(60) pygame.quit() ```
bilhox commented 1 month ago

Looks correct for me. Good job !

mzivic7 commented 1 month ago

Because this implementation uses Bresenham's algorithm for drawing thick lines, it does not support float values for coordinates. New PR will be open with new implementation, using modified Xiaolin Wu's algorithm.