Skip to content

Commit

Permalink
Merge pull request #3127 from MrRedstone058/geometry_circle_attributes
Browse files Browse the repository at this point in the history
Added additional circle attributes
  • Loading branch information
MyreMylar authored Oct 1, 2024
2 parents 0fe6832 + 66e32e0 commit b03251e
Show file tree
Hide file tree
Showing 5 changed files with 384 additions and 0 deletions.
16 changes: 16 additions & 0 deletions buildconfig/stubs/pygame/geometry.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,22 @@ class Circle:
def center(self) -> Tuple[float, float]: ...
@center.setter
def center(self, value: Coordinate) -> None: ...
@property
def top(self) -> Tuple[float, float]: ...
@top.setter
def top(self, value: Coordinate) -> None: ...
@property
def left(self) -> Tuple[float, float]: ...
@left.setter
def left(self, value: Coordinate) -> None: ...
@property
def bottom(self) -> Tuple[float, float]: ...
@bottom.setter
def bottom(self, value: Coordinate) -> None: ...
@property
def right(self) -> Tuple[float, float]: ...
@right.setter
def right(self, value: Coordinate) -> None: ...
@overload
def __init__(self, x: float, y: float, r: float) -> None: ...
@overload
Expand Down
52 changes: 52 additions & 0 deletions docs/reST/ref/geometry.rst
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,58 @@

.. ## Circle.circumference ##
.. attribute:: top

| :sl:`top coordinate of the circle`
| :sg:`top -> (float, float)`
It's a tuple containing the `x` and `y` coordinates that represent the top
of the circle.
Reassigning it moves the circle to the new position. The radius will not be affected.

.. versionadded:: 2.5.2

.. ## Circle.top ##
.. attribute:: bottom

| :sl:`bottom coordinate of the circle`
| :sg:`bottom -> (float, float)`
It's a tuple containing the `x` and `y` coordinates that represent the bottom
of the circle.
Reassigning it moves the circle to the new position. The radius will not be affected.

.. versionadded:: 2.5.2

.. ## Circle.bottom ##
.. attribute:: left

| :sl:`left coordinate of the circle`
| :sg:`left -> (float, float)`
It's a tuple containing the `x` and `y` coordinates that represent the left
of the circle.
Reassigning it moves the circle to the new position. The radius will not be affected.

.. versionadded:: 2.5.2

.. ## Circle.left ##
.. attribute:: right

| :sl:`right coordinate of the circle`
| :sg:`right -> (float, float)`
It's a tuple containing the `x` and `y` coordinates that represent the right
of the circle.
Reassigning it moves the circle to the new position. The radius will not be affected.

.. versionadded:: 2.5.2

.. ## Circle.right ##
**Circle Methods**

----
Expand Down
108 changes: 108 additions & 0 deletions src_c/circle.c
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,106 @@ pg_circle_setdiameter(pgCircleObject *self, PyObject *value, void *closure)
return 0;
}

static PyObject *
pg_circle_gettop(pgCircleObject *self, void *closure)
{
return pg_tuple_couple_from_values_double(self->circle.x,
self->circle.y - self->circle.r);
}

static int
pg_circle_settop(pgCircleObject *self, PyObject *value, void *closure)
{
double x, y;

DEL_ATTR_NOT_SUPPORTED_CHECK_NO_NAME(value);

if (!pg_TwoDoublesFromObj(value, &x, &y)) {
PyErr_SetString(PyExc_TypeError, "Expected a sequence of 2 numbers");
return -1;
}

self->circle.y = y + self->circle.r;
self->circle.x = x;

return 0;
}

static PyObject *
pg_circle_getleft(pgCircleObject *self, void *closure)
{
return pg_tuple_couple_from_values_double(self->circle.x - self->circle.r,
self->circle.y);
}

static int
pg_circle_setleft(pgCircleObject *self, PyObject *value, void *closure)
{
double x, y;

DEL_ATTR_NOT_SUPPORTED_CHECK_NO_NAME(value);

if (!pg_TwoDoublesFromObj(value, &x, &y)) {
PyErr_SetString(PyExc_TypeError, "Expected a sequence of 2 numbers");
return -1;
}

self->circle.x = x + self->circle.r;
self->circle.y = y;

return 0;
}

static PyObject *
pg_circle_getbottom(pgCircleObject *self, void *closure)
{
return pg_tuple_couple_from_values_double(self->circle.x,
self->circle.y + self->circle.r);
}

static int
pg_circle_setbottom(pgCircleObject *self, PyObject *value, void *closure)
{
double x, y;

DEL_ATTR_NOT_SUPPORTED_CHECK_NO_NAME(value);

if (!pg_TwoDoublesFromObj(value, &x, &y)) {
PyErr_SetString(PyExc_TypeError, "Expected a sequence of 2 numbers");
return -1;
}

self->circle.y = y - self->circle.r;
self->circle.x = x;

return 0;
}

static PyObject *
pg_circle_getright(pgCircleObject *self, void *closure)
{
return pg_tuple_couple_from_values_double(self->circle.x + self->circle.r,
self->circle.y);
}

static int
pg_circle_setright(pgCircleObject *self, PyObject *value, void *closure)
{
double x, y;

DEL_ATTR_NOT_SUPPORTED_CHECK_NO_NAME(value);

if (!pg_TwoDoublesFromObj(value, &x, &y)) {
PyErr_SetString(PyExc_TypeError, "Expected a sequence of 2 numbers");
return -1;
}

self->circle.x = x - self->circle.r;
self->circle.y = y;

return 0;
}

static PyObject *
pg_circle_richcompare(PyObject *self, PyObject *other, int op)
{
Expand Down Expand Up @@ -859,6 +959,14 @@ static PyGetSetDef pg_circle_getsets[] = {
DOC_CIRCLE_AREA, NULL},
{"circumference", (getter)pg_circle_getcircumference,
(setter)pg_circle_setcircumference, DOC_CIRCLE_CIRCUMFERENCE, NULL},
{"top", (getter)pg_circle_gettop, (setter)pg_circle_settop, DOC_CIRCLE_TOP,
NULL},
{"left", (getter)pg_circle_getleft, (setter)pg_circle_setleft,
DOC_CIRCLE_LEFT, NULL},
{"bottom", (getter)pg_circle_getbottom, (setter)pg_circle_setbottom,
DOC_CIRCLE_BOTTOM, NULL},
{"right", (getter)pg_circle_getright, (setter)pg_circle_setright,
DOC_CIRCLE_RIGHT, NULL},
{NULL, 0, NULL, NULL, NULL}};

static PyTypeObject pgCircle_Type = {
Expand Down
4 changes: 4 additions & 0 deletions src_c/doc/geometry_doc.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
#define DOC_CIRCLE_DIAMETER "diameter -> float\ndiameter of the circle"
#define DOC_CIRCLE_AREA "area -> float\narea of the circle"
#define DOC_CIRCLE_CIRCUMFERENCE "circumference -> float\ncircumference of the circle"
#define DOC_CIRCLE_TOP "top -> (float, float)\ntop coordinate of the circle"
#define DOC_CIRCLE_BOTTOM "bottom -> (float, float)\nbottom coordinate of the circle"
#define DOC_CIRCLE_LEFT "left -> (float, float)\nleft coordinate of the circle"
#define DOC_CIRCLE_RIGHT "right -> (float, float)\nright coordinate of the circle"
#define DOC_CIRCLE_COLLIDEPOINT "collidepoint((x, y), /) -> bool\ncollidepoint(x, y, /) -> bool\ncollidepoint(vector2, /) -> bool\ntests if a point is inside the circle"
#define DOC_CIRCLE_COLLIDECIRCLE "collidecircle(circle, /) -> bool\ncollidecircle(x, y, radius, /) -> bool\ncollidecircle((x, y), radius, /) -> bool\ncollidecircle(vector2, radius, /) -> bool\ntests if a circle collides with this circle"
#define DOC_CIRCLE_COLLIDERECT "colliderect(rect, /) -> bool\ncolliderect((x, y, width, height), /) -> bool\ncolliderect(x, y, width, height, /) -> bool\ncolliderect((x, y), (width, height), /) -> bool\ncolliderect(vector2, (width, height), /) -> bool\ntests if a rectangle collides with this circle"
Expand Down
Loading

0 comments on commit b03251e

Please sign in to comment.