Advertisement
TwoThe

OC Vector

Sep 13th, 2014
556
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.64 KB | None | 0 0
  1. local class = {}
  2.  
  3. function class.new(x, y, z)
  4.   return setmetatable({["x"] = (x or 0), ["y"] = (y or 0), ["z"] = (z or 0)}, {__index = class})
  5. end
  6.  
  7. function class.add(self, other)
  8.   assert(type(self) == "table", "Use : for operation")
  9.   assert(type(other) == "table", "Unable to add "..type(other).." to vector")
  10.   self.x = self.x + other.x
  11.   self.y = self.y + other.y
  12.   self.z = self.z + other.z
  13.   return self
  14. end
  15.  
  16. function class.sub(self, other)
  17.   assert(type(self) == "table", "Use : for operation")
  18.   assert(type(other) == "table", "Unable to sub "..type(other).." from vector")
  19.   self.x = self.x - other.x
  20.   self.y = self.y - other.y
  21.   self.z = self.z - other.z
  22.   return self
  23. end
  24.  
  25. function class.multiply(self, scale)
  26.   assert(type(self) == "table", "Use : for operation")
  27.   assert(type(scale) == "number", "Unable to multiply "..type(scale).." with vector")
  28.   self.x = self.x * scale
  29.   self.y = self.y * scale
  30.   self.z = self.z * scale
  31.   return self
  32. end
  33.  
  34. function class.divide(self, scale)
  35.   assert(type(self) == "table", "Use : for operation")
  36.   assert(type(scale) == "number", "Unable to divide "..type(scale).." with vector")
  37.   self.x = self.x / scale
  38.   self.y = self.y / scale
  39.   self.z = self.z / scale
  40.   return self
  41. end
  42.  
  43. function class.dot(self, other)
  44.   assert(type(self) == "table", "Use : for operation")
  45.   assert(type(other) == "table", "Unable to dot "..type(other).." to vector")
  46.   return (self.x * other.x + self.y * other.y + self.z * other.z)
  47. end
  48.  
  49. function class.cross(self, other)
  50.   assert(type(self) == "table", "Use : for operation")
  51.   assert(type(other) == "table", "Unable to cross "..type(other).." to vector")
  52.   return class.new(self.y * other.z - self.z * other.y,
  53.                    self.z * other.x - self.x * other.z,
  54.                    self.x * other.y - self.y * other.x)
  55. end
  56.  
  57. function class.round(self)
  58.   assert(type(self) == "table", "Use : for operation")
  59.   self.x = math.floor(self.x + 0.5)
  60.   self.y = math.floor(self.y + 0.5)
  61.   self.z = math.floor(self.z + 0.5)
  62.   return self  
  63. end
  64.  
  65. function class.length(self)
  66.   assert(type(self) == "table", "Use : for operation")
  67.   return math.sqrt(self.x * self.x + self.y * self.y + self.z * self.z)
  68. end
  69.  
  70. function class.normalize(self)
  71.   assert(type(self) == "table", "Use : for operation")
  72.   return class.divide(self, class.length(self))
  73. end
  74.  
  75. function class.copy(self)
  76.   assert(type(self) == "table", "Use : for operation")
  77.   return class.new(self.x, self.y, self.z)
  78. end
  79.  
  80. function class.toString(self)
  81.   assert(type(self) == "table", "Use : for operation")
  82.   return string.format("[%.3f, %.3f, %.3f]", self.x, self.y, self.z)
  83. end
  84.  
  85. return class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement