Skip to content

Commit

Permalink
made _pg_circle_collideswith PG_FORCEINLINE
Browse files Browse the repository at this point in the history
  • Loading branch information
itzpr3d4t0r committed May 27, 2024
1 parent 73a0427 commit f032a65
Showing 1 changed file with 94 additions and 1 deletion.
95 changes: 94 additions & 1 deletion src_c/circle.c
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ pg_circle_colliderect(pgCircleObject *self, PyObject *const *args,
return PyBool_FromLong(pgCollision_RectCircle(&temp, &self->circle));
}

static int
static PG_FORCEINLINE int
_pg_circle_collideswith(pgCircleBase *scirc, PyObject *arg)
{
if (pgCircle_Check(arg)) {
Expand Down Expand Up @@ -732,6 +732,99 @@ pg_circle_collidelistall(pgCircleObject *self, PyObject *arg)
return ret;
}

static void
_pg_rotate_circle_helper(pgCircleBase *circle, double angle, double rx,
double ry)
{
if (angle == 0.0 || fmod(angle, 360.0) == 0.0) {
return;
}

double x = circle->x - rx;
double y = circle->y - ry;

const double angle_rad = DEG_TO_RAD(angle);

double cos_theta = cos(angle_rad);
double sin_theta = sin(angle_rad);

circle->x = rx + x * cos_theta - y * sin_theta;
circle->y = ry + x * sin_theta + y * cos_theta;
}

static PyObject *
pg_circle_rotate(pgCircleObject *self, PyObject *const *args, Py_ssize_t nargs)
{
if (!nargs || nargs > 2) {
return RAISE(PyExc_TypeError, "rotate requires 1 or 2 arguments");
}

pgCircleBase *circle = &self->circle;
double angle, rx, ry;

rx = circle->x;
ry = circle->y;

if (!pg_DoubleFromObj(args[0], &angle)) {
return RAISE(PyExc_TypeError,
"Invalid angle argument, must be numeric");
}

if (nargs != 2) {
return _pg_circle_subtype_new(Py_TYPE(self), circle);
}

if (!pg_TwoDoublesFromObj(args[1], &rx, &ry)) {
return RAISE(PyExc_TypeError,
"Invalid rotation point argument, must be a sequence of "
"2 numbers");
}

PyObject *circle_obj = _pg_circle_subtype_new(Py_TYPE(self), circle);
if (!circle_obj) {
return NULL;
}

_pg_rotate_circle_helper(&pgCircle_AsCircle(circle_obj), angle, rx, ry);

return circle_obj;
}

static PyObject *
pg_circle_rotate_ip(pgCircleObject *self, PyObject *const *args,
Py_ssize_t nargs)
{
if (!nargs || nargs > 2) {
return RAISE(PyExc_TypeError, "rotate requires 1 or 2 arguments");
}

pgCircleBase *circle = &self->circle;
double angle, rx, ry;

rx = circle->x;
ry = circle->y;

if (!pg_DoubleFromObj(args[0], &angle)) {
return RAISE(PyExc_TypeError,
"Invalid angle argument, must be numeric");
}

if (nargs != 2) {
/* just return None */
Py_RETURN_NONE;
}

if (!pg_TwoDoublesFromObj(args[1], &rx, &ry)) {
return RAISE(PyExc_TypeError,
"Invalid rotation point argument, must be a sequence "
"of 2 numbers");
}

_pg_rotate_circle_helper(circle, angle, rx, ry);

Py_RETURN_NONE;
}

static struct PyMethodDef pg_circle_methods[] = {
{"collidecircle", (PyCFunction)pg_circle_collidecircle, METH_FASTCALL,
NULL},
Expand Down

0 comments on commit f032a65

Please sign in to comment.