Repository navigation
Expand file tree
/
Copy pathvector.lua
More file actions
207 lines (187 loc) · 4.51 KB
/
Copy pathvector.lua
File metadata and controls
207 lines (187 loc) · 4.51 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
local math = require "math"
local concat = require "table".concat
local format = require "string".format
local setmetatable = setmetatable
local type, error = type, error
local rawset, rawget = rawset, rawget
-- One of those cases where you really miss the "using" keyword.
local pow, sqrt, max, floor, acos, sin, cos, modf, pi = math.pow, math.sqrt, math.max, math.floor, math.acos, math.sin, math.cos, math.modf, math.pi
math = nil
local vector
local function check(o) -- Vector type check
local t = type(o)
if not (t == "table" and o.object == "vector") then
error("attempt to perform arithmetic on a "..t.." value", 3)
end
end
local __index = {
tostring = function(self, f)
f = type(f) == "number" and f >= 0 and f or 3
f = "%."..f.."f"
local r = { }
for i = 1, self.n do
local n = self[i]
r[i] = format(modf(n) == n and "%1d" or f, n)
end
return "{"..concat(r, "; ").."}"
end;
add = function(a, b)
check(a)
check(b)
local v = { }
for i = 1, max(a.n, b.n) do
v[i] = (a[i] or 0) + (b[i] or 0)
end
return vector(v)
end;
sub = function(a, b)
check(a)
check(b)
local v = { }
for i = 1, max(a.n, b.n) do
v[i] = (a[i] or 0) - (b[i] or 0)
end
return vector(v)
end;
mul = function(a, b)
local t1 = type(a)
local t2 = type(b)
local v = { }
if t1 == "number" then
check(b)
for i = 1, b.n do
v[i] = b[i] * a
end
elseif t2 == "number" then
check(a)
for i = 1, a.n do
v[i] = a[i] * b
end
else
check(a)
check(b)
v = a:cross(b)
end
return vector(v)
end;
div = function(a, b)
check(a)
local t = type(b)
if t ~= "number" then
error("attempt to perform arithmetic on a "..t.." value", 3)
end
local v = { }
for i = 1, a.n do
v[i] = a[i] / b
end
return vector(v)
end;
len = function(self)
local r = 0
for i = 1, self.n do
r = r + pow(self[i], 2)
end
return sqrt(r)
end;
dot = function(a, b)
check(a)
check(b)
local r = 0
for i = 1, max(a.n, b.n) do
r = r + (a[i] or 0) * (b[i] or 0)
end
return r
end;
cross = function(a, b)
check(a)
check(b)
local v = {
(a[2] or 0) * (b[3] or 0) - (a[3] or 0) * (b[2] or 0);
(a[3] or 0) * (b[1] or 0) - (a[1] or 0) * (b[3] or 0);
(a[1] or 0) * (b[2] or 0) - (a[2] or 0) * (b[1] or 0);
}
return vector(v)
end;
normalize = function(self)
return self / #self
end;
angle = function(a, b)
check(a)
check(b)
a, b = a:normalize(), b:normalize()
return acos(a:dot(b))
end;
rotate = function(v, g, a, b)
local r = { }
local c, s = cos(g), sin(g)
for i = 1, v.n do
r[i] = i == a and v[a] * c - v[b] * s or b == i and v[a] * s + v[b] * c or v[i]
end
return vector(r)
end;
round = function(self, d)
local v = { }
d = type(d) == "number" and d or 0.5
for i = 1, self.n do
local a, b = modf(self[i])
v[i] = b < d and a or a + 1
end
return vector(v)
end;
eq = function(a, b)
if not (type(a) == "table" and a.object == "vector") or not (type(a) == "table" and a.object == "vector") then
return false
end
local r = true
for i = 1, max(a.n, b.n) do
if a[i] ~= b[i] then
r = false
break
end
end
return r
end;
object = "vector"; -- Object type
}
local meta = {
__index = __index;
__tostring = __index.tostring;
__add = __index.add;
__sub = __index.sub;
__mul = __index.mul;
__div = __index.div;
__len = __index.len;
__eq = __index.eq;
__newindex = function(self, d, n)
local a = type(d)
local b = type(n)
if not (a == "number" and d == floor(d) and d > 0) then
error("invalid dimension type (signed integer expected, got "..(a == "number" and d ~= floor(d) and "float" or a)..")", 2)
end
if b ~= "number" then
error("invalid coordinate type (number expected, got "..b..")")
end
d = floor(d)
for i = 1, d-1 do
if not rawget(self, i) then
rawset(self, i, 0)
end
end
rawset(self, d, n)
rawset(self, "n", d)
end;
}
vector = function(v) -- Creates a new vector
v.n = #v
return setmetatable(v, meta)
end
return function(...) -- The same as above, but with additional checks
local v = { ... }
for i=1, #v do
local t = type(v[i])
if t ~= "number" then
error("bad argument #"..i.." (number expected, got "..t..")")
end
end
return vector(v)
end