Mercurial > touhou
comparison pytouhou/utils/matrix.pyx @ 131:fab7ad2f0d8b
Use Cython, improve performances!
author | Thibaut Girka <thib@sitedethib.com> |
---|---|
date | Sun, 11 Sep 2011 02:02:59 +0200 |
parents | pytouhou/utils/matrix.py@d1c82d43bbf3 |
children | 74471afbac37 |
comparison
equal
deleted
inserted
replaced
130:11ab06f4c4c6 | 131:fab7ad2f0d8b |
---|---|
1 # -*- encoding: utf-8 -*- | |
2 ## | |
3 ## Copyright (C) 2011 Thibaut Girka <thib@sitedethib.com> | |
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 sin, cos | |
16 | |
17 | |
18 cdef class Matrix: | |
19 def __init__(Matrix self, data=None): | |
20 self.data = data or [[0] * 4 for i in xrange(4)] | |
21 | |
22 | |
23 cpdef flip(Matrix self): | |
24 data = self.data | |
25 a, b, c, d = data[0] | |
26 data[0] = [-a, -b, -c, -d] | |
27 | |
28 | |
29 cpdef scale(Matrix self, x, y, z): | |
30 d1 = self.data | |
31 d1[0] = [a * x for a in d1[0]] | |
32 d1[1] = [a * y for a in d1[1]] | |
33 d1[2] = [a * z for a in d1[2]] | |
34 | |
35 | |
36 cpdef scale2d(Matrix self, x, y): | |
37 data = self.data | |
38 d1a, d1b, d1c, d1d = data[0] | |
39 d2a, d2b, d2c, d2d = data[1] | |
40 data[0] = [d1a * x, d1b * x, d1c * x, d1d * x] | |
41 data[1] = [d2a * y, d2b * y, d2c * y, d2d * y] | |
42 | |
43 | |
44 cpdef translate(Matrix self, x, y, z): | |
45 data = self.data | |
46 a, b, c = data[3][:3] | |
47 a, b, c = a * x, b * y, c * z | |
48 d1a, d1b, d1c, d1d = data[0] | |
49 d2a, d2b, d2c, d2d = data[1] | |
50 d3a, d3b, d3c, d3d = data[2] | |
51 data[0] = [d1a + a, d1b + a, d1c + a, d1d + a] | |
52 data[1] = [d2a + b, d2b + b, d2c + b, d2d + b] | |
53 data[2] = [d3a + c, d3b + c, d3c + c, d3d + c] | |
54 | |
55 | |
56 cpdef rotate_x(Matrix self, angle): | |
57 d1 = self.data | |
58 cos_a = cos(angle) | |
59 sin_a = sin(angle) | |
60 d1[1], d1[2] = ([cos_a * d1[1][i] - sin_a * d1[2][i] for i in range(4)], | |
61 [sin_a * d1[1][i] + cos_a * d1[2][i] for i in range(4)]) | |
62 | |
63 | |
64 cpdef rotate_y(Matrix self, angle): | |
65 d1 = self.data | |
66 cos_a = cos(angle) | |
67 sin_a = sin(angle) | |
68 d1[0], d1[2] = ([cos_a * d1[0][i] + sin_a * d1[2][i] for i in range(4)], | |
69 [- sin_a * d1[0][i] + cos_a * d1[2][i] for i in range(4)]) | |
70 | |
71 | |
72 cpdef rotate_z(Matrix self, angle): | |
73 d1 = self.data | |
74 cos_a = cos(angle) | |
75 sin_a = sin(angle) | |
76 d1[0], d1[1] = ([cos_a * d1[0][i] - sin_a * d1[1][i] for i in range(4)], | |
77 [sin_a * d1[0][i] + cos_a * d1[1][i] for i in range(4)]) | |
78 |