-
Notifications
You must be signed in to change notification settings - Fork 0
/
vec3.py
156 lines (137 loc) · 4.46 KB
/
vec3.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
151
152
153
154
155
156
import math
class Vec3:
def __init__(self, x=0.0, y=0.0, z=0.0):
self.x = x
self.y = y
self.z = z
@classmethod
def from_scalar(cls, scalar):
if isinstance(scalar, float) or isinstance(scalar, int):
return Vec3(x=scalar, y=scalar, z=scalar)
else:
return NotImplemented
@property
def r(self):
return self.x
@property
def g(self):
return self.y
@property
def b(self):
return self.z
def __add__(self, other):
if isinstance(other, Vec3):
return Vec3(x=self.x+other.x,
y=self.y+other.y,
z=self.z+other.z)
elif isinstance(other, float) or isinstance(other, int):
return Vec3(x=self.x+other,
y=self.y+other,
z=self.z+other)
else:
return NotImplemented
def __sub__(self, other):
if isinstance(other, Vec3):
return Vec3(x=self.x-other.x,
y=self.y-other.y,
z=self.z-other.z)
elif isinstance(other, float) or isinstance(other, int):
return Vec3(x=self.x-other,
y=self.y-other,
z=self.z-other)
else:
return NotImplemented
def __mul__(self, other):
if isinstance(other, Vec3):
return Vec3(x=self.x*other.x,
y=self.y*other.y,
z=self.z*other.z)
elif isinstance(other, float) or isinstance(other, int):
return Vec3(x=self.x*other,
y=self.y*other,
z=self.z*other)
else:
return NotImplemented
def __truediv__(self, other):
if isinstance(other, Vec3):
return Vec3(x=self.x/other.x,
y=self.y/other.y,
z=self.z/other.z)
elif isinstance(other, float) or isinstance(other, int):
return Vec3(x=self.x/other,
y=self.y/other,
z=self.z/other)
else:
return NotImplemented
def __iadd__(self, other):
if isinstance(other, Vec3):
self.x+=other.x
self.y+=other.y
self.z+=other.z
return self
elif isinstance(other, float) or isinstance(other, int):
self.x+=other
self.y+=other
self.z+=other
return self
else:
return NotImplemented
def __isub__(self, other):
if isinstance(other, Vec3):
self.x-=other.x
self.y-=other.y
self.z-=other.z
return self
elif isinstance(other, float) or isinstance(other, int):
self.x-=other
self.y-=other
self.z-=other
return self
else:
return NotImplemented
def __imul__(self, other):
if isinstance(other, Vec3):
self.x*=other.x
self.y*=other.y
self.z*=other.z
return self
elif isinstance(other, float) or isinstance(other, int):
self.x*=other
self.y*=other
self.z*=other
return self
else:
return NotImplemented
def __itruediv__(self, other):
if isinstance(other, Vec3):
self.x/=other.x
self.y/=other.y
self.z/=other.z
return self
elif isinstance(other, float) or isinstance(other, int):
self.x/=other
self.y/=other
self.z/=other
return self
else:
return NotImplemented
def __neg__(self):
return Vec3(x=-self.x, y=-self.y, z=-self.z)
def cross(self, other):
if isinstance(other, Vec3):
return Vec3(x=(self.y*other.z)-(self.z*other.y),
y=(self.z*other.x)-(self.x*other.z),
z=(self.x*other.y)-(self.y*other.x))
else:
return NotImplemented
def dot(self, other):
if isinstance(other, Vec3):
return (self.x * other.x) + (self.y * other.y) + (self.z * other.z)
else:
return NotImplemented
def length(self):
return math.sqrt(self.squared_length())
def squared_length(self):
return self.x ** 2 + self.y ** 2 + self.z ** 2
def unit_vector(self):
return self / self.length()