4 function vector.new(a, b, c)
5 if type(a) == "table" then
6 assert(a.x and a.y and a.z, "Invalid vector passed to vector.new()")
7 return {x=a.x, y=a.y, z=a.z}
9 assert(b and c, "Invalid arguments for vector.new()")
10 return {x=a, y=b, z=c}
12 return {x=0, y=0, z=0}
15 function vector.equals(a, b)
21 function vector.length(v)
22 return math.hypot(v.x, math.hypot(v.y, v.z))
25 function vector.normalize(v)
26 local len = vector.length(v)
28 return {x=0, y=0, z=0}
30 return vector.divide(v, len)
34 function vector.floor(v)
42 function vector.round(v)
44 x = math.floor(v.x + 0.5),
45 y = math.floor(v.y + 0.5),
46 z = math.floor(v.z + 0.5)
50 function vector.apply(v, func)
58 function vector.distance(a, b)
62 return math.hypot(x, math.hypot(y, z))
65 function vector.direction(pos1, pos2)
66 return vector.normalize({
73 function vector.angle(a, b)
74 local dotp = vector.dot(a, b)
75 local cp = vector.cross(a, b)
76 local crossplen = vector.length(cp)
77 return math.atan2(crossplen, dotp)
80 function vector.dot(a, b)
81 return a.x * b.x + a.y * b.y + a.z * b.z
84 function vector.cross(a, b)
86 x = a.y * b.z - a.z * b.y,
87 y = a.z * b.x - a.x * b.z,
88 z = a.x * b.y - a.y * b.x
92 function vector.add(a, b)
93 if type(b) == "table" then
94 return {x = a.x + b.x,
104 function vector.subtract(a, b)
105 if type(b) == "table" then
106 return {x = a.x - b.x,
116 function vector.multiply(a, b)
117 if type(b) == "table" then
118 return {x = a.x * b.x,
128 function vector.divide(a, b)
129 if type(b) == "table" then
130 return {x = a.x / b.x,
140 function vector.sort(a, b)
141 return {x = math.min(a.x, b.x), y = math.min(a.y, b.y), z = math.min(a.z, b.z)},
142 {x = math.max(a.x, b.x), y = math.max(a.y, b.y), z = math.max(a.z, b.z)}