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)
40 function vec2:__sub(v)
41 assert(isvector(v), "Expected a vec2 type")
42 return vec2:new(self.x - v.x, self.y - v.y)
45 function vec2:__mul(a)
46 if type(a) == 'number' then
47 return vec2:new(self.x * a, self.y * a)
49 assert(isvector(a), "Expected a number or vec2 type")
50 return vec2:new(self.x * a.x, self.y * a.y)
54 function vec2:__div(a)
55 if type(a) == 'number' then
56 return vec2:new(self.x / a, self.y / a)
58 assert(isvector(a), "Expected a number or vec2 type")
59 return vec2:new(self.x / a.x, self.y / a.y)