-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector.py
43 lines (34 loc) · 811 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
34
35
36
37
38
39
40
41
#!/usr/bin/python
# -*- coding: utf-8 -*-
import math
class Vector(object):
x = None
y = None
def __init__(self, x, y):
"""
2d vector class
@param x: x component
@param y: y component
"""
self.x = float(x)
self.y = float(y)
def dot(self, v2):
"""
Vector dot product
"""
return self.x * v2.x + self.y * v2.y
def length(self):
"""
Vector Length
"""
return math.sqrt(self.x * self.x + self.y * self.y)
def project(self, b):
"""
Vector Projection
@return: The vector created when self is projected onto b
"""
proj = Vector(0, 0)
val = self.dot(b) / b.dot(b)
proj.x = val * b.x
proj.y = val * b.y
return proj