-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vector2.py
39 lines (29 loc) · 852 Bytes
/
Vector2.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
'''
Created on 15 May 2010
@author: David Schneider
'''
import math
class Vector2():
'''
Stores state and behaviour of a two-dimensional vector.
'''
def __init__(self, x=0.0, y=0.0):
'''
Constructor
'''
self.x = x
self.y = y
def __add__(self, vec2):
return Vector2(self.x + vec2.x, self.y + vec2.y)
def __sub__(self, vec2):
return Vector2(self.x - vec2.x, self.y - vec2.y)
def __mul__(self, val):
return Vector2(self.x * val, self.y * val)
def __div__(self, val):
return Vector2(self.x / val, self.y / val)
def length(self):
return math.sqrt(self.x*self.x + self.y*self.y)
def normalise(self):
length = self.length()
self.x = self.x / length
self.y = self.y / length