summaryrefslogtreecommitdiff
path: root/math.lua
blob: 4a1351766b9c7ac1821eae4beb3b844d29eb8386 (plain) (blame)
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
Vec2 = {}
Vec2.__index = Vec2
Vec2.__add = function(a, b) return vec2(a.x + b.x, a.y + b.y) end
Vec2.__unm = function(a) return vec2(-a.x, -a.y) end
Vec2.__sub = function(a, b) return a + (-b) end
Vec2.__mul = function(a, b)
    if (type(b) == "number") then return vec2(a.x * b, a.y * b) end -- scalar mult
    return a.x * b.x + a.y * b.y
end
Vec2.__div = function(a, b) return vec2(a.x / b.x, a.y / b.y) end
Vec2.__tostring = function(a) return "(" .. fmt(a.x) .. "," .. fmt(a.y) .. ")" end

function Vec2:magnitude()
    return sqrt(self.x * self.x + self.y * self.y)
end

function Vec2:normal()
    local m = self:magnitude()
    if m == 0 then return vec2(self.x, self.y) end
    return vec2(self.x / m, self.y / m)
end

function Vec2:apply(f)
    return vec2(f(self.x), f(self.y))
end

function vec2(x, y)
    if type(x) == "table" then return vec2(x.x, x.y) end
    return setmetatable({ x = x, y = y }, Vec2)
end

function normalize_scalar(x)
    if x == 0 then return 0 end
    if x < 0 then return -1 end
    return 1
end