-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathvector.py
More file actions
39 lines (27 loc) · 1.09 KB
/
vector.py
File metadata and controls
39 lines (27 loc) · 1.09 KB
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
import math
class Vector:
"""A three element vector used in 3D graphics for multiple purposes"""
def __init__(self, x=0.0, y=0.0, z=0.0):
self.x = x
self.y = y
self.z = z
def __str__(self):
return "({}, {}, {})".format(self.x, self.y, self.z)
def dot_product(self, other):
return self.x * other.x + self.y * other.y + self.z * other.z
def magnitude(self):
return math.sqrt(self.dot_product(self))
def normalize(self):
return self / self.magnitude()
def __add__(self, other):
return Vector(self.x + other.x, self.y + other.y, self.z + other.z)
def __sub__(self, other):
return Vector(self.x - other.x, self.y - other.y, self.z - other.z)
def __mul__(self, other):
assert not isinstance(other, Vector)
return Vector(self.x * other, self.y * other, self.z * other)
def __rmul__(self, other):
return self.__mul__(other)
def __truediv__(self, other):
assert not isinstance(other, Vector)
return Vector(self.x / other, self.y / other, self.z / other)