Skip to content

new angle and angle_rad property in vector2 #3222

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 13 commits into from
May 24, 2025
Merged
Show file tree
Hide file tree
Changes from 10 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
2 changes: 2 additions & 0 deletions buildconfig/stubs/pygame/math.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,8 @@ class Vector2(_GenericVector):
xy: Vector2
yx: Vector2
yy: Vector2
angle: float
angle_rad: float
@overload
def __init__(
self: _TVec,
Expand Down
17 changes: 17 additions & 0 deletions docs/reST/ref/math.rst
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,23 @@ Multiple coordinates can be set using slices or swizzling
find that either the margin is too large or too small, in which case changing ``epsilon`` slightly
might help you out.

.. attribute:: angle

| :sl:`Gives the angle of the vector in degrees, relative to the X-axis, normalized to the interval (-180, 180].`

Read-only attribute representing the angle of the vector in degrees relative to the X-axis. This angle is normalized to
the interval (-180, 180].

Usage: Accessing `angle` provides the current angle of the vector in degrees within the specified range.

.. attribute:: angle_rad

| :sl:`Gives the angle of the vector in radians, relative to the X-axis, normalized to the interval (-π, π].`

Read-only attribute representing the angle of the vector in radians relative to the X-axis. This value is equivalent
to the `angle` attribute converted to radians and is normalized to the interval (-π, π].

Usage: Accessing `angle_rad` provides the current angle of the vector in radians within the specified range.

.. ## pygame.math.Vector2 ##

Expand Down
2 changes: 2 additions & 0 deletions src_c/doc/math_doc.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
#define DOC_MATH_VECTOR2_CLAMPMAGNITUDEIP "clamp_magnitude_ip(max_length, /) -> None\nclamp_magnitude_ip(min_length, max_length, /) -> None\nClamps the vector's magnitude between max_length and min_length"
#define DOC_MATH_VECTOR2_UPDATE "update() -> None\nupdate(int) -> None\nupdate(float) -> None\nupdate(Vector2) -> None\nupdate(x, y) -> None\nupdate((x, y)) -> None\nSets the coordinates of the vector."
#define DOC_MATH_VECTOR2_EPSILON "Determines the tolerance of vector calculations."
#define DOC_MATH_VECTOR2_ANGLE "Gives the angle of the vector in degrees, relative to the X-axis, normalized to the interval (-180, 180]."
#define DOC_MATH_VECTOR2_ANGLERAD "Gives the angle of the vector in radians, relative to the X-axis, normalized to the interval (-π, π]."
#define DOC_MATH_VECTOR3 "Vector3() -> Vector3(0, 0, 0)\nVector3(int) -> Vector3\nVector3(float) -> Vector3\nVector3(Vector3) -> Vector3\nVector3(x, y, z) -> Vector3\nVector3((x, y, z)) -> Vector3\na 3-Dimensional Vector"
#define DOC_MATH_VECTOR3_DOT "dot(Vector3, /) -> float\ncalculates the dot- or scalar-product with the other vector"
#define DOC_MATH_VECTOR3_CROSS "cross(Vector3, /) -> Vector3\ncalculates the cross- or vector-product"
Expand Down
59 changes: 58 additions & 1 deletion src_c/math.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@

#define TWO_PI (2. * M_PI)

#define RAD_TO_DEG (180.0 / M_PI)
#define DEG_TO_RAD (M_PI / 180.0)

#ifndef M_PI_2
#define M_PI_2 (M_PI / 2.0)
#endif /* M_PI_2 */
Expand Down Expand Up @@ -142,7 +145,6 @@ _vector_coords_from_string(PyObject *str, char **delimiter, double *coords,
static void
_vector_move_towards_helper(Py_ssize_t dim, double *origin_coords,
double *target_coords, double max_distance);

/* generic vector functions */
static PyObject *
pgVector_NEW(Py_ssize_t dim);
Expand Down Expand Up @@ -202,6 +204,10 @@ vector_sety(pgVector *self, PyObject *value, void *closure);
static int
vector_setz(pgVector *self, PyObject *value, void *closure);
static PyObject *
vector_get_angle(pgVector *self, void *closure);
static PyObject *
vector_get_angle_rad(pgVector *self, void *closure);
static PyObject *
vector_richcompare(PyObject *o1, PyObject *o2, int op);
static PyObject *
vector_length(pgVector *self, PyObject *args);
Expand Down Expand Up @@ -1269,6 +1275,54 @@ vector_setz(pgVector *self, PyObject *value, void *closure)
return vector_set_component(self, value, 2);
}

static PyObject *
vector_get_angle(pgVector *self, void *closure)
{
PyObject *angle_obj = vector_get_angle_rad(self, closure);
double angle_rad = PyFloat_AsDouble(angle_obj);
double angle_deg = angle_rad * RAD_TO_DEG;

if (angle_deg > 180.0) {
angle_deg -= 360.0;
}
else if (angle_deg <= -180.0) {
angle_deg += 360.0;
}

return PyFloat_FromDouble(angle_deg);
}

static PyObject *
vector_get_angle_rad(pgVector *self, void *closure)
{
pgVector *vec = self;
double x = vec->coords[0];
double y = vec->coords[1];

if (Py_IS_NAN(x) || Py_IS_NAN(y)) {
return PyFloat_FromDouble(Py_NAN);
}

if (Py_IS_INFINITY(y)) {
if (Py_IS_INFINITY(x)) {
if (copysign(1., x) == 1.)
return PyFloat_FromDouble(copysign(0.25 * Py_MATH_PI, y));
else
return PyFloat_FromDouble(copysign(0.75 * Py_MATH_PI, y));
}
return PyFloat_FromDouble(copysign(0.5 * Py_MATH_PI, y));
}

if (Py_IS_INFINITY(x) || y == 0.) {
if (copysign(1., x) == 1.)
return PyFloat_FromDouble(copysign(0., y));
else
return PyFloat_FromDouble(copysign(Py_MATH_PI, y));
}

return PyFloat_FromDouble(atan2(y, x));
}

static PyObject *
vector_richcompare(PyObject *o1, PyObject *o2, int op)
{
Expand Down Expand Up @@ -2585,6 +2639,9 @@ static PyMethodDef vector2_methods[] = {
static PyGetSetDef vector2_getsets[] = {
{"x", (getter)vector_getx, (setter)vector_setx, NULL, NULL},
{"y", (getter)vector_gety, (setter)vector_sety, NULL, NULL},
{"angle", (getter)vector_get_angle, NULL, DOC_MATH_VECTOR2_ANGLE, NULL},
{"angle_rad", (getter)vector_get_angle_rad, NULL,
DOC_MATH_VECTOR2_ANGLERAD, NULL},
{NULL, 0, NULL, NULL, NULL} /* Sentinel */
};

Expand Down
98 changes: 98 additions & 0 deletions test/math_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1363,6 +1363,104 @@ def test_del_y(self):
exception = ctx.exception
self.assertEqual(str(exception), "Cannot delete the y attribute")

def test_angle_rad_property(self):
v0 = Vector2(1, 0)
self.assertEqual(v0.angle_rad, 0.0)

v1 = Vector2(0, 1)
self.assertEqual(v1.angle_rad, math.pi / 2)

v2 = Vector2(-1, 0)
self.assertEqual(v2.angle_rad, math.pi)

v3 = Vector2(0, -1)
self.assertEqual(v3.angle_rad, -math.pi / 2)

v4 = Vector2(1, 1)
self.assertEqual(v4.angle_rad, math.pi / 4)

v5 = Vector2(-1, 1)
self.assertEqual(v5.angle_rad, 3 * math.pi / 4)

v6 = Vector2(-1, -1)
self.assertEqual(v6.angle_rad, -3 * math.pi / 4)

v7 = Vector2(1, -1)
self.assertEqual(v7.angle_rad, -math.pi / 4)

v8 = Vector2(float('inf'), float('inf'))
self.assertEqual(v8.angle_rad, math.pi / 4)

v9 = Vector2(float('-inf'), float('inf'))
self.assertEqual(v9.angle_rad, 3 * math.pi / 4)

v10 = Vector2(float('-inf'), float('-inf'))
self.assertEqual(v10.angle_rad, -3 * math.pi / 4)

v11 = Vector2(float('inf'), float('-inf'))
self.assertEqual(v11.angle_rad, -math.pi / 4)

v12 = Vector2(0, 0)
self.assertEqual(v12.angle_rad, 0.0)

v13 = Vector2(float('nan'), 1)
self.assertTrue(math.isnan(v13.angle_rad))

v14 = Vector2(1, float('nan'))
self.assertTrue(math.isnan(v14.angle_rad))

v15 = Vector2(float('nan'), float('nan'))
self.assertTrue(math.isnan(v15.angle_rad))

def test_angle_property(self):
v0 = pygame.math.Vector2(1, 0)
self.assertEqual(v0.angle, 0.0)

v1 = pygame.math.Vector2(0, 1)
self.assertEqual(v1.angle, 90.0)

v2 = pygame.math.Vector2(-1, 0)
self.assertEqual(v2.angle, 180.0)

v3 = pygame.math.Vector2(0, -1)
self.assertEqual(v3.angle, -90.0)

v4 = pygame.math.Vector2(1, 1)
self.assertEqual(v4.angle, 45.0)

v5 = pygame.math.Vector2(-1, 1)
self.assertEqual(v5.angle, 135.0)

v6 = pygame.math.Vector2(-1, -1)
self.assertEqual(v6.angle, -135.0)

v7 = pygame.math.Vector2(1, -1)
self.assertEqual(v7.angle, -45.0)

v8 = pygame.math.Vector2(float('inf'), float('inf'))
self.assertEqual(v8.angle, 45.0)

v9 = pygame.math.Vector2(float('-inf'), float('inf'))
self.assertEqual(v9.angle, 135.0)

v10 = pygame.math.Vector2(float('-inf'), float('-inf'))
self.assertEqual(v10.angle, -135.0)

v11 = pygame.math.Vector2(float('inf'), float('-inf'))
self.assertEqual(v11.angle, -45.0)

v12 = pygame.math.Vector2(0, 0)
self.assertEqual(v12.angle, 0.0)

v13 = pygame.math.Vector2(float('nan'), 1)
self.assertTrue(math.isnan(v13.angle))

v14 = pygame.math.Vector2(1, float('nan'))
self.assertTrue(math.isnan(v14.angle))

v15 = pygame.math.Vector2(float('nan'), float('nan'))
self.assertTrue(math.isnan(v15.angle))


class Vector3TypeTest(unittest.TestCase):
def setUp(self):
Expand Down
Loading