Advertisement
LuaWeaver

Vector class for wOOP 0.5

Nov 8th, 2012
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.07 KB | None | 0 0
  1. class "vector"
  2. {
  3.     x=0,
  4.     y=0,
  5.  
  6.     magnitude=function()
  7.         return math.sqrt(self.x^2+self.y^2)
  8.     end,
  9.     unit=function()
  10.         return self/self:magnitude()
  11.     end,
  12.  
  13.     coords=function()
  14.         return self.x,self.y
  15.     end,
  16.  
  17.     __settings=
  18.     {
  19.         new=function(x, y)
  20.             return {x=x,y=y}
  21.         end
  22.     },
  23.    
  24.     __omt=
  25.     {
  26.         __tostring=
  27.         function(a)
  28.             return "Vector : "..a.x..", "..a.y
  29.         end,
  30.  
  31.         __add=
  32.         function(a,b)
  33.             if tostring(b)=="Vector2" then
  34.                 return newObj("vector", a.x+b.x,a.y+b.y)
  35.             else
  36.                 return newObj("vector", a.x+b,a.y+b)
  37.             end
  38.         end,
  39.  
  40.         __sub=
  41.         function(a,b)
  42.             if tostring(b)=="Vector" then
  43.                 return newObj("vector", a.x-b.x,a.y-b.y)
  44.             else
  45.                 return newObj("vector", a.x-b,a.y-b)
  46.             end
  47.         end,
  48.  
  49.         __div=
  50.         function(a,b)
  51.             if tostring(b)=="Vector" then
  52.                 return newObj("vector", a.x/b.x,a.y/b.y)
  53.             else
  54.                 return newObj("vector", a.x/b,a.y/b)
  55.             end
  56.         end,
  57.  
  58.         __mul=
  59.         function(a,b)
  60.             if tostring(b)=="Vector" then
  61.                 return newObj("vector", a.x*b.x,a.y*b.y)
  62.             else
  63.                 return newObj("vector", a.x*b,a.y*b)
  64.             end
  65.         end
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement