-
Notifications
You must be signed in to change notification settings - Fork 1
/
vector2.py
executable file
·62 lines (47 loc) · 1.53 KB
/
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/python
# coding: latin-1
import math
class Vector2(object):
def __init__(self, x=0.0, y=0.0):
self.x = x
self.y = y
def __str__(self):
return "(%s, %s)"%(self.x, self.y)
def _get_length(self):
x, y = self.x, self.y
return sqrt(x*x + y*y)
def _set_length(self, length):
try:
x, y = self.x, self.y
l = length / sqrt(x*x +y*y)
except ZeroDivisionError:
v[0] = 0.0
v[1] = 0.0
return self
v[0] *= l
v[1] *= l
length = property(_get_length, _set_length, None, "Length of the vector")
@staticmethod
def from_points(P1, P2):
return Vector2(P2[0] - P1[0], P2[1] - P1[1])
def get_magnitude(self):
return math.sqrt( self.x**2 + self.y**2 )
def normalize(self):
magnitude = self.get_magnitude()
try:
self.x /= magnitude
self.y /= magnitude
except ZeroDivisionError:
self.x = 0
self.y = 0
# rhs stands for Right Hand Side
def __add__(self, rhs):
return Vector2(self.x + rhs.x, self.y + rhs.y)
def __sub__(self, rhs):
return Vector2(self.x - rhs.x, self.y - rhs.y)
def __neg__(self):
return Vector2(-self.x , -self.y)
def __mul__(self, scalar):
return Vector2(self.x * scalar, self.y * scalar)
def __div__(self, scalar):
return Vector2(self.x / scalar, self.y / scalar)