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
Add support for pygame.math. #74
Changes from 15 commits
af1fc89
9510a28
859a6e5
b69d0d3
efb7ca4
f2cbb91
296174b
4f1a553
458e1e6
361c8c6
7d28b25
f846f6f
f849efd
c085311
a57f07e
e06b19a
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
from __future__ import absolute_import | ||
import math | ||
import sys | ||
import time | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -114,3 +114,26 @@ def next_(i, *args): | |
import itertools.imap as imap_ | ||
except ImportError: | ||
imap_ = map | ||
|
||
|
||
# The behaviour of the power operator, '**' and built-in pow() function, | ||
# changed in Python 3.x. In Python 3.x raising a negative number to a | ||
# fractional power results in a complex number, while in earlier versions | ||
# it raised a ValueError. Matching the behaviour of pygame requires | ||
# the ValueError instead of a complex result. The compatibility function | ||
# defined below is provided for this purpose. | ||
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. It looks like 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. While math.pow will return a ValueError, it is also using the underlying C math library and will have a different precision from pow. pygame is explicitly using pow underneath it's implementation, so if we use math.pow(), there will be differences between the implementations. Whether these will matter is another question, since they'll arguably only show up in odd corner cases. 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 tried 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. Tx for the discussion. Happy for it to stay this way then. |
||
try: | ||
(-2.2) ** 1.1 | ||
|
||
def pow_compat(x, y): | ||
"""Return x ** y, with Python 2.x compatible exceptions.""" | ||
if x < 0 and not float(y).is_integer(): | ||
raise ValueError("negative number cannot be raised to a fractional power") | ||
else: | ||
return x ** y | ||
|
||
except ValueError: | ||
|
||
def pow_compat(x, y): | ||
"""Return x ** y, with Python 2.x compatible exceptions.""" | ||
return x ** y |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,7 +18,8 @@ | |
|
||
# Implement the pygame API for the bitmask functions | ||
|
||
from math import atan2, pi | ||
from __future__ import absolute_import | ||
import math | ||
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. If this is intended to import from 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. mask.py is intending to import the standard math module - this code was there before the local math module was added. Should I still add the absolute_import? That's default behaviour isn't it, or are we aiming to support Python < 2.7? 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. Another place where I think we should have a blank line for consistency. 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. There are a few more of these cases, but I'm not going to comment on them all individually. :) |
||
|
||
from pygame._sdl import sdl, ffi | ||
from pygame.surflock import locked | ||
|
@@ -65,11 +66,11 @@ def angle(self): | |
if tot: | ||
xc = xs // tot | ||
yc = ys // tot | ||
theta = atan2(2 * (xy / tot - xc * yc), | ||
(xx / tot - xc * xc) - (yy / tot - yc * yc)) | ||
theta = math.atan2(2 * (xy / tot - xc * yc), | ||
(xx / tot - xc * xc) - (yy / tot - yc * yc)) | ||
# covert from radians | ||
# We copy this logic from pygame upstream, because reasons | ||
theta = -90.0 * theta / pi | ||
theta = -90.0 * theta / math.pi | ||
return theta | ||
return 0.0 | ||
|
||
|
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.
I think we're missing a blank line after the future import (at least we seem to do that elsewhere).
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.
Sure. I like consistency too.
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.
Tx.