annotate bGrease/geometry.py @ 84:bb9e2f2548c6

Added moveObject method to the GameModel class.
author KarstenBock@gmx.net
date Fri, 23 Sep 2011 15:08:27 +0200
parents ff3e395abf91
children a6bbb732b27b
rev   line source
5
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
1 __version__ = "$Id$"
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
2 __docformat__ = "reStructuredText"
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
3
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
4 import operator
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
5 import math
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
6 import ctypes
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
7
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
8 class Vec2d(ctypes.Structure):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
9 """2d vector class, supports vector and scalar operators,
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
10 and also provides a bunch of high level functions
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
11 """
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
12 __slots__ = ['x', 'y']
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
13
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
14 @classmethod
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
15 def from_param(cls, arg):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
16 return cls(arg)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
17
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
18 def __init__(self, x_or_pair, y = None):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
19
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
20 if y == None:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
21 self.x = x_or_pair[0]
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
22 self.y = x_or_pair[1]
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
23 else:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
24 self.x = x_or_pair
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
25 self.y = y
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
26
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
27 def __len__(self):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
28 return 2
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
29
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
30 def __getitem__(self, key):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
31 if key == 0:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
32 return self.x
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
33 elif key == 1:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
34 return self.y
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
35 else:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
36 raise IndexError("Invalid subscript "+str(key)+" to Vec2d")
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
37
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
38 def __setitem__(self, key, value):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
39 if key == 0:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
40 self.x = value
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
41 elif key == 1:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
42 self.y = value
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
43 else:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
44 raise IndexError("Invalid subscript "+str(key)+" to Vec2d")
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
45
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
46 # String representaion (for debugging)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
47 def __repr__(self):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
48 return 'Vec2d(%s, %s)' % (self.x, self.y)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
49
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
50 # Comparison
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
51 def __eq__(self, other):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
52 if hasattr(other, "__getitem__") and len(other) == 2:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
53 return self.x == other[0] and self.y == other[1]
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
54 else:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
55 return False
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
56
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
57 def __ne__(self, other):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
58 if hasattr(other, "__getitem__") and len(other) == 2:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
59 return self.x != other[0] or self.y != other[1]
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
60 else:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
61 return True
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
62
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
63 def __nonzero__(self):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
64 return self.x or self.y
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
65
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
66 # Generic operator handlers
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
67 def _o2(self, other, f):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
68 "Any two-operator operation where the left operand is a Vec2d"
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
69 if isinstance(other, Vec2d):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
70 return Vec2d(f(self.x, other.x),
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
71 f(self.y, other.y))
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
72 elif (hasattr(other, "__getitem__")):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
73 return Vec2d(f(self.x, other[0]),
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
74 f(self.y, other[1]))
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
75 else:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
76 return Vec2d(f(self.x, other),
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
77 f(self.y, other))
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
78
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
79 def _r_o2(self, other, f):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
80 "Any two-operator operation where the right operand is a Vec2d"
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
81 if (hasattr(other, "__getitem__")):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
82 return Vec2d(f(other[0], self.x),
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
83 f(other[1], self.y))
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
84 else:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
85 return Vec2d(f(other, self.x),
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
86 f(other, self.y))
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
87
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
88 def _io(self, other, f):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
89 "inplace operator"
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
90 if (hasattr(other, "__getitem__")):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
91 self.x = f(self.x, other[0])
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
92 self.y = f(self.y, other[1])
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
93 else:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
94 self.x = f(self.x, other)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
95 self.y = f(self.y, other)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
96 return self
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
97
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
98 # Addition
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
99 def __add__(self, other):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
100 if isinstance(other, Vec2d):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
101 return Vec2d(self.x + other.x, self.y + other.y)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
102 elif hasattr(other, "__getitem__"):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
103 return Vec2d(self.x + other[0], self.y + other[1])
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
104 else:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
105 return Vec2d(self.x + other, self.y + other)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
106 __radd__ = __add__
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
107
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
108 def __iadd__(self, other):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
109 if isinstance(other, Vec2d):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
110 self.x += other.x
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
111 self.y += other.y
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
112 elif hasattr(other, "__getitem__"):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
113 self.x += other[0]
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
114 self.y += other[1]
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
115 else:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
116 self.x += other
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
117 self.y += other
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
118 return self
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
119
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
120 # Subtraction
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
121 def __sub__(self, other):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
122 if isinstance(other, Vec2d):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
123 return Vec2d(self.x - other.x, self.y - other.y)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
124 elif (hasattr(other, "__getitem__")):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
125 return Vec2d(self.x - other[0], self.y - other[1])
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
126 else:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
127 return Vec2d(self.x - other, self.y - other)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
128 def __rsub__(self, other):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
129 if isinstance(other, Vec2d):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
130 return Vec2d(other.x - self.x, other.y - self.y)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
131 if (hasattr(other, "__getitem__")):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
132 return Vec2d(other[0] - self.x, other[1] - self.y)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
133 else:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
134 return Vec2d(other - self.x, other - self.y)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
135 def __isub__(self, other):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
136 if isinstance(other, Vec2d):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
137 self.x -= other.x
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
138 self.y -= other.y
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
139 elif (hasattr(other, "__getitem__")):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
140 self.x -= other[0]
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
141 self.y -= other[1]
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
142 else:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
143 self.x -= other
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
144 self.y -= other
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
145 return self
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
146
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
147 # Multiplication
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
148 def __mul__(self, other):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
149 if isinstance(other, Vec2d):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
150 return Vec2d(self.x*other.y, self.y*other.y)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
151 if (hasattr(other, "__getitem__")):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
152 return Vec2d(self.x*other[0], self.y*other[1])
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
153 else:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
154 return Vec2d(self.x*other, self.y*other)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
155 __rmul__ = __mul__
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
156
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
157 def __imul__(self, other):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
158 if isinstance(other, Vec2d):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
159 self.x *= other.x
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
160 self.y *= other.y
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
161 elif (hasattr(other, "__getitem__")):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
162 self.x *= other[0]
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
163 self.y *= other[1]
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
164 else:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
165 self.x *= other
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
166 self.y *= other
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
167 return self
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
168
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
169 # Division
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
170 def __div__(self, other):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
171 return self._o2(other, operator.div)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
172 def __rdiv__(self, other):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
173 return self._r_o2(other, operator.div)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
174 def __idiv__(self, other):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
175 return self._io(other, operator.div)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
176
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
177 def __floordiv__(self, other):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
178 return self._o2(other, operator.floordiv)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
179 def __rfloordiv__(self, other):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
180 return self._r_o2(other, operator.floordiv)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
181 def __ifloordiv__(self, other):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
182 return self._io(other, operator.floordiv)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
183
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
184 def __truediv__(self, other):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
185 return self._o2(other, operator.truediv)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
186 def __rtruediv__(self, other):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
187 return self._r_o2(other, operator.truediv)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
188 def __itruediv__(self, other):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
189 return self._io(other, operator.floordiv)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
190
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
191 # Modulo
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
192 def __mod__(self, other):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
193 return self._o2(other, operator.mod)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
194 def __rmod__(self, other):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
195 return self._r_o2(other, operator.mod)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
196
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
197 def __divmod__(self, other):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
198 return self._o2(other, divmod)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
199 def __rdivmod__(self, other):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
200 return self._r_o2(other, divmod)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
201
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
202 # Exponentation
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
203 def __pow__(self, other):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
204 return self._o2(other, operator.pow)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
205 def __rpow__(self, other):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
206 return self._r_o2(other, operator.pow)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
207
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
208 # Bitwise operators
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
209 def __lshift__(self, other):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
210 return self._o2(other, operator.lshift)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
211 def __rlshift__(self, other):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
212 return self._r_o2(other, operator.lshift)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
213
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
214 def __rshift__(self, other):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
215 return self._o2(other, operator.rshift)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
216 def __rrshift__(self, other):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
217 return self._r_o2(other, operator.rshift)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
218
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
219 def __and__(self, other):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
220 return self._o2(other, operator.and_)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
221 __rand__ = __and__
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
222
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
223 def __or__(self, other):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
224 return self._o2(other, operator.or_)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
225 __ror__ = __or__
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
226
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
227 def __xor__(self, other):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
228 return self._o2(other, operator.xor)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
229 __rxor__ = __xor__
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
230
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
231 # Unary operations
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
232 def __neg__(self):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
233 return Vec2d(operator.neg(self.x), operator.neg(self.y))
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
234
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
235 def __pos__(self):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
236 return Vec2d(operator.pos(self.x), operator.pos(self.y))
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
237
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
238 def __abs__(self):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
239 return Vec2d(abs(self.x), abs(self.y))
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
240
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
241 def __invert__(self):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
242 return Vec2d(-self.x, -self.y)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
243
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
244 # vectory functions
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
245 def get_length_sqrd(self):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
246 """Get the squared length of the vector.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
247 It is more efficent to use this method instead of first call
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
248 get_length() or access .length and then do a sqrt().
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
249
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
250 :return: The squared length
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
251 """
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
252 return self.x**2 + self.y**2
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
253
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
254 def get_length(self):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
255 """Get the length of the vector.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
256
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
257 :return: The length
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
258 """
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
259 return math.sqrt(self.x**2 + self.y**2)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
260 def __setlength(self, value):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
261 length = self.get_length()
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
262 self.x *= value/length
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
263 self.y *= value/length
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
264 length = property(get_length, __setlength, doc = """Gets or sets the magnitude of the vector""")
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
265
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
266 def rotate(self, angle_degrees):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
267 """Rotate the vector by angle_degrees degrees clockwise."""
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
268 radians = -math.radians(angle_degrees)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
269 cos = math.cos(radians)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
270 sin = math.sin(radians)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
271 x = self.x*cos - self.y*sin
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
272 y = self.x*sin + self.y*cos
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
273 self.x = x
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
274 self.y = y
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
275
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
276 def rotated(self, angle_degrees):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
277 """Create and return a new vector by rotating this vector by
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
278 angle_degrees degrees clockwise.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
279
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
280 :return: Rotated vector
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
281 """
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
282 radians = -math.radians(angle_degrees)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
283 cos = math.cos(radians)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
284 sin = math.sin(radians)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
285 x = self.x*cos - self.y*sin
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
286 y = self.x*sin + self.y*cos
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
287 return Vec2d(x, y)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
288
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
289 def get_angle(self):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
290 if (self.get_length_sqrd() == 0):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
291 return 0
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
292 return math.degrees(math.atan2(self.y, self.x))
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
293 def __setangle(self, angle_degrees):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
294 self.x = self.length
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
295 self.y = 0
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
296 self.rotate(angle_degrees)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
297 angle = property(get_angle, __setangle, doc="""Gets or sets the angle of a vector""")
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
298
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
299 def get_angle_between(self, other):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
300 """Get the angle between the vector and the other in degrees
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
301
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
302 :return: The angle
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
303 """
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
304 cross = self.x*other[1] - self.y*other[0]
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
305 dot = self.x*other[0] + self.y*other[1]
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
306 return math.degrees(math.atan2(cross, dot))
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
307
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
308 def normalized(self):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
309 """Get a normalized copy of the vector
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
310
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
311 :return: A normalized vector
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
312 """
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
313 length = self.length
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
314 if length != 0:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
315 return self/length
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
316 return Vec2d(self)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
317
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
318 def normalize_return_length(self):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
319 """Normalize the vector and return its length before the normalization
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
320
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
321 :return: The length before the normalization
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
322 """
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
323 length = self.length
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
324 if length != 0:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
325 self.x /= length
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
326 self.y /= length
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
327 return length
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
328
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
329 def perpendicular(self):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
330 return Vec2d(-self.y, self.x)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
331
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
332 def perpendicular_normal(self):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
333 length = self.length
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
334 if length != 0:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
335 return Vec2d(-self.y/length, self.x/length)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
336 return Vec2d(self)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
337
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
338 def dot(self, other):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
339 """The dot product between the vector and other vector
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
340 v1.dot(v2) -> v1.x*v2.x + v1.y*v2.y
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
341
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
342 :return: The dot product
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
343 """
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
344 return float(self.x*other[0] + self.y*other[1])
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
345
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
346 def get_distance(self, other):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
347 """The distance between the vector and other vector
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
348
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
349 :return: The distance
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
350 """
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
351 return math.sqrt((self.x - other[0])**2 + (self.y - other[1])**2)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
352
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
353 def get_dist_sqrd(self, other):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
354 """The squared distance between the vector and other vector
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
355 It is more efficent to use this method than to call get_distance()
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
356 first and then do a sqrt() on the result.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
357
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
358 :return: The squared distance
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
359 """
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
360 return (self.x - other[0])**2 + (self.y - other[1])**2
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
361
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
362 def projection(self, other):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
363 other_length_sqrd = other[0]*other[0] + other[1]*other[1]
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
364 projected_length_times_other_length = self.dot(other)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
365 return other*(projected_length_times_other_length/other_length_sqrd)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
366
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
367 def cross(self, other):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
368 """The cross product between the vector and other vector
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
369 v1.cross(v2) -> v1.x*v2.y - v2.y-v1.x
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
370
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
371 :return: The cross product
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
372 """
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
373 return self.x*other[1] - self.y*other[0]
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
374
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
375 def interpolate_to(self, other, range):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
376 return Vec2d(self.x + (other[0] - self.x)*range, self.y + (other[1] - self.y)*range)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
377
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
378 def convert_to_basis(self, x_vector, y_vector):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
379 return Vec2d(self.dot(x_vector)/x_vector.get_length_sqrd(), self.dot(y_vector)/y_vector.get_length_sqrd())
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
380
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
381 # Extra functions, mainly for chipmunk
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
382 def cpvrotate(self, other):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
383 return Vec2d(self.x*other.x - self.y*other.y, self.x*other.y + self.y*other.x)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
384 def cpvunrotate(self, other):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
385 return Vec2d(self.x*other.x + self.y*other.y, self.y*other.x - self.x*other.y)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
386
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
387 # Pickle, does not work atm.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
388 def __getstate__(self):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
389 return [self.x, self.y]
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
390
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
391 def __setstate__(self, dict):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
392 self.x, self.y = dict
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
393 def __newobj__(cls, *args):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
394 return cls.__new__(cls, *args)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
395 Vec2d._fields_ = [
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
396 ('x', ctypes.c_double),
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
397 ('y', ctypes.c_double),
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
398 ]
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
399
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
400
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
401 class Vec2dArray(list):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
402
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
403 def __init__(self, iterable=()):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
404 list.__init__(self, (Vec2d(i) for i in iterable))
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
405
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
406 def __setitem__(self, index, value):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
407 list.__setitem__(self, index, Vec2d(value))
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
408
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
409 def append(self, value):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
410 """Append a vector to the array"""
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
411 list.append(self, Vec2d(value))
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
412
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
413 def insert(self, index, value):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
414 """Insert a vector into the array"""
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
415 list.insert(self, index, Vec2d(value))
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
416
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
417 def transform(self, offset=Vec2d(0,0), angle=0, scale=1.0):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
418 """Return a new transformed Vec2dArray"""
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
419 offset = Vec2d(offset)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
420 angle = math.radians(-angle)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
421 rot_vec = Vec2d(math.cos(angle), math.sin(angle))
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
422 xformed = Vec2dArray()
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
423 for vec in self:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
424 xformed.append(vec.cpvrotate(rot_vec) * scale + offset)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
425 return xformed
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
426
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
427 def segments(self, closed=True):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
428 """Generate arrays of line segments connecting adjacent vetices
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
429 in this array, exploding the shape into it's constituent segments
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
430 """
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
431 if len(self) >= 2:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
432 last = self[0]
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
433 for vert in self[1:]:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
434 yield Vec2dArray((last, vert))
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
435 last = vert
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
436 if closed:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
437 yield Vec2dArray((last, self[0]))
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
438 elif self and closed:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
439 yield Vec2dArray((self[0], self[0]))
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
440
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
441
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
442
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
443 class Rect(ctypes.Structure):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
444 """Simple rectangle. Will gain more functionality as needed"""
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
445 _fields_ = [
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
446 ('left', ctypes.c_double),
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
447 ('top', ctypes.c_double),
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
448 ('right', ctypes.c_double),
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
449 ('bottom', ctypes.c_double),
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
450 ]
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
451
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
452 def __init__(self, rect_or_left, bottom=None, right=None, top=None):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
453 if bottom is not None:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
454 assert right is not None and top is not None, "No enough arguments to Rect"
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
455 self.left = rect_or_left
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
456 self.bottom = bottom
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
457 self.right = right
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
458 self.top = top
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
459 else:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
460 self.left = rect_or_left.left
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
461 self.bottom = rect_or_left.bottom
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
462 self.right = rect_or_left.right
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
463 self.top = rect_or_left.top
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
464
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
465 @property
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
466 def width(self):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
467 """Rectangle width"""
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
468 return self.right - self.left
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
469
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
470 @property
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
471 def height(self):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
472 """Rectangle height"""
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
473 return self.top - self.bottom
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
474
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
475
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
476 ########################################################################
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
477 ## Unit Testing ##
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
478 ########################################################################
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
479 if __name__ == "__main__":
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
480
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
481 import unittest
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
482 import pickle
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
483
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
484 ####################################################################
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
485 class UnitTestVec2d(unittest.TestCase):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
486
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
487 def setUp(self):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
488 pass
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
489
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
490 def testCreationAndAccess(self):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
491 v = Vec2d(111, 222)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
492 self.assert_(v.x == 111 and v.y == 222)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
493 v.x = 333
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
494 v[1] = 444
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
495 self.assert_(v[0] == 333 and v[1] == 444)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
496
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
497 def testMath(self):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
498 v = Vec2d(111,222)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
499 self.assertEqual(v + 1, Vec2d(112, 223))
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
500 self.assert_(v - 2 == [109, 220])
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
501 self.assert_(v * 3 == (333, 666))
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
502 self.assert_(v / 2.0 == Vec2d(55.5, 111))
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
503 #self.assert_(v / 2 == (55, 111)) # Not supported since this is a c_float structure in the bottom
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
504 self.assert_(v ** Vec2d(2, 3) == [12321, 10941048])
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
505 self.assert_(v + [-11, 78] == Vec2d(100, 300))
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
506 #self.assert_(v / [11,2] == [10,111]) # Not supported since this is a c_float structure in the bottom
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
507
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
508 def testReverseMath(self):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
509 v = Vec2d(111, 222)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
510 self.assert_(1 + v == Vec2d(112, 223))
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
511 self.assert_(2 - v == [-109, -220])
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
512 self.assert_(3 * v == (333, 666))
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
513 #self.assert_([222,999] / v == [2,4]) # Not supported since this is a c_float structure in the bottom
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
514 self.assert_([111, 222] ** Vec2d(2, 3) == [12321, 10941048])
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
515 self.assert_([-11, 78] + v == Vec2d(100, 300))
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
516
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
517 def testUnary(self):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
518 v = Vec2d(111, 222)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
519 v = -v
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
520 self.assert_(v == [-111, -222])
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
521 v = abs(v)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
522 self.assert_(v == [111, 222])
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
523
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
524 def testLength(self):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
525 v = Vec2d(3,4)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
526 self.assert_(v.length == 5)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
527 self.assert_(v.get_length_sqrd() == 25)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
528 self.assert_(v.normalize_return_length() == 5)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
529 self.assertAlmostEquals(v.length, 1)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
530 v.length = 5
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
531 self.assert_(v == Vec2d(3, 4))
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
532 v2 = Vec2d(10, -2)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
533 self.assert_(v.get_distance(v2) == (v - v2).get_length())
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
534
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
535 def testAngles(self):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
536 v = Vec2d(0, 3)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
537 self.assertEquals(v.angle, 90)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
538 v2 = Vec2d(v)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
539 v.rotate(-90)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
540 self.assertEqual(v.get_angle_between(v2), 90)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
541 v2.angle -= 90
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
542 self.assertEqual(v.length, v2.length)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
543 self.assertEquals(v2.angle, 0)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
544 self.assertEqual(v2, [3, 0])
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
545 self.assert_((v - v2).length < .00001)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
546 self.assertEqual(v.length, v2.length)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
547 v2.rotate(300)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
548 self.assertAlmostEquals(v.get_angle_between(v2), -60, 5) # Allow a little more error than usual (floats..)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
549 v2.rotate(v2.get_angle_between(v))
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
550 angle = v.get_angle_between(v2)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
551 self.assertAlmostEquals(v.get_angle_between(v2), 0)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
552
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
553 def testHighLevel(self):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
554 basis0 = Vec2d(5.0, 0)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
555 basis1 = Vec2d(0, .5)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
556 v = Vec2d(10, 1)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
557 self.assert_(v.convert_to_basis(basis0, basis1) == [2, 2])
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
558 self.assert_(v.projection(basis0) == (10, 0))
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
559 self.assert_(basis0.dot(basis1) == 0)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
560
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
561 def testCross(self):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
562 lhs = Vec2d(1, .5)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
563 rhs = Vec2d(4, 6)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
564 self.assert_(lhs.cross(rhs) == 4)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
565
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
566 def testComparison(self):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
567 int_vec = Vec2d(3, -2)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
568 flt_vec = Vec2d(3.0, -2.0)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
569 zero_vec = Vec2d(0, 0)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
570 self.assert_(int_vec == flt_vec)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
571 self.assert_(int_vec != zero_vec)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
572 self.assert_((flt_vec == zero_vec) == False)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
573 self.assert_((flt_vec != int_vec) == False)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
574 self.assert_(int_vec == (3, -2))
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
575 self.assert_(int_vec != [0, 0])
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
576 self.assert_(int_vec != 5)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
577 self.assert_(int_vec != [3, -2, -5])
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
578
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
579 def testInplace(self):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
580 inplace_vec = Vec2d(5, 13)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
581 inplace_ref = inplace_vec
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
582 inplace_src = Vec2d(inplace_vec)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
583 inplace_vec *= .5
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
584 inplace_vec += .5
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
585 inplace_vec /= (3, 6)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
586 inplace_vec += Vec2d(-1, -1)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
587 alternate = (inplace_src*.5 + .5)/Vec2d(3, 6) + [-1, -1]
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
588 self.assertEquals(inplace_vec, inplace_ref)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
589 self.assertEquals(inplace_vec, alternate)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
590
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
591 def testPickle(self):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
592 return # pickling does not work atm
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
593 testvec = Vec2d(5, .3)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
594 testvec_str = pickle.dumps(testvec)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
595 loaded_vec = pickle.loads(testvec_str)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
596 self.assertEquals(testvec, loaded_vec)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
597
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
598 ####################################################################
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
599 unittest.main()
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
600