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

Added additional circle attributes #3127

Merged
Merged
Show file tree
Hide file tree
Changes from 6 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
5 changes: 5 additions & 0 deletions buildconfig/stubs/pygame/geometry.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ _CanBeCollided = Union[Circle, Rect, FRect, Coordinate, Vector2]
_CanBeIntersected = Union[Circle]

class Circle:
top: Tuple[float, float]
left: Tuple[float, float]
right: Tuple[float, float]
bottom: Tuple[float, float]
itzpr3d4t0r marked this conversation as resolved.
Show resolved Hide resolved

itzpr3d4t0r marked this conversation as resolved.
Show resolved Hide resolved
@property
def x(self) -> float: ...
@x.setter
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 @@ -667,6 +667,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 @@ -709,6 +809,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
Loading