annotate src/parpg/bGrease/geometry.py @ 144:a6b25c2cedab

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