Skip to content
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

Add warning when using negative rect dimensions in draw module #3158

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

mzivic7
Copy link
Contributor

@mzivic7 mzivic7 commented Oct 9, 2024

Using negative rect dimension values have some undesirable results as shown in #2727.
This PR adds deprecation warning to these draw functions: draw.rect, draw.ellipse, draw.arc when using negative value for rect width and height.
And prevents anything from being drawn at wrong position.
Warnings are also added to docs and tests.
This warning will also be added to draw.aaellipse in #3016

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((0, 0, 0))
  pygame.draw.rect(screen, "white", (150, 150, -50, 30), 0)
  pygame.draw.ellipse(screen, "white", (150, 150, -50, 30), 0)
  pygame.draw.arc(screen, "white", (150, 150, -50, 30), 0, 180, 0)
  pygame.display.flip()
  clock.tick(60)
pygame.quit()

Added warning in docs
Added test for this warning
@mzivic7 mzivic7 requested a review from a team as a code owner October 9, 2024 23:28
src_c/draw.c Outdated Show resolved Hide resolved
@yunline yunline added Code quality/robustness Code quality and resilience to changes draw pygame.draw labels Oct 10, 2024
Copy link
Contributor

@bilhox bilhox left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems fair to me, LGTM !

@ankith26
Copy link
Member

while thinking about this, I realised that some other draw API also takes in rect style arguments. How are negative dims handled everywhere else. Whatever fix we do, we should try to be consistent with the approach everywhere

@mzivic7 mzivic7 changed the title Add warning when using negative dimensions in draw.ellipse Add warning when using negative rect dimensions in draw module Oct 12, 2024
@mzivic7
Copy link
Contributor Author

mzivic7 commented Oct 12, 2024

I added same warning, docs and tests to draw.rect and draw.arc

@bilhox bilhox added this to the 2.5.3 milestone Oct 13, 2024
@mzivic7 mzivic7 mentioned this pull request Oct 8, 2024
19 tasks
Copy link
Member

@ankith26 ankith26 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you make all the 2.5.2 -> 2.5.3 in this PR?

@mzivic7
Copy link
Contributor Author

mzivic7 commented Nov 2, 2024

Sure, here it comes.

Copy link
Member

@ankith26 ankith26 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks for the PR 🎉

Copy link
Member

@Starbuck5 Starbuck5 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not convinced this is the thing to do.

If it's a problem have the algorithm normalize the rect first?

@Starbuck5
Copy link
Member

Did some testing,

  • pygame.draw.arc: Seems to not draw if the rect dimensions are negative
  • pygame.draw.ellipse: Described in original issue, but my testing TLDR is that negative rect widths are drawn, negative rect heights are not, and negative rect widths are always drawn filled for some reason.
  • pygame.draw.rect: With or without outline nothing is drawn with negative dimensions
  • pygame.draw.rect (rounded corners): Interestingly rects with rounded corners draw with negative dimensions fine.

I still think the most straightforward way to fix this is normalize the rect arguments when they come in. (https://pyga.me/docs/ref/rect.html#pygame.Rect.normalize)

@mzivic7
Copy link
Contributor Author

mzivic7 commented Nov 3, 2024

Why is normalizing input rects bad:

  • Arc can be non-symmetrical, so if its rect is normalized, should it be mirrored?
  • Normalize function would have no purpose, because every rect input is normalized internally.
  • One drags right side of rect over left side and get negative width, and he want to calculate area or perimeter, but it turns out to be wrong, so he then has to call normalize() to get correct area or use few abs().
  • Enforced normalization would only add more cpu load, those few who needs this functionality just have to use normalize(), and probably already do.

So I will remove warning and only disable drawing if rect width/height is negative.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Code quality/robustness Code quality and resilience to changes draw pygame.draw
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants