-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvectors.lua
64 lines (57 loc) · 1.68 KB
/
vectors.lua
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
-- Vectors library using FFI. Uses less memory than Lua tables.
local sqrt = math.sqrt
local ffi = require("ffi")
local Vector = {}
setmetatable(Vector, {__call = function(self,...) return Vector.new(...) end})
Vector.__index = Vector
local new_vector3 = ffi.metatype("struct { double x, y, z; }",Vector)
function Vector.new(x,y,z)
return new_vector3(x or 0, y or 0, z or 0)
end
function Vector:__add(other)
return new_vector3(self.x+other.x, self.y+other.y, self.z+other.z)
end
function Vector:__sub(other)
return new_vector3(self.x-other.x, self.y-other.y, self.z-other.z)
end
function Vector:__mul(other)
return new_vector3(self.x*other, self.y*other, self.z*other)
end
function Vector:__div(other)
return new_vector3(self.x/other, self.y/other, self.z/other)
end
function Vector:__unm()
return new_vector3(-self.x, -self.y, -self.z)
end
function Vector:__tostring()
return string.format("<%f,%f,%f>",self.x,self.y,self.z)
end
function Vector:len()
return sqrt(self.x*self.x+self.y*self.y+self.z*self.z)
end
Vector.__len = Vector.len
function Vector:len2()
return self.x*self.x+self.y*self.y+self.z*self.z
end
function Vector:normalized()
return self/self:len()
end
Vector.normalize = Vector.normalized
function Vector:dot(o)
return self.x*o.x + self.y*o.y + self.z*o.z
end
function Vector:cross(other)
return new_vector3(self.y*other.z-self.z*other.y, self.z*other.x - self.x*other.z, self.x*other.y - self.y*other.x)
end
function Vector:set(x,y,z)
self.x = x
self.y = y
self.z = z
end
function Vector:unpack()
return self.x, self.y, self.z
end
function Vector:copy()
return new_vector3(self.x, self.y, self.z)
end
return Vector