forked from thiago-souzaf/python-ray-tracing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector.py
More file actions
58 lines (44 loc) · 1.71 KB
/
Copy pathvector.py
File metadata and controls
58 lines (44 loc) · 1.71 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import math
class Vector:
"""
Representa um vetor em um espaço tridimensional.
Atributos:
x (float): Componente do vetor na direção X.
y (float): Componente do vetor na direção Y.
z (float): Componente do vetor na direção Z.
"""
def __init__(self, x: float, y: float, z:float):
self.x = x
self.y = y
self.z = z
def __str__(self):
return f"({self.x}, {self.y}, {self.z})"
def __add__(self, vector):
return Vector(self.x + vector.x, self.y + vector.y, self.z + vector.z)
def __sub__(self, vector):
return Vector(self.x - vector.x, self.y - vector.y, self.z - vector.z)
def magnitude(self):
return (self.x**2 + self.y**2 + self.z**2) ** 0.5
def normalize(self):
return Vector(self.x / self.magnitude(), self.y / self.magnitude(), self.z / self.magnitude())
def cross_product(self, vector):
return Vector(
self.y * vector.z - self.z * vector.y,
self.z * vector.x - self.x * vector.z,
self.x * vector.y - self.y * vector.x
)
def dot_product(self, vector):
return self.x * vector.x + self.y * vector.y + self.z * vector.z
def scale(self, t):
return Vector(self.x * t, self.y * t, self.z * t)
def normalize(self):
return Vector(
self.x / self.magnitude(),
self.y / self.magnitude(),
self.z / self.magnitude()
)
def angle(self, vector):
# Calcula o ângulo entre dois vetores usando o produto escalar
dot_product = self.dot_product(vector)
magnitudes = self.magnitude() * vector.magnitude()
return math.acos(dot_product / magnitudes)