-
-
Notifications
You must be signed in to change notification settings - Fork 213
Fix issues and inconsistency with draw_line_width #1998
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
MightyJosip
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is good now. If you would like to do it, can you also add the tests for this
MyreMylar
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM 👍
Fixes the original issue that was visible in this test program:
import pygame
WIDTH, HEIGHT = 800, 600
FPS = 60
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
clock = pygame.time.Clock()
test_surf = pygame.Surface((100, 100))
test_surf.fill((255, 255, 255))
rect = pygame.Rect(0, 0, 100, 100)
test_surf_pos = (150, 150)
running = True
while running:
clock.tick(FPS)
screen.fill("black")
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
running = False
test_surf.fill("white")
rect.center = pygame.mouse.get_pos()[0] - test_surf_pos[0], pygame.mouse.get_pos()[1] - test_surf_pos[0]
pygame.draw.rect(test_surf, "red", rect, 30, 0, 0, 20, 0, 20)
screen.blit(test_surf, test_surf_pos)
pygame.display.flip()
Try moving the D shape around inside the white surface with the mouse. On main you get lots of artefacts when parts of the D are clipped off screen and parts, which should still be visible get culled. After this PR everything looks as it should.
|
I tested this and confirm to be faster with this PR: OLD Mean: 0.34615143499993795 Code: from random import randint
import pygame
from pygame.draw import line as draw_line
from timeit import repeat
from statistics import mean
pygame.init()
WIDTH, HEIGHT = 400, 400
win = pygame.display.set_mode((WIDTH, HEIGHT))
def rand_color():
return randint(0, 255), randint(0, 255), randint(0, 255)
def create_lines(number, width):
return [(rand_color(),
(randint(0, WIDTH), randint(0, HEIGHT)),
(randint(0, WIDTH), randint(0, HEIGHT)), width)
for _ in range(number)]
lst = create_lines(100, 3)
for _ in range(10):
print(mean(repeat("for line in lst: draw_line(win, *line)", globals=globals(), number=1000, repeat=10))) |
This change resolves issues #1968 and pygame/pygame#3199 through a rewrite of the draw_line_width function. Drawing lines from a to b should now also be the same as drawing from b to a.