Skip to content

Add Circle collidepoint() #2536

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

Merged
merged 4 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions buildconfig/stubs/pygame/geometry.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ from typing import (
overload,
)

from pygame._common import Coordinate


class Circle:
x: float
y: float
Expand All @@ -16,5 +19,9 @@ class Circle:
def __init__(self, circle: Circle) -> None: ...
@overload
def __init__(self, obj_with_circle_attr) -> None: ...
@overload
def collidepoint(self, x: float, y: float) -> bool: ...
@overload
def collidepoint(self, point: Coordinate) -> bool: ...
def __copy__(self) -> Circle: ...
copy = __copy__
14 changes: 14 additions & 0 deletions docs/reST/ref/geometry.rst
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,20 @@

----

.. method:: collidepoint

| :sl:`test if a point is inside the circle`
| :sg:`collidepoint((x, y)) -> bool`
| :sg:`collidepoint(x, y) -> bool`
| :sg:`collidepoint(Vector2) -> bool`

The `collidepoint` method tests whether a given point is inside the `Circle`
(including the edge of the `Circle`). It takes a tuple of (x, y) coordinates, two
separate x and y coordinates, or a `Vector2` object as its argument, and returns
`True` if the point is inside the `Circle`, `False` otherwise.

.. ## Circle.collidepoint ##

.. method:: copy

| :sl:`returns a copy of the circle`
Expand Down
2 changes: 1 addition & 1 deletion src_c/_pygame.h
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ typedef enum {
#define PYGAMEAPI_PIXELARRAY_NUMSLOTS 2
#define PYGAMEAPI_COLOR_NUMSLOTS 5
#define PYGAMEAPI_MATH_NUMSLOTS 2
#define PYGAMEAPI_BASE_NUMSLOTS 26
#define PYGAMEAPI_BASE_NUMSLOTS 27
#define PYGAMEAPI_EVENT_NUMSLOTS 8
#define PYGAMEAPI_WINDOW_NUMSLOTS 1
#define PYGAMEAPI_GEOMETRY_NUMSLOTS 1
Expand Down
17 changes: 16 additions & 1 deletion src_c/base.c
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,20 @@ pg_TwoDoublesFromObj(PyObject *obj, double *val1, double *val2)
return 1;
}

static inline int
pg_TwoDoublesFromFastcallArgs(PyObject *const *args, Py_ssize_t nargs,
double *val1, double *val2)
{
if (nargs == 1 && pg_TwoDoublesFromObj(args[0], val1, val2)) {
return 1;
}
else if (nargs == 2 && pg_DoubleFromObj(args[0], val1) &&
pg_DoubleFromObj(args[1], val2)) {
return 1;
}
return 0;
}

static int
pg_UintFromObj(PyObject *obj, Uint32 *val)
{
Expand Down Expand Up @@ -2258,8 +2272,9 @@ MODINIT_DEFINE(base)
c_api[23] = pg_EnvShouldBlendAlphaSDL2;
c_api[24] = pg_DoubleFromObj;
c_api[25] = pg_TwoDoublesFromObj;
c_api[26] = pg_TwoDoublesFromFastcallArgs;

#define FILLED_SLOTS 26
#define FILLED_SLOTS 27

#if PYGAMEAPI_BASE_NUMSLOTS != FILLED_SLOTS
#error export slot count mismatch
Expand Down
17 changes: 17 additions & 0 deletions src_c/circle.c
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,24 @@ pg_circle_str(pgCircleObject *self)
return pg_circle_repr(self);
}

static PyObject *
pg_circle_collidepoint(pgCircleObject *self, PyObject *const *args,
Py_ssize_t nargs)
{
double px, py;

if (!pg_TwoDoublesFromFastcallArgs(args, nargs, &px, &py)) {
return RAISE(
PyExc_TypeError,
"Circle.collidepoint requires a point or PointLike object");
}

return PyBool_FromLong(pgCollision_CirclePoint(&self->circle, px, py));
}

static struct PyMethodDef pg_circle_methods[] = {
{"collidepoint", (PyCFunction)pg_circle_collidepoint, METH_FASTCALL,
DOC_CIRCLE_COLLIDEPOINT},
{"__copy__", (PyCFunction)pg_circle_copy, METH_NOARGS, DOC_CIRCLE_COPY},
{"copy", (PyCFunction)pg_circle_copy, METH_NOARGS, DOC_CIRCLE_COPY},
{NULL, NULL, 0, NULL}};
Expand Down
9 changes: 9 additions & 0 deletions src_c/collisions.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include "collisions.h"

static int
pgCollision_CirclePoint(pgCircleBase *circle, double Cx, double Cy)
{
double dx = circle->x - Cx;
double dy = circle->y - Cy;
return dx * dx + dy * dy <= circle->r * circle->r;
}
9 changes: 9 additions & 0 deletions src_c/collisions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef _PG_COLLISIONS_H
#define _PG_COLLISIONS_H

#include "geometry.h"

static int
pgCollision_CirclePoint(pgCircleBase *circle, double, double);

#endif /* ~_PG_COLLISIONS_H */
1 change: 1 addition & 0 deletions src_c/doc/geometry_doc.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
#define DOC_CIRCLE_X "x -> float\ncenter x coordinate of the circle"
#define DOC_CIRCLE_Y "y -> float\ncenter y coordinate of the circle"
#define DOC_CIRCLE_R "r -> float\nradius of the circle"
#define DOC_CIRCLE_COLLIDEPOINT "collidepoint((x, y)) -> bool\ncollidepoint(x, y) -> bool\ncollidepoint(Vector2) -> bool\ntest if a point is inside the circle"
#define DOC_CIRCLE_COPY "copy() -> Circle\nreturns a copy of the circle"
1 change: 1 addition & 0 deletions src_c/geometry.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "geometry.h"
#include "collisions.c"
#include "circle.c"

static PyMethodDef geometry_methods[] = {{NULL, NULL, 0, NULL}};
Expand Down
4 changes: 4 additions & 0 deletions src_c/include/_pygame.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ typedef struct pg_bufferinfo_s {
#define pg_TwoDoublesFromObj \
(*(int (*)(PyObject *, double *, double *))PYGAMEAPI_GET_SLOT(base, 25))

#define pg_TwoDoublesFromFastcallArgs \
(*(int (*)(PyObject *const *, Py_ssize_t, double *, \
double *))PYGAMEAPI_GET_SLOT(base, 26))

#define pg_UintFromObj \
(*(int (*)(PyObject *, Uint32 *))PYGAMEAPI_GET_SLOT(base, 8))

Expand Down
55 changes: 54 additions & 1 deletion test/geometry_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import unittest

from pygame import Vector2
from pygame import Vector2, Vector3

from pygame.geometry import Circle

Expand Down Expand Up @@ -206,6 +206,59 @@ def test_copy(self):
# check c2 is not c
self.assertIsNot(c_2, c)

def test_collidepoint_argtype(self):
"""tests if the function correctly handles incorrect types as parameters"""
invalid_types = (None, [], "1", (1,), Vector3(1, 1, 1), 1)

c = Circle(10, 10, 4)

for value in invalid_types:
with self.assertRaises(TypeError):
c.collidepoint(value)

def test_collidepoint_argnum(self):
c = Circle(10, 10, 4)
args = [tuple(range(x)) for x in range(3, 13)]

# no params
with self.assertRaises(TypeError):
c.collidepoint()

# too many params
for arg in args:
with self.assertRaises(TypeError):
c.collidepoint(*arg)

def test_collidepoint(self):
c = Circle(0, 0, 5)

p1 = (3, 3)
p2 = (10, 10)
p3 = Vector2(3, 3)
p4 = Vector2(10, 10)

# colliding single
self.assertTrue(c.collidepoint(p1), "Expected True, point should collide here")
self.assertTrue(c.collidepoint(p3), "Expected True, point should collide here")

# not colliding single
self.assertFalse(
c.collidepoint(p2), "Expected False, point should not collide here"
)
self.assertFalse(
c.collidepoint(p4), "Expected False, point should not collide here"
)

# colliding 2 args
self.assertTrue(
c.collidepoint(3, 3), "Expected True, point should collide here"
)

# not colliding 2 args
self.assertFalse(
c.collidepoint(10, 10), "Expected False, point should not collide here"
)


if __name__ == "__main__":
unittest.main()