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.round(v)
36 x = math.floor(v.x + 0.5),
37 y = math.floor(v.y + 0.5),
38 z = math.floor(v.z + 0.5)
42 function vector.distance(a, b)
46 return math.hypot(x, math.hypot(y, z))
49 function vector.direction(pos1, pos2)
50 local x_raw = pos2.x - pos1.x
51 local y_raw = pos2.y - pos1.y
52 local z_raw = pos2.z - pos1.z
53 local x_abs = math.abs(x_raw)
54 local y_abs = math.abs(y_raw)
55 local z_abs = math.abs(z_raw)
58 y_raw = y_raw * (1 / x_abs)
59 z_raw = z_raw * (1 / x_abs)
64 x_raw = x_raw * (1 / y_abs)
65 z_raw = z_raw * (1 / y_abs)
70 x_raw = x_raw * (1 / z_abs)
71 y_raw = y_raw * (1 / z_abs)
74 return {x=x_raw, y=y_raw, z=z_raw}
78 function vector.add(a, b)
79 if type(b) == "table" then
80 return {x = a.x + b.x,
90 function vector.subtract(a, b)
91 if type(b) == "table" then
92 return {x = a.x - b.x,
102 function vector.multiply(a, b)
103 if type(b) == "table" then
104 return {x = a.x * b.x,
114 function vector.divide(a, b)
115 if type(b) == "table" then
116 return {x = a.x / b.x,