-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector.py
33 lines (28 loc) · 910 Bytes
/
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
import math
class Vector():
'''
Class:
creates operations to handle vectors such
as direction, position, and speed
'''
def __init__(self, x, y):
self.x = x
self.y = y
def __str__(self): # used for printing vectors
return "(%s, %s)"%(self.x, self.y)
def __getitem__(self, key):
if key == 0:
return self.x
elif key == 1:
return self.y
else:
raise IndexError("This "+str(key)+" key is not a vector key!")
def __sub__(self, o): # subtraction
return Vector(self.x - o.x, self.y - o.y)
def length(self): # get length (used for normalize)
return math.sqrt((self.x**2 + self.y**2))
def normalize(self): # divides a vector by its length
l = self.length()
if l != 0:
return (self.x / l, self.y / l)
return None