-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector.py
More file actions
29 lines (21 loc) · 833 Bytes
/
vector.py
File metadata and controls
29 lines (21 loc) · 833 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
import math
class Vector3:
def __init__(self, x: float, y: float, z: float):
self.x = x
self.y = y
self.z = z
def __sub__(self, other):
return Vector3(self.x - other.x, self.y - other.y, self.z - other.z)
def __add__(self, other):
return Vector3(self.x + other.x, self.y + other.y, self.z + other.z)
def __mul__(self, scalar: float):
return Vector3(self.x * scalar, self.y * scalar, self.z * scalar)
def __neg__(self):
return Vector3(-self.x, -self.y, -self.z)
def dot(self, other):
return self.x * other.x + self.y * other.y + self.z * other.z
def length(self):
return math.sqrt(self.dot(self))
def normalize(self):
length = self.length()
return Vector3(self.x / length, self.y / length, self.z / length)