comparison 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
comparison
equal deleted inserted replaced
435:878273a984c4 436:cb5c68598ab0
1 # -*- encoding: utf-8 -*-
2 ##
3 ## Copyright (C) 2012 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
4 ##
5 ## This program is free software; you can redistribute it and/or modify
6 ## it under the terms of the GNU General Public License as published
7 ## by the Free Software Foundation; version 3 only.
8 ##
9 ## This program is distributed in the hope that it will be useful,
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ## GNU General Public License for more details.
13 ##
14
15 from libc.math cimport sqrt
16
17
18 cdef class Vector:
19 def __init__(self, float x, float y, float z):
20 self.x = x
21 self.y = y
22 self.z = z
23
24
25 cdef Vector sub(self, Vector other):
26 cdef float x, y, z
27
28 x = self.x - other.x
29 y = self.y - other.y
30 z = self.z - other.z
31
32 return Vector(x, y, z)
33
34
35 cdef Vector cross(Vector vec1, Vector vec2):
36 return Vector(vec1.y * vec2.z - vec2.y * vec1.z,
37 vec1.z * vec2.x - vec2.z * vec1.x,
38 vec1.x * vec2.y - vec2.x * vec1.y)
39
40
41 cdef float dot(Vector vec1, Vector vec2):
42 return vec1.x * vec2.x + vec2.y * vec1.y + vec1.z * vec2.z
43
44
45 cdef Vector normalize(Vector vec):
46 cdef float normal
47
48 normal = 1 / sqrt(vec.x * vec.x + vec.y * vec.y + vec.z * vec.z)
49 return Vector(vec.x * normal, vec.y * normal, vec.z * normal)