-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvector.lua
More file actions
53 lines (45 loc) · 1.33 KB
/
vector.lua
File metadata and controls
53 lines (45 loc) · 1.33 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
-- 2D vector class
vector = {}
function vector.new(x, y)
return setmetatable({x = x or 0, y = y or 0,
length = function() return math.sqrt(x * x + y * y) end,
ortho = function() return vector.new(-y, x) end,
dot = function(b) return x*b.x + y*b.y end,
abs = function() return vector.new(math.abs(x), math.abs(y)) end}, vector)
end
function vector.__add(a, b)
return vector.new(a.x + b.x, a.y + b.y)
end
function vector.__sub(a, b)
return vector.new(a.x - b.x, a.y - b.y)
end
function vector.__sub(a, b)
return vector.new(a.x - b.x, a.y - b.y)
end
function vector.__mul(a, b)
if type(a) == "number" then
return vector.new(a * b.x, a * b.y)
elseif type(b) == "number" then
return vector.new(b * a.x, b * a.y)
else
return vector.new(a.x * b.x, a.y * b.y);
end
end
function vector.__div(a, b)
if type(a) == "number" then
return vector.new(a / b.x, a / b.y)
elseif type(b) == "number" then
return vector.new(b / a.x, b / a.y)
else
return vector.new(a.x / b.x, a.y / b.y);
end
end
function vector.__eq(a, b)
return a.x == b.x and a.y == b.y
end
function vector.__tostring(a)
return "(" .. tostring(a.x) .. ", " .. tostring(a.y) .. ")"
end
function vector.__concat(a, b)
return tostring(a) .. " " .. tostring(b)
end