Skip to content

Commit

Permalink
Merge pull request #74 from ajoubertza/issue-53-add-pygame-math
Browse files Browse the repository at this point in the history
Add support for pygame.math.
  • Loading branch information
hodgestar committed Mar 23, 2017
2 parents d1b1a45 + e06b19a commit dbcb41e
Show file tree
Hide file tree
Showing 11 changed files with 3,393 additions and 5 deletions.
2 changes: 2 additions & 0 deletions benchmarks/run_benchmark.py
@@ -1,3 +1,5 @@
from __future__ import absolute_import

import math
import sys
import time
Expand Down
2 changes: 1 addition & 1 deletion pygame/__init__.py
Expand Up @@ -28,7 +28,7 @@
from pygame import (
display, color, surface, time, event, constants, sprite,
mouse, locals, image, transform, pkgdata, font, mixer,
cursors, key, draw, joystick
cursors, key, draw, joystick, math
)
from pygame.base import (
init, quit, HAVE_NEWBUF, get_sdl_version, get_sdl_byteorder,
Expand Down
23 changes: 23 additions & 0 deletions pygame/compat.py
Expand Up @@ -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.
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
2 changes: 2 additions & 0 deletions pygame/draw.py
Expand Up @@ -20,6 +20,8 @@
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301 USA

from __future__ import absolute_import

from pygame.surface import locked
from pygame.color import create_color
from pygame.compat import xrange_
Expand Down
10 changes: 6 additions & 4 deletions pygame/mask.py
Expand Up @@ -18,7 +18,9 @@

# Implement the pygame API for the bitmask functions

from math import atan2, pi
from __future__ import absolute_import

import math

from pygame._sdl import sdl, ffi
from pygame.surflock import locked
Expand Down Expand Up @@ -65,11 +67,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

Expand Down

0 comments on commit dbcb41e

Please sign in to comment.