Skip to content

Added missing Circle attrs #2519

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
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
47 changes: 40 additions & 7 deletions buildconfig/stubs/pygame/geometry.pyi
Original file line number Diff line number Diff line change
@@ -1,20 +1,53 @@
from typing import (
Sequence,
overload,
Tuple,
)

from pygame._common import Coordinate

from ._common import Coordinate

class Circle:
x: float
y: float
r: float
@property
def x(self) -> float: ...
@x.setter
def x(self, value: float) -> None: ...
@property
def y(self) -> float: ...
@y.setter
def y(self, value: float) -> None: ...
@property
def r(self) -> float: ...
@r.setter
def r(self, value: float) -> None: ...

radius = r

@property
def r_sqr(self) -> float: ...
@r_sqr.setter
def r_sqr(self, value: float) -> None: ...
@property
def d(self) -> float: ...
@d.setter
def d(self, value: float) -> None: ...

diameter = d

@property
def area(self) -> float: ...
@area.setter
def area(self, value: float) -> None: ...
@property
def circumference(self) -> float: ...
@circumference.setter
def circumference(self, value: float) -> None: ...
@property
def center(self) -> Tuple[float, float]: ...
@center.setter
def center(self, value: Coordinate) -> None: ...
@overload
def __init__(self, x: float, y: float, r: float) -> None: ...
@overload
def __init__(self, pos: Sequence[float], r: float) -> None: ...
def __init__(self, pos: Coordinate, r: float) -> None: ...
@overload
def __init__(self, circle: Circle) -> None: ...
@overload
Expand Down
55 changes: 55 additions & 0 deletions docs/reST/ref/geometry.rst
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,61 @@

.. ## Circle.r ##

.. attribute:: r_sqr

| :sl:`radius of the circle squared`
| :sg:`r_sqr -> float`

It's equivalent to `r*r`. It can be reassigned. If reassigned, the radius
of the circle will be changed to the square root of the new value.
The circle will not be moved from its original position.

.. ## Circle.r_sqr ##

.. attribute:: center

| :sl:`x and y coordinates of the center of the circle`
| :sg:`center -> (float, float)`

It's a tuple containing the `x` and `y` coordinates that represent the center
of the circle. It can be reassigned. If reassigned, the circle will be moved
to the new position. The radius will not be affected.

.. ## Circle.center ##

.. attribute:: diameter

| :sl:`diameter of the circle`
| :sg:`diameter -> float`

It's calculated using the `d=2*r` formula. It can be reassigned. If reassigned
the radius will be changed to half the diameter.
The circle will not be moved from its original position.

.. ## Circle.diameter ##

.. attribute:: area

| :sl:`area of the circle`
| :sg:`area -> float`

It's calculated using the `area=pi*r*r` formula. It can be reassigned.
If reassigned the circle radius will be changed to produce a circle with matching
area. The circle will not be moved from its original position.

.. ## Circle.area ##

.. attribute:: circumference

| :sl:`circumference of the circle`
| :sg:`circumference -> float`

It's calculated using the `circumference=2*pi*r` formula. It can be reassigned.
If reassigned the circle radius will be changed to produce a circle with matching
circumference. The circle will not be moved from its original position.

.. ## Circle.circumference ##

**Circle Methods**

----
Expand Down
151 changes: 151 additions & 0 deletions src_c/circle.c
Original file line number Diff line number Diff line change
Expand Up @@ -311,10 +311,161 @@ pg_circle_setr(pgCircleObject *self, PyObject *value, void *closure)
return 0;
}

static PyObject *
pg_circle_getr_sqr(pgCircleObject *self, void *closure)
{
return PyFloat_FromDouble(self->circle.r * self->circle.r);
}

static int
pg_circle_setr_sqr(pgCircleObject *self, PyObject *value, void *closure)
{
double radius_squared;

DEL_ATTR_NOT_SUPPORTED_CHECK_NO_NAME(value);

if (!pg_DoubleFromObj(value, &radius_squared)) {
PyErr_SetString(PyExc_TypeError,
"Invalid type for radius squared, must be numeric");
return -1;
}

if (radius_squared <= 0) {
PyErr_SetString(PyExc_ValueError,
"Invalid radius squared value, must be > 0");
return -1;
}

self->circle.r = sqrt(radius_squared);

return 0;
}

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

static int
pg_circle_setcenter(pgCircleObject *self, PyObject *value, void *closure)
{
DEL_ATTR_NOT_SUPPORTED_CHECK_NO_NAME(value);
if (!pg_TwoDoublesFromObj(value, &self->circle.x, &self->circle.y)) {
PyErr_SetString(PyExc_TypeError, "Expected a sequence of 2 numbers");
return -1;
}
return 0;
}

static PyObject *
pg_circle_getarea(pgCircleObject *self, void *closure)
{
return PyFloat_FromDouble(M_PI * self->circle.r * self->circle.r);
}

static int
pg_circle_setarea(pgCircleObject *self, PyObject *value, void *closure)
{
double area;

DEL_ATTR_NOT_SUPPORTED_CHECK_NO_NAME(value);

if (!pg_DoubleFromObj(value, &area)) {
PyErr_SetString(PyExc_TypeError,
"Invalid type for area, must be numeric");
return -1;
}

if (area <= 0) {
PyErr_SetString(PyExc_ValueError, "Invalid area value, must be > 0");
return -1;
}

self->circle.r = sqrt(area / M_PI);

return 0;
}

static PyObject *
pg_circle_getcircumference(pgCircleObject *self, void *closure)
{
return PyFloat_FromDouble(M_TWOPI * self->circle.r);
}

static int
pg_circle_setcircumference(pgCircleObject *self, PyObject *value,
void *closure)
{
double circumference;

DEL_ATTR_NOT_SUPPORTED_CHECK_NO_NAME(value);

if (!pg_DoubleFromObj(value, &circumference)) {
PyErr_SetString(PyExc_TypeError,
"Invalid type for circumference, must be numeric");
return -1;
}

if (circumference <= 0) {
PyErr_SetString(PyExc_ValueError,
"Invalid circumference value, must be > 0");
return -1;
}

self->circle.r = circumference / M_TWOPI;

return 0;
}

static PyObject *
pg_circle_getdiameter(pgCircleObject *self, void *closure)
{
return PyFloat_FromDouble(2 * self->circle.r);
}

static int
pg_circle_setdiameter(pgCircleObject *self, PyObject *value, void *closure)
{
double diameter;

DEL_ATTR_NOT_SUPPORTED_CHECK_NO_NAME(value);

if (!pg_DoubleFromObj(value, &diameter)) {
PyErr_SetString(PyExc_TypeError,
"Invalid type for diameter, must be numeric");
return -1;
}

if (diameter <= 0) {
PyErr_SetString(PyExc_ValueError,
"Invalid diameter value, must be > 0");
return -1;
}

self->circle.r = diameter / 2;

return 0;
}

static PyGetSetDef pg_circle_getsets[] = {
{"x", (getter)pg_circle_getx, (setter)pg_circle_setx, DOC_CIRCLE_X, NULL},
{"y", (getter)pg_circle_gety, (setter)pg_circle_sety, DOC_CIRCLE_Y, NULL},
{"r", (getter)pg_circle_getr, (setter)pg_circle_setr, DOC_CIRCLE_R, NULL},
{"radius", (getter)pg_circle_getr, (setter)pg_circle_setr, DOC_CIRCLE_R,
NULL},
{"r_sqr", (getter)pg_circle_getr_sqr, (setter)pg_circle_setr_sqr,
DOC_CIRCLE_RSQR, NULL},
{"d", (getter)pg_circle_getdiameter, (setter)pg_circle_setdiameter,
DOC_CIRCLE_DIAMETER, NULL},
{"diameter", (getter)pg_circle_getdiameter, (setter)pg_circle_setdiameter,
DOC_CIRCLE_DIAMETER, NULL},
{"center", (getter)pg_circle_getcenter, (setter)pg_circle_setcenter,
DOC_CIRCLE_CENTER, NULL},
{"area", (getter)pg_circle_getarea, (setter)pg_circle_setarea,
DOC_CIRCLE_AREA, NULL},
{"circumference", (getter)pg_circle_getcircumference,
(setter)pg_circle_setcircumference, DOC_CIRCLE_CIRCUMFERENCE, NULL},
{NULL, 0, NULL, NULL, NULL}};

static PyTypeObject pgCircle_Type = {
Expand Down
5 changes: 5 additions & 0 deletions src_c/doc/geometry_doc.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,10 @@
#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_RSQR "r_sqr -> float\nradius of the circle squared"
#define DOC_CIRCLE_CENTER "center -> (float, float)\nx and y coordinates of the center of the circle"
#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_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"
12 changes: 12 additions & 0 deletions src_c/geometry.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,16 @@ static PyTypeObject pgCircle_Type;
static int
pgCircle_FromObject(PyObject *obj, pgCircleBase *out);

/* Constants */

/* PI */
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif

/* 2PI */
#ifndef M_TWOPI
#define M_TWOPI 6.28318530717958647692
#endif

#endif // PYGAME_CE_GEOMETRY_H
28 changes: 28 additions & 0 deletions src_c/include/_pygame.h
Original file line number Diff line number Diff line change
Expand Up @@ -617,3 +617,31 @@ pg_tuple_triple_from_values_int(int val1, int val2, int val3)

return tup;
}

static PG_INLINE PyObject *
pg_tuple_couple_from_values_double(double val1, double val2)
{
/* This function turns two input doubles into a python tuple object.
* Currently, 5th November 2022, this is faster than using Py_BuildValue
* to do the same thing.
*/
PyObject *tuple = PyTuple_New(2);
if (!tuple)
return NULL;

PyObject *tmp = PyFloat_FromDouble(val1);
if (!tmp) {
Py_DECREF(tuple);
return NULL;
}
PyTuple_SET_ITEM(tuple, 0, tmp);

tmp = PyFloat_FromDouble(val2);
if (!tmp) {
Py_DECREF(tuple);
return NULL;
}
PyTuple_SET_ITEM(tuple, 1, tmp);

return tuple;
}
Loading