forked from scottjgibson/PixelPi
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvector.py
243 lines (224 loc) · 6.6 KB
/
vector.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# -*- coding: utf8 -*-
from random import *
from math import *
class Vector:
def __init__(self, x=0, y=0):
self.x = 0
self.y = 0
if isinstance(x, tuple) or isinstance(x, list):
y = x[1]
x = x[0]
elif isinstance(x, Vector):
y = x.y
x = x.x
self.set(x,y)
@staticmethod
def random(size=1):
sizex = size
sizey = size
if isinstance(size, tuple) or isinstance(size, list):
sizex = size[0]
sizey = size[1]
elif isinstance(size, Vector):
sizex = size.x
sizey = size.y
return Vector(random() * sizex, random() * sizey)
@staticmethod
def randomUnitCircle():
d = random()*pi
return Vector(cos(d)*choice([1,-1]), sin(d)*choice([1,-1]))
@staticmethod
def distance(a, b):
return (a - b).getLength()
def set(self, x,y):
self.x = x
self.y = y
def constrain(self, x,y,width,height):
if self.x < x:
self.x = x
elif self.x > x + width:
self.x = x + width
if self.y < y:
self.y = y
elif self.y > y + height:
self.y = y + height
def toArr(self): return [self.x, self.y]
def toInt(self): return Vector(int(self.x), int(self.y))
def toIntArr(self): return self.toInt().toArr()
def getNormalized(self):
if len(self) != 0:
return self / self.getLength()
else: return Vector(0,0)
def __add__(self, other):
if isinstance(other, Vector):
return Vector(self.x + other.x, self.y + other.y)
elif isinstance(other, tuple) or isinstance(other, list):
return Vector(self.x + other[0], self.y + other[1])
elif isinstance(other, int) or isinstance(other, float):
return Vector(self.x + other, self.y + other)
else:
return NotImplemented
def __sub__(self, other):
if isinstance(other, Vector):
return Vector(self.x - other.x, self.y - other.y)
if isinstance(other, tuple) or isinstance(other, list):
return Vector(self.x - other[0], self.y - other[1])
elif isinstance(other, int) or isinstance(other, float):
return Vector(self.x - other, self.y - other)
else:
return NotImplemented
def __rsub__(self, other):
if isinstance(other, Vector):
return Vector(other.x - self.x, other.y - self.y)
elif isinstance(other, tuple) or isinstance(other, list):
return Vector(other[0] - self.x, other[1] - self.y)
elif isinstance(other, int) or isinstance(other, float):
return Vector(other - self.x, other - self.y)
else:
return NotImplemented
def __mul__(self, other):
if isinstance(other, Vector):
return Vector(self.x * other.x, self.y * other.y)
elif isinstance(other, tuple) or isinstance(other, list):
return Vector(self.x * other[0], self.y * other[1])
elif isinstance(other, int) or isinstance(other, float):
return Vector(self.x * other, self.y * other)
else:
return NotImplemented
def __div__(self, other):
if isinstance(other, Vector):
return Vector(self.x / other.x, self.y / other.y)
elif isinstance(other, tuple) or isinstance(other, list):
return Vector(self.x / other[0], self.y / other[1])
elif isinstance(other, int) or isinstance(other, float):
return Vector(self.x / other, self.y / other)
else:
return NotImplemented
def __rdiv__(self, other):
if isinstance(other, Vector):
return Vector(other.x / self.x, other.y / self.y)
elif isinstance(other, tuple) or isinstance(other, list):
return Vector(other[0] / self.x, other[1] / self.y)
elif isinstance(other, int) or isinstance(other, float):
return Vector(other / self.x, other / self.y)
else:
return NotImplemented
def __pow__(self, other):
if isinstance(other, int) or isinstance(other, float):
return Vector(self.x ** other, self.y ** other)
else:
return NotImplemented
def __iadd__(self, other):
if isinstance(other, Vector):
self.x += other.x
self.y += other.y
return self
elif isinstance(other, tuple) or isinstance(other, list):
self.x += other[0]
self.y += other[1]
return self
elif isinstance(other, int) or isinstance(other, float):
self.x += other
self.y += other
return self
else:
return NotImplemented
def __isub__(self, other):
if isinstance(other, Vector):
self.x -= other.x
self.y -= other.y
return self
elif isinstance(other, tuple) or isinstance(other, list):
self.x -= other[0]
self.y -= other[1]
return self
elif isinstance(other, int) or isinstance(other, float):
self.x -= other
self.y -= other
return self
else:
return NotImplemented
def __imul__(self, other):
if isinstance(other, Vector):
self.x *= other.x
self.y *= other.y
return self
elif isinstance(other, tuple) or isinstance(other, list):
self.x *= other[0]
self.y *= other[1]
return self
elif isinstance(other, int) or isinstance(other, float):
self.x *= other
self.y *= other
return self
else:
return NotImplemented
def __idiv__(self, other):
if isinstance(other, Vector):
self.x /= other.x
self.y /= other.y
return self
elif isinstance(other, tuple) or isinstance(other, list):
self.x /= other[0]
self.y /= other[1]
return self
elif isinstance(other, int) or isinstance(other, float):
self.x /= other
self.y /= other
return self
else:
return NotImplemented
def __ipow__(self, other):
if isinstance(other, int) or isinstance(other, float):
self.x **= other
self.y **= other
return self
else:
return NotImplemented
def __eq__(self, other):
if isinstance(other, Vector):
return self.x == other.x and self.y == other.y
else:
return NotImplemented
def __ne__(self, other):
if isinstance(other, Vector):
return self.x != other.x or self.y != other.y
else:
return NotImplemented
def __gt__(self, other):
if isinstance(other, Vector):
return len(self) > len(other)
else:
return NotImplemented
def __ge__(self, other):
if isinstance(other, Vector):
return len(self) >= len(other)
else:
return NotImplemented
def __lt__(self, other):
if isinstance(other, Vector):
return len(self) < len(other)
else:
return NotImplemented
def __le__(self, other):
if isinstance(other, Vector):
return len(self) <= len(other)
else:
return NotImplemented
def __eq__(self, other):
if isinstance(other, Vector):
return self.x == other.x and self.y == other.y
else:
return NotImplemented
def __len__(self):
return int(sqrt(self.x**2 + self.y**2))
def getLength(self):
return sqrt(self.x**2 + self.y**2)
def __getitem__(self, key):
if key == "x" or key == "X" or key == 0 or key == "0":
return self.x
elif key == "y" or key == "Y" or key == 1 or key == "1":
return self.y
def __str__(self): return "[x: %(x)f, y: %(y)f]" % self
def __repr__(self): return "{'x': %(x)f, 'y': %(y)f}" % self
def __neg__(self): return Vector(-self.x, -self.y)