-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvector.py
More file actions
34 lines (24 loc) · 848 Bytes
/
vector.py
File metadata and controls
34 lines (24 loc) · 848 Bytes
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
import math
class Vector(object):
def __init__(self, x, y):
self.x = float(x)
self.y = float(y)
def __str__(self):
return "(%f, %f)" % (self.x, self.y)
def __repr__(self):
return "Vector(%f, %f)" % (self.x, self.y)
def __add__(self, other):
return Vector(self.x + other.x, self.y + other.y)
def __sub__(self, other):
return Vector(self.x - other.x, self.y - other.y)
def __mul__(self, scalar):
return Vector(self.x * scalar, self.y * scalar)
def __neg__(self):
return Vector(-self.x, -self.y)
def length(self):
return math.sqrt(self.x*self.x + self.y*self.y)
def normalized(self):
L = self.length()
return Vector(self.x / L, self.y / L)
def dot(self, other):
return self.x * other.x + self.y * other.y