annotate bGrease/geometry.py @ 179:90b2545f7459

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