pygame-ce icon indicating copy to clipboard operation
pygame-ce copied to clipboard

Add draw.aaellipse

Open mzivic7 opened this issue 1 year ago โ€ข 5 comments
trafficstars

This PR adds draw.aaellipse function, with width and thickness options. It is completely interchangeable with draw.ellipse in terms of arguments. Algorithm used is adapted from Xiaolin Wu's general fast antialiasing algorithm, and is very similar to draw.aacircle. Only notable difference: in aacircle, 8 pixels are drawn at once, but here, only 4, because ellipse has 2 symmetries. Because of that aaelipse has additional vertical drawing (aacircle has only horizontal but because of extra symmetries it is vertical at the same time). It is added to docs too.

Sample code
import pygame

pygame.init()
screen = pygame.display.set_mode((512, 512))
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.aaellipse(screen, "red", [100, 100, 100, 50], 1)
    pygame.draw.aaellipse(screen, "red", [100, 160, 100, 50])
    pygame.draw.aaellipse(screen, "red", [100, 220, 100, 50], 4)
    pygame.display.flip()
    clock.tick(60)
pygame.quit()

mzivic7 avatar Jul 22 '24 18:07 mzivic7

Note: before merging, correct versionadded in docs/reST/ref/draw.rst, if needed.

mzivic7 avatar Oct 05 '24 17:10 mzivic7

I moved ellipse and aaellipse functions bellow circle and aacircle, because aacircle is called in aaellipse and aa* functions should always be bellow regular one.

mzivic7 avatar Nov 09 '24 17:11 mzivic7

I see holes around the edges of the ellipse in this test case.

import pygame
screen = pygame.display.set_mode((600,600))
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.KEYDOWN and event.key == pygame.K_w:
            pygame.image.save(screen, "output.png")
    screen.fill("purple")
    pygame.draw.aaellipse(screen, "red", [100,200,300,400])
    pygame.display.flip()
pygame.quit()

output

Starbuck5 avatar Nov 11 '24 07:11 Starbuck5

Other things I see.

  • [x] pygame.draw.aaellipse(screen, "red", [100,200,30,200000], width=0) crashes the app
  • [x] putting in a rect with negative dimensions leads to weird stuff being drawn
  • [x] if the width and the height are equal the surface being drawn on cannot be blitted in the future (you forgot to unlock it in this case)

Starbuck5 avatar Nov 11 '24 08:11 Starbuck5

๐Ÿ“ Walkthrough

Walkthrough

Adds a new public API pygame.draw.aaellipse, implements antialiased ellipse/circle rendering in C using Xiaolin Wu variants, updates bindings and stubs, updates an example, and extends tests to cover aaellipse parity with ellipse.

Changes

Cohort / File(s) Summary
Public stub / API declaration
buildconfig/stubs/pygame/draw.pyi
Adds aaellipse(surface: Surface, color: ColorLike, rect: RectLike, width: int = 0) -> Rect declaration (versionadded 2.5.6).
C implementation & bindings
src_c/draw.c
Adds aaellipse Python entrypoint and docstring; introduces Xiaolin Wu AA ellipse/circle routines (draw_aaellipse_xiaolinwu, _thin), renames circle Wu paths, adds symmetric-pixel helpers (draw_four_symmetric_pixels, draw_eight_symmetric_pixels, swap_int), updates clipping/bounds and 64-bit-safe arithmetic, routes square rects to aacircle, and registers method in _draw_methods.
Documentation example
docs/reST/ref/code_examples/draw_module_example.py
Adjusts example ellipse x positions (+10) and adds two pygame.draw.aaellipse(...) examples (outlined and filled).
Tests
test/draw_test.py
Adds draw_aaellipse alias, DrawAAEllipseMixin and DrawAAEllipseTest, and extensive tests mirroring ellipse coverage for aaellipse (args, keywords, color formats, widths, rect formats, bounds, edge/off-screen cases).

Sequence Diagram(s)

sequenceDiagram
  autonumber
  participant App
  participant Draw as pygame.draw
  participant AA as aaellipse()
  participant Clip as Clip/Bounds
  participant Algo as Wu AA Ellipse/Circle
  participant Sym as Symmetric Helpers
  participant Surf as Surface

  App->>Draw: aaellipse(surface, color, rect, width?)
  Draw->>AA: parse args & kwargs

  AA->>Clip: validate rect, compute bounding box, clip
  Clip-->>AA: clipped bounds

  alt rect is square
    AA->>Algo: route to aacircle (thin if width==1)
  else rect is non-square
    AA->>Algo: AA ellipse (thin if width==1)
  end

  Algo->>Sym: compute symmetric pixel positions & coverage
  Sym->>Surf: blend/plot pixels
  Algo-->>AA: return bounding Rect
  AA-->>Draw: Rect
  Draw-->>App: Rect

Estimated code review effort

๐ŸŽฏ 4 (Complex) | โฑ๏ธ ~60 minutes

[!TIP]

๐Ÿ”Œ Remote MCP (Model Context Protocol) integration is now available!

Pro plan users can now connect to remote MCP servers from the Integrations page. Connect with popular remote MCPs such as Notion and Linear to add more context to your reviews and chats.

โœจ Finishing Touches
  • [ ] ๐Ÿ“ Generate Docstrings
๐Ÿงช Generate unit tests
  • [ ] Create PR with unit tests
  • [ ] Post copyable unit tests in a comment

๐Ÿชง Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

coderabbitai[bot] avatar Aug 26 '25 13:08 coderabbitai[bot]