-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTools.py
More file actions
97 lines (83 loc) · 2.09 KB
/
Tools.py
File metadata and controls
97 lines (83 loc) · 2.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
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
""" ---------- 3D Vector ----------- """
class Vec_3:
def __init__(self, i, j, k):
self.i = i
self.j = j
self.k = k
def __add__(self, other):
# add
i = self.i + other.i
j = self.j + other.j
k = self.k + other.k
return Vec_3(i, j, k)
def __sub__(self, other):
# subtract
i = self.i - other.i
j = self.j - other.j
k = self.k - other.k
return Vec_3(i, j, k)
def __mul__(self, other):
# scalar multiplication
i = self.i * other
j = self.j * other
k = self.k * other
return Vec_3(i, j, k)
def __truediv__(self, other):
# scalar multiplication
i = self.i / other
j = self.j / other
k = self.k / other
return Vec_3(i, j, k)
def __floordiv__(self, other):
# scalar multiplication
i = self.i // other
j = self.j // other
k = self.k // other
return Vec_3(i, j, k)
def show(self):
print(self.i, self.j, self.k)
return
def dot(a, b):
# dot product
i = a.i * b.i
j = a.j * b.j
k = a.k * b.k
return i + j + k
def cross(a, b):
# cross product
i = a.j * b.k - a.k * b.j
j = a.k * b.i - a.i * b.k
k = a.i * b.j - a.j * b.i
return Vec_3(i, j, k)
def mag(a):
# magnitude
d = dot(a, a)
return d**(.5)
def norm(a):
m = mag(a)
if m != 0:
return a / m
print("Normalized zero vector.")
return 0/0
def mat_multi(M, v):
"""
M example:
M = [
[a, b, c],
[d, e, f],
[g, h, i]
]
"""
if len(M) != 3:
print("Invalid Matrix.")
return 0/0
if len(M[0]) != 3 or len(M[1]) != 3 or len(M[2]) != 3:
print("Invalid Matrix.")
return 0/0
r1 = Vec_3(M[0][0], M[0][1], M[0][2])
i = dot(r1, v)
r2 = Vec_3(M[1][0], M[1][1], M[1][2])
j = dot(r2, v)
r3 = Vec_3(M[2][0], M[2][1], M[2][2])
k = dot(r3, v)
return Vec_3(i, j, k)