-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector.py
129 lines (96 loc) · 3.37 KB
/
vector.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
import math
from math import pow, sqrt
import colorsys
class Vector3:
def __init__(self, x=0, y=0, z=0):
self.x = x
self.y = y
self.z = z
self.w = 1
def __add__(a, b):
if type(b) == Vector3:
return Vector3(a.x + b.x, a.y + b.y, a.z + b.z)
return Vector3(a.x + b, a.y + b, a.z + b)
def __sub__(a, b):
if type(b) == Vector3:
return Vector3(a.x - b.x, a.y - b.y, a.z - b.z)
return Vector3(a.x - b, a.y - b, a.z - b)
def __mul__(a, b):
if type(b) == Vector3:
return Vector3(a.x * b.x, a.y * b.y, a.z * b.z)
return Vector3(a.x * b, a.y * b, a.z * b)
def __truediv__(a, b):
if type(b) == Vector3:
return Vector3(a.x / b.x, a.y / b.y, a.z / b.z)
return Vector3(a.x / b, a.y / b, a.z / b)
def norm(self):
mg = sqrt(pow(self.x, 2) + pow(self.y, 2) + pow(self.z, 2))
if mg == 0:
self.x, self.y, self.z = 0, 0, 0
else:
self.x, self.y, self.z = self.x / mg, self.y / mg, self.z / mg
def dot(self, b):
return self.x * b.x + self.y * b.y + self.z * b.z
def toMatrix(self):
return [[self.x, self.y, self.z, self.w]]
def GetTuple(self):
return (int(self.x), int(self.y))
def __repr__(self):
# debug
return f" vec3-> ({self.x}, {self.y}, {self.z})"
def get_dist(self, b):
return math.sqrt((self.x - b.x) * (self.x - b.x) +
(self.y - b.y) * (self.y - b.y) + (self.z - b.z) * (self.z - b.z))
class Vector2:
def __init__(self, x=0, y=0):
self.x = x
self.y = y
def __add__(a, b):
if type(b) == Vector2:
return Vector2(a.x + b.x, a.y + b.y)
return Vector2(a.x + b, a.y + b)
def __sub__(a, b):
if type(b) == Vector2:
return Vector2(a.x - b.x, a.y - b.y)
return Vector2(a.x - b, a.y - b)
def __mul__(a, b):
if type(b) == Vector2:
return Vector2(a.x * b.x, a.y * b.y)
return Vector2(a.x * b, a.y * b)
def __truediv__(a, b):
if type(b) == Vector2:
return Vector2(a.x / b.x, a.y / b.y)
return Vector2(a.x / b, a.y / b)
def __repr__(self):
return f"vec2-> ({self.x}, {self.y})"
def get_dist(self, b):
return math.sqrt((self.x - b.x) * (self.x - b.x) +
(self.y - b.y) * (self.y - b.y))
def toVector3(matrix):
return Vector3(matrix.val[0][0], matrix.val[0][1], matrix.val[0][2])
def crossProduct(a, b):
x = a.y * b.z - a.z * b.y
y = a.z * b.x - a.x * b.z
z = a.x * b.y - a.y * b.x
return Vector3(x, y, z)
def dotProduct(a, b):
return a.x * b.x + a.y * b.y + a.z * b.z
def GetMagnitude(a):
if type(a) == Vector3:
return sqrt(pow(a.x, 2) + pow(a.y, 2) + pow(a.z, 2))
else:
return sqrt(pow(a.x, 2) + pow(a.y, 2))
def Normalize(a):
mg = GetMagnitude(a)
if mg == 0:
return Vector3()
return Vector3(a.x / mg, a.y / mg, a.z / mg)
def PlaneLineIntersection(pos, normal, lineStart, lineEnd):
normal = Normalize(normal)
p = - dotProduct(normal, pos)
ad = dotProduct(lineStart, normal)
bd = dotProduct(lineEnd, normal)
t = (-p - ad) / (bd - ad)
lineStartEnd = lineEnd - lineStart
lineTointersect = lineStartEnd * t
return (lineStart + lineTointersect)