-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector.py
150 lines (118 loc) · 5.09 KB
/
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import math
class Vector2D:
def __init__(self, x=0, y=0):
self.x = x
self.y = y
def add(self, other):
if isinstance(other, Vector2D):
return Vector2D(self.x + other.x, self.y + other.y)
elif isinstance(other, tuple) or isinstance(other, list):
return Vector2D(self.x + other[0], self.y + other[1])
elif isinstance(other, int) or isinstance(other, float):
return Vector2D(self.x + other, self.y + other)
else:
raise TypeError("Unsupported operand type(s) for +: 'Vector2D' and '{}'".format(type(other).__name__))
def sub(self, other):
if isinstance(other, Vector2D):
return Vector2D(self.x - other.x, self.y - other.y)
elif isinstance(other, tuple) or isinstance(other, list):
return Vector2D(self.x - other[0], self.y - other[1])
elif isinstance(other, int) or isinstance(other, float):
return Vector2D(self.x - other, self.y - other)
else:
raise TypeError("Unsupported operand type(s) for -: 'Vector2D' and '{}'".format(type(other).__name__))
def mul(self, other):
if isinstance(other, Vector2D):
return Vector2D(self.x * other.x, self.y * other.y)
elif isinstance(other, tuple) or isinstance(other, list):
return Vector2D(self.x * other[0], self.y * other[1])
elif isinstance(other, int) or isinstance(other, float):
return Vector2D(self.x * other, self.y * other)
else:
raise TypeError("Unsupported operand type(s) for *: 'Vector2D' and '{}'".format(type(other).__name__))
def div(self, other):
if isinstance(other, Vector2D):
return Vector2D(self.x / other.x, self.y / other.y)
elif isinstance(other, tuple) or isinstance(other, list):
return Vector2D(self.x / other[0], self.y / other[1])
elif isinstance(other, int) or isinstance(other, float):
return Vector2D(self.x / other, self.y / other)
else:
raise TypeError("Unsupported operand type(s) for /: 'Vector2D' and '{}'".format(type(other).__name__))
def __iter__(self):
yield self.x
yield self.y
def __getitem__(self, index):
if index == 0:
return self.x
elif index == 1:
return self.y
else:
raise IndexError("Index out of range")
def __setitem__(self, index, value):
if index == 0:
self.x = value
elif index == 1:
self.y = value
else:
raise IndexError("Index out of range")
def __add__(self, other):
return self.add(other)
def __radd__(self, other):
return self.add(other)
def __sub__(self, other):
return self.sub(other)
def __rsub__(self, other):
return Vector2D(0, 0).sub(self).add(other)
def __mul__(self, other):
return self.mul(other)
def __rmul__(self, other):
return self.mul(other)
def __truediv__(self, other):
return self.div(other)
def __rtruediv__(self, other):
if isinstance(other, Vector2D):
return Vector2D(other.x / self.x, other.y / self.y)
elif isinstance(other, (tuple, list)):
return Vector2D(other[0] / self.x, other[1] / self.y)
elif isinstance(other, (int, float)):
return Vector2D(other / self.x, other / self.y)
else:
raise TypeError("Unsupported operand type(s) for /: '{}' and 'Vector2D'".format(type(other).__name__))
def dot(self, other):
return self.x * other.x + self.y * other.y
def cross(self, other):
return self.x * other.y - self.y * other.x
def magnitude(self):
return math.sqrt(self.x**2 + self.y**2)
def normalize(self):
magnitude = self.magnitude()
if magnitude == 0:
return Vector2D(0, 0) #
return Vector2D(self.x / magnitude, self.y / magnitude)
def __abs__(self):
return Vector2D(abs(self.x), abs(self.y))
def __len__(self):
return self.magnitude()
def distance_to(self, other):
return Vector2D.distance(self, other)
@staticmethod
def distance(v1, v2):
return ((v1[0] - v2[0])**2 + (v1[1] - v2[1])**2)**0.5
def __str__(self):
return "Vector2D({}, {})".format(self.x, self.y)
def __neg__(self):
return Vector2D(-self.x, -self.y)
def rotate(self, angle, in_radians = True):
"""Rotate the vector by an angle in radians"""
if not in_radians:
angle = math.radians(angle)
cos = math.cos(angle)
sin = math.sin(angle)
x = self.x * cos - self.y * sin
y = self.x * sin + self.y * cos
return Vector2D(x, y)
def __eq__(self, other):
return self.x == other.x and self.y == other.y
def __ne__(self, other):
return not self == other