comparison pytouhou/utils/vector.py @ 370:74471afbac37

Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Fri, 27 Jul 2012 18:43:48 +0200
parents
children
comparison
equal deleted inserted replaced
369:f305cdd6f6c5 370:74471afbac37
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 math import sqrt
16
17
18 class Vector(list):
19 def __init__(self, data=None):
20 list.__init__(self, data or [0] * 3)
21
22
23 def __add__(self, other):
24 return Vector([a+b for a, b in zip(self, other)])
25
26
27 def __sub__(self, other):
28 return Vector([a-b for a, b in zip(self, other)])
29
30
31 def cross(vec1, vec2):
32 return Vector([vec1[1] * vec2[2] - vec2[1] * vec1[2],
33 vec1[2] * vec2[0] - vec2[2] * vec1[0],
34 vec1[0] * vec2[1] - vec2[0] * vec1[1]])
35
36
37 def dot(vec1, vec2):
38 return vec1[0] * vec2[0] + vec2[1] * vec1[1] + vec1[2] * vec2[2]
39
40
41 def normalize(vec1):
42 normal = 1 / sqrt(vec1[0] * vec1[0] + vec1[1] * vec1[1] + vec1[2] * vec1[2])
43 return Vector(x * normal for x in vec1)