-
-
Notifications
You must be signed in to change notification settings - Fork 165
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
new angle and angle_rad property in vector2 #3222
base: main
Are you sure you want to change the base?
Changes from 10 commits
1932b82
49dbee5
ddf0c48
131d956
91fe678
faf77a8
0f6dc9a
4d66c2a
ad6f04f
71c12a7
23f426f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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]. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My mistake, the interval is actually inclusive of -180. So, |
||
|
||
Usage: Accessing `angle` provides the current angle of the vector in degrees within the specified range. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reword "specified range". The word 'specified' implies that the user provides it. |
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. need to document .. versionadded:: 2.5.x There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
yes 👍 |
||
.. 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 ## | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 */ | ||
|
@@ -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); | ||
|
@@ -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); | ||
|
@@ -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; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems unnecessary. The radians is within |
||
|
||
return PyFloat_FromDouble(angle_deg); | ||
} | ||
|
||
static PyObject * | ||
vector_get_angle_rad(pgVector *self, void *closure) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just reuse the function you already have instead of writing the same code twice. Since you already got the output in a form you like, just convert it from degrees to radians There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would suggest creating a helper function There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where I should make the documentations for |
||
{ | ||
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) | ||
{ | ||
|
@@ -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 */ | ||
}; | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These are properties and they are read-only.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@property
doesn't say that it's a read-only attribute, right ?