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
130
131
132
133
134
135
136
|
vector = {}
function vector.new(a, b, c)
v = {x=0, y=0, z=0}
if type(a) == "table" then
v = {x=a.x, y=a.y, z=a.z}
elseif a and b and c then
v = {x=a, y=b, z=c}
end
setmetatable(v, {
__add = vector.add,
__sub = vector.subtract,
__mul = vector.multiply,
__div = vector.divide,
__umn = function(v) return vector.multiply(v, -1) end,
__len = vector.length,
__eq = vector.equals,
})
return v
end
function vector.equals(a, b)
return a.x == b.x and
a.y == b.y and
a.z == b.z
end
function vector.length(v)
return math.hypot(v.x, math.hypot(v.y, v.z))
end
function vector.normalize(v)
return vector.divide(v, vector.length(v))
end
function vector.round(v)
return {
x = math.floor(v.x + 0.5),
y = math.floor(v.y + 0.5),
z = math.floor(v.z + 0.5)
}
end
function vector.distance(a, b)
local x = a.x - b.x
local y = a.y - b.y
local z = a.z - b.z
return math.hypot(x, math.hypot(y, z))
end
function vector.direction(pos1, pos2)
local x_raw = pos2.x - pos1.x
local y_raw = pos2.y - pos1.y
local z_raw = pos2.z - pos1.z
local x_abs = math.abs(x_raw)
local y_abs = math.abs(y_raw)
local z_abs = math.abs(z_raw)
if x_abs >= y_abs and
x_abs >= z_abs then
y_raw = y_raw * (1 / x_abs)
z_raw = z_raw * (1 / x_abs)
x_raw = x_raw / x_abs
end
if y_abs >= x_abs and
y_abs >= z_abs then
x_raw = x_raw * (1 / y_abs)
z_raw = z_raw * (1 / y_abs)
y_raw = y_raw / y_abs
end
if z_abs >= y_abs and
z_abs >= x_abs then
x_raw = x_raw * (1 / z_abs)
y_raw = y_raw * (1 / z_abs)
z_raw = z_raw / z_abs
end
return {x=x_raw, y=y_raw, z=z_raw}
end
function vector.add(a, b)
if type(b) == "table" then
return vector.new(
a.x + b.x,
a.y + b.y,
a.z + b.z)
else
return vector.new(
a.x + b,
a.y + b,
a.z + b)
end
end
function vector.subtract(a, b)
if type(b) == "table" then
return vector.new(
a.x - b.x,
a.y - b.y,
a.z - b.z)
else
return vector.new(
a.x - b,
a.y - b,
a.z - b)
end
end
function vector.multiply(a, b)
if type(b) == "table" then
return vector.new(
a.x * b.x,
a.y * b.y,
a.z * b.z)
else
return vector.new(
a.x * b,
a.y * b,
a.z * b)
end
end
function vector.divide(a, b)
if type(b) == "table" then
return vector.new(
a.x / b.x,
a.y / b.y,
a.z / b.z)
else
return vector.new(
a.x / b,
a.y / b,
a.z / b)
end
end
|