Mercurial > touhou
diff pytouhou/utils/vector.pyx @ 436:cb5c68598ab0
Cythonize pytouhou.utils.maths and pytouhou.utils.vector.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Wed, 07 Aug 2013 11:34:42 +0200 |
parents | pytouhou/utils/vector.py@74471afbac37 |
children | 7f016dfbdfb1 |
line wrap: on
line diff
copy from pytouhou/utils/vector.py copy to pytouhou/utils/vector.pyx --- a/pytouhou/utils/vector.py +++ b/pytouhou/utils/vector.pyx @@ -12,32 +12,38 @@ ## GNU General Public License for more details. ## -from math import sqrt +from libc.math cimport sqrt -class Vector(list): - def __init__(self, data=None): - list.__init__(self, data or [0] * 3) - - - def __add__(self, other): - return Vector([a+b for a, b in zip(self, other)]) +cdef class Vector: + def __init__(self, float x, float y, float z): + self.x = x + self.y = y + self.z = z - def __sub__(self, other): - return Vector([a-b for a, b in zip(self, other)]) + cdef Vector sub(self, Vector other): + cdef float x, y, z + + x = self.x - other.x + y = self.y - other.y + z = self.z - other.z + + return Vector(x, y, z) -def cross(vec1, vec2): - return Vector([vec1[1] * vec2[2] - vec2[1] * vec1[2], - vec1[2] * vec2[0] - vec2[2] * vec1[0], - vec1[0] * vec2[1] - vec2[0] * vec1[1]]) +cdef Vector cross(Vector vec1, Vector vec2): + return Vector(vec1.y * vec2.z - vec2.y * vec1.z, + vec1.z * vec2.x - vec2.z * vec1.x, + vec1.x * vec2.y - vec2.x * vec1.y) -def dot(vec1, vec2): - return vec1[0] * vec2[0] + vec2[1] * vec1[1] + vec1[2] * vec2[2] +cdef float dot(Vector vec1, Vector vec2): + return vec1.x * vec2.x + vec2.y * vec1.y + vec1.z * vec2.z -def normalize(vec1): - normal = 1 / sqrt(vec1[0] * vec1[0] + vec1[1] * vec1[1] + vec1[2] * vec1[2]) - return Vector(x * normal for x in vec1) +cdef Vector normalize(Vector vec): + cdef float normal + + normal = 1 / sqrt(vec.x * vec.x + vec.y * vec.y + vec.z * vec.z) + return Vector(vec.x * normal, vec.y * normal, vec.z * normal)