-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector.lua
More file actions
95 lines (78 loc) · 1.85 KB
/
vector.lua
File metadata and controls
95 lines (78 loc) · 1.85 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
module(..., package.seeall)
--
-- A basic Vector library
-- Only supports 2 dimensions, x and y
-- Supports in place (vector:add(b)) and return based (vect1 + vect2) operations
--
local function do_func(typ, a, b)
local ret = Vector:new(a.x, a.y)
ret[typ](ret,b)
return ret
end
Vector = {
__add = function(a,b) return do_func('add', a, b) end,
__sub = function(a,b) return do_func('sub', a, b) end,
__mul = function(a,b) return do_func('mul', a, b) end,
__div = function(a,b) return do_func('div', a, b) end,
__index = function(i,v,...)
if v=='add' or v=='sub' or v=='mul' or v=='div' then
return function(self,b)
b = type(b) == "table" and b or {x=b, y=b}
Vector[v](self, b)
end
else
return Vector[v]
end
end
}
function Vector:new(x,y)
return setmetatable({ x = x, y = y }, Vector)
end
function Vector:print()
print(self.x, self.y)
end
function Vector:add(b)
self.x, self.y = self.x + b.x, self.y + b.y
end
function Vector:sub(b)
self.x, self.y = self.x - b.x, self.y - b.y
end
function Vector:mul(b)
self.x, self.y = self.x * b.x, self.y * b.y
end
function Vector:div(b)
self.x, self.y = self.x / b.x, self.y / b.y
end
function Vector:norm()
local m = self:mag()
if m == 0 then m = 0.0001 end
self.x = self.x / m
self.y = self.y / m
end
function Vector:neg()
self:mul(-1)
end
function Vector:mag()
return math.sqrt(math.pow(self.x, 2) + math.pow(self.y, 2))
end
function Vector:perp()
self.x, self.y = -self.y, self.x
end
function Vector:reverse()
self.x, self.y = -self.x, -self.y;
end
function Vector:dot(b)
return self.x * b.x + self.y * b.y
end
function Vector:abs()
self.x, self.y = math.abs(self.x), math.abs(self.y)
end
function Vector:reflect(w)
w:perp()
w:norm()
w:print()
w = w * 2 * self:dot(w)
w:print()
self.x = self.x - w.x
self.y = self.y - w.y
end