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 Circle contains() #2791

Merged
merged 7 commits into from
May 25, 2024
Merged

Conversation

itzpr3d4t0r
Copy link
Member

@itzpr3d4t0r itzpr3d4t0r commented Apr 5, 2024

This PR adds the Circle.contains() method for checking when a shape (Rect/Frect/Circle for now) or point falls completely inside the circle.

I have also made a ltl program to test stuff, feel free to use it as you wish:

import pygame
from pygame.draw import circle as draw_circle, rect as draw_rect
from pygame.geometry import Circle


pygame.init()


screen = pygame.display.set_mode((800, 800))
pygame.display.set_caption("Circle contains test")

running = True
c = Circle(400, 400, 200)

r = pygame.Rect(100, 100, 200, 200)
fr = pygame.Rect(100, 100, 200, 200)
c2 = Circle(400, 400, 100)

C_T = (0, 255, 0)
C_F = (255, 0, 0)

font = pygame.font.SysFont("Arial", 25, True)

shape_ix = 0

shapes = {
    0: [c2, font.render("Circle", True, (255, 255, 255))],
    1: [r, font.render("Rect", True, (255, 255, 255))],
    2: [fr, font.render("FRect", True, (255, 255, 255))],
}
info_text = font.render("mouse wheel to change shape", True, (255, 255, 255))

clock = pygame.Clock()

while running:
    clock.tick(60)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.MOUSEWHEEL:
            if event.y > 0:
                shape_ix = (shape_ix + 1) % len(shapes)
            else:
                shape_ix = (shape_ix - 1) % len(shapes)

    shape, text = shapes[shape_ix]
    if c.contains(shape):
        color = C_T
    else:
        color = C_F

    shape.center = pygame.mouse.get_pos()

    screen.fill(0)

    draw_circle(screen, color, c.center, c.radius, 1)

    if isinstance(shape, pygame.Rect):
        draw_rect(screen, color, shape, 1)
    elif isinstance(shape, pygame.geometry.Circle):
        draw_circle(screen, color, shape.center, shape.radius, 1)

    screen.blit(text, text.get_rect(center=(400, 60)))
    screen.blit(info_text, info_text.get_rect(center=(400, 30)))

    pygame.display.flip()


pygame.quit()

Credits

Geometry Project:
For code, docs and tests:
@novialriptide @Emc2356 @itzpr3d4t0r @ScriptLineStudios @avaxar @Matiiss @newpaxonian @maqa41 @blankRiot96
Also thanks to @Starbuck5 for kickstarting the idea and the occasional help!

Functionality added in this PR

@itzpr3d4t0r itzpr3d4t0r added New API This pull request may need extra debate as it adds a new class or function to pygame geometry pygame.geometry labels Apr 5, 2024
@itzpr3d4t0r itzpr3d4t0r requested a review from a team as a code owner April 5, 2024 12:15
@itzpr3d4t0r itzpr3d4t0r mentioned this pull request Apr 5, 2024
87 tasks
@ankith26 ankith26 added this to the 2.5.0 milestone Apr 9, 2024
src_c/circle.c Outdated Show resolved Hide resolved
@oddbookworm
Copy link
Member

Pull main to rerun CI
That sysfont lint fail should be fixed

itzpr3d4t0r and others added 5 commits May 21, 2024 00:05
Co-authored-by: Emc2356 <63981925+emc2356@users.noreply.github.com>
Co-authored-by: NovialRiptide <35881688+novialriptide@users.noreply.github.com>
Co-authored-by: ScriptLineStudios <scriptlinestudios@protonmail.com>
Co-authored-by: Avaxar <44055981+avaxar@users.noreply.github.com>
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

@oddbookworm oddbookworm left a comment

Choose a reason for hiding this comment

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

This almost looks ready to merge IMO. I just have a few small comments, 3 of which are just for making the tests slightly more robust

src_c/circle.c Outdated Show resolved Hide resolved
test/geometry_test.py Show resolved Hide resolved
test/geometry_test.py Show resolved Hide resolved
test/geometry_test.py Show resolved Hide resolved
Copy link
Member

@MyreMylar MyreMylar left a comment

Choose a reason for hiding this comment

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

OK, LGTM 👍

@MyreMylar MyreMylar merged commit 3388ec9 into pygame-community:main May 25, 2024
39 checks passed
@itzpr3d4t0r itzpr3d4t0r deleted the circle-contains branch May 25, 2024 16:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
geometry pygame.geometry New API This pull request may need extra debate as it adds a new class or function to pygame
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants