7 return setmetatable({x=x, y=y}, self)
10 function vec2:length()
11 return math.sqrt(self.x * self.x + self.y * self.y)
14 function vec2:normalize()
15 local len = self:length()
22 function vec2:__tostring()
23 return "("..tonumber(self.x)..","..tonumber(self.y)..")"
26 local function isvector(v)
27 return type(v) == 'table' and type(v.x) == 'number' and type(v.y) == 'number'
31 assert(isvector(v), "Expected a vec2 type")
32 return self.x == v.x and self.y == v.y
35 function vec2:__add(v)
36 assert(isvector(v), "Expected a vec2 type")
37 return vec2:new(self.x + v.x, self.y + v.y)
41 assert(isvector(v), "Expected a vec2 type")
46 function vec2:__sub(v)
47 assert(isvector(v), "Expected a vec2 type")
48 return vec2:new(self.x - v.x, self.y - v.y)
51 function vec2:__mul(a)
52 if type(a) == 'number' then
53 return vec2:new(self.x * a, self.y * a)
55 assert(isvector(a), "Expected a number or vec2 type")
56 return vec2:new(self.x * a.x, self.y * a.y)
60 function vec2:__div(a)
61 if type(a) == 'number' then
62 return vec2:new(self.x / a, self.y / a)
64 assert(isvector(a), "Expected a number or vec2 type")
65 return vec2:new(self.x / a.x, self.y / a.y)