Fixed math.py inconsistencies#2771
Conversation
|
I'm not sure if I'm in favor of requiring the use of Point2 objects here, the current API leaves it up to the user if they want to have tuple positions, but changing to this would require them to do it. While tuples are pretty performant it's not exactly a zero cost to add tuples into functions which are likely to be used in heavily performance sensitive pieces of code |
|
Yes but the issue is not not using Point2 but the incosistency. In some function x,y is used and in other Point2. This is really confusing, because in one function you put something in a tuple and in the other x, y. If the performance is an issue shouldn't all Point2's be replaced by x, y? |
I agree it's not ideal. Please consider:
See the end of comment after the break for details beyond "document it" with a call-out ( Quick FixesYou Want Tuples as Points:
point_to_rotate = (0, 1)
center = (1,1)
rotated = rotate_point(*point_to_rotate, *center, angle_degrees=45)You Want OOP / Modifiable ShapesThe Why These Functions Look DifferentTL;DR: A middle-ground design, history, and the way GPUs work The Design Goals Of These FunctionsTL;DR: Balance reliability and debugging ease against performance
HistoryThe For example, The
|
This PR refactors math utilities and related code to use
Point2tuples consistently instead of mixingx, yfloat parameters andPoint2:Changes
Updated core math functions:
get_distance(x1, y1, x2, y2)→get_distance(pos1: Point2, pos2: Point2)rotate_point(x, y, cx, cy, angle)→rotate_point(point: Point2, center: Point2, angle)get_angle_degreesandget_angle_radiansnow take Point2 pairs.Fixed
get_angle_radians: previously usedmath.atan2(x, y), now corrected tomath.atan2(y, x).Updated all call sites across the codebase (drawing functions, physics, easing, examples, etc.) to use the new signatures.
Improved readability and reduced chance of mismatching parameters.