Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local class = {}
- function class.new(x, y, z)
- return setmetatable({["x"] = (x or 0), ["y"] = (y or 0), ["z"] = (z or 0)}, {__index = class})
- end
- function class.add(self, other)
- assert(type(self) == "table", "Use : for operation")
- assert(type(other) == "table", "Unable to add "..type(other).." to vector")
- self.x = self.x + other.x
- self.y = self.y + other.y
- self.z = self.z + other.z
- return self
- end
- function class.sub(self, other)
- assert(type(self) == "table", "Use : for operation")
- assert(type(other) == "table", "Unable to sub "..type(other).." from vector")
- self.x = self.x - other.x
- self.y = self.y - other.y
- self.z = self.z - other.z
- return self
- end
- function class.multiply(self, scale)
- assert(type(self) == "table", "Use : for operation")
- assert(type(scale) == "number", "Unable to multiply "..type(scale).." with vector")
- self.x = self.x * scale
- self.y = self.y * scale
- self.z = self.z * scale
- return self
- end
- function class.divide(self, scale)
- assert(type(self) == "table", "Use : for operation")
- assert(type(scale) == "number", "Unable to divide "..type(scale).." with vector")
- self.x = self.x / scale
- self.y = self.y / scale
- self.z = self.z / scale
- return self
- end
- function class.dot(self, other)
- assert(type(self) == "table", "Use : for operation")
- assert(type(other) == "table", "Unable to dot "..type(other).." to vector")
- return (self.x * other.x + self.y * other.y + self.z * other.z)
- end
- function class.cross(self, other)
- assert(type(self) == "table", "Use : for operation")
- assert(type(other) == "table", "Unable to cross "..type(other).." to vector")
- return class.new(self.y * other.z - self.z * other.y,
- self.z * other.x - self.x * other.z,
- self.x * other.y - self.y * other.x)
- end
- function class.round(self)
- assert(type(self) == "table", "Use : for operation")
- self.x = math.floor(self.x + 0.5)
- self.y = math.floor(self.y + 0.5)
- self.z = math.floor(self.z + 0.5)
- return self
- end
- function class.length(self)
- assert(type(self) == "table", "Use : for operation")
- return math.sqrt(self.x * self.x + self.y * self.y + self.z * self.z)
- end
- function class.normalize(self)
- assert(type(self) == "table", "Use : for operation")
- return class.divide(self, class.length(self))
- end
- function class.copy(self)
- assert(type(self) == "table", "Use : for operation")
- return class.new(self.x, self.y, self.z)
- end
- function class.toString(self)
- assert(type(self) == "table", "Use : for operation")
- return string.format("[%.3f, %.3f, %.3f]", self.x, self.y, self.z)
- end
- return class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement