-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvectors.py
81 lines (56 loc) · 1.29 KB
/
vectors.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
#
# Author Dario Clavijo 2016
# GPLv3
import math
def multiply(a, b):
return [a[0] * b[0], a[1] * b[1]]
def multiplyScalar(a, scalar):
return [a[0] * scalar, a[1] * scalar]
def dot(a, b):
return a[0] * b[0] + a[1] * b[1] + a[2] * b[2]
def cross(a, b):
ax = a[0]
ay = a[1]
az = a[2]
bx = b[0]
by = b[1]
bz = b[2]
rx = ay * bz - az * by
ry = az * bx - ax * bz
rz = ax * by - ay * bx
return [rx, ry, rz]
def length(vec):
x = vec[0]
y = vec[1]
z = vec[2]
return math.sqrt(x * x + y * y + z * z)
def normalize(vec):
x = vec[0]
y = vec[1]
z = vec[2]
squaredLength = x * x + y * y + z * z
if squaredLength > 0:
length = math.sqrt(squaredLength)
vec[0] = vec[0] / length
vec[1] = vec[1] / length
vec[2] = vec[2] / length
return vec
def magnitude(vec):
res = sum(vec[n] * vec[n] for n in range(0, len(vec)))
return math.sqrt(res)
def tests():
vec = [0, 4, -3]
print((length(vec)))
print((normalize(vec)))
s = 3
k = [1, 2]
j = [2, 3]
tmp = multiply(k, j)
print(tmp)
print((multiplyScalar(tmp, s)))
k = [0, 1, 0]
j = [1, 0, 0]
print((dot(k, j)))
print((cross(k, j)))
print((magnitude(j)))
print((magnitude(k)))