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.intersect() #3071

Merged
merged 6 commits into from
Sep 15, 2024
Merged

Conversation

itzpr3d4t0r
Copy link
Member

@itzpr3d4t0r itzpr3d4t0r commented Aug 20, 2024

This PR adds the general purpose Circle.intersect() function. It's a function that finds all intersection points (as long as there's a finite number of them) between the circle and any other shape. Since this is a first implementation it only supports circle-circle intesection but it will support rects/lines/polygons in the future too.

A little demo to test this:

from pygame.geometry import Circle
import pygame

sw, sh = 600, 600
swh, shh = sw / 2, sh / 2
c = Circle((swh, shh), 100)

pygame.init()
screen = pygame.display.set_mode((sw, sh))
clock = pygame.time.Clock()
keep = True

c2 = Circle((swh + 50, shh + 50), 100)

angle = 1

while keep:
    clock.tick_busy_loop(60)

    screen.fill((0, 0, 0))

    c2.center = pygame.mouse.get_pos()

    pygame.draw.circle(screen, (255, 255, 255), c.center, c.r, 1)
    pygame.draw.circle(screen, (255, 255, 255), c2.center, c2.r, 1)

    for center in c.intersect(c2):
        pygame.draw.circle(screen, (255, 0, 0), center, 5)

    pygame.display.flip()

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            keep = False


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

Co-authored-by: Emc2356 <[email protected]>
Co-authored-by: NovialRiptide <[email protected]>
Co-authored-by: ScriptLineStudios <[email protected]>
Co-authored-by: Avaxar <[email protected]>
Co-authored-by: maqa41 <[email protected]>
@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 Aug 20, 2024
@itzpr3d4t0r itzpr3d4t0r requested a review from a team as a code owner August 20, 2024 16:23
@itzpr3d4t0r itzpr3d4t0r mentioned this pull request Aug 20, 2024
89 tasks
@damusss
Copy link
Member

damusss commented Aug 20, 2024

Any reason why it doesn't support Rects and FRects (as they exist already)?

@itzpr3d4t0r
Copy link
Member Author

Any reason why it doesn't support Rects and FRects (as they exist already)?

I did a direct port from the geometry repo, which doesn’t have rectangle intersection implemented yet. We agreed to implement features there first to ensure proper code attribution.

Copy link
Member

@damusss damusss left a comment

Choose a reason for hiding this comment

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

I tested locally and the intersection cases worked out great (as expected since this is a direct port), so the PR LGTM. One step closer for Circle to be stable (allowing the other shapes to be ported aswell)

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.

LGTM !

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 🎉

@ankith26 ankith26 added this to the 2.5.2 milestone Sep 15, 2024
@ankith26 ankith26 merged commit 15ee198 into pygame-community:main Sep 15, 2024
26 checks passed
@itzpr3d4t0r itzpr3d4t0r deleted the circle-intersect branch September 15, 2024 12:46
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.

4 participants