SHARE
TWEET

Vector4f

incinirate Jun 2nd, 2016 (edited) 33 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --Vector4f = {}
  2. do
  3.   local Vector4f = {} --_G.Vector4f
  4.  
  5.   function Vector4f.__init__(_,x,y,z,w)
  6.     local self = {x=x,y=y,z=z,w=w}
  7.     setmetatable(self, {__index=Vector4f})
  8.     return self
  9.   end
  10.  
  11.   setmetatable(Vector4f, {__call=Vector4f.__init__})
  12.  
  13.   local Math = math
  14.  
  15.   function Vector4f:Length()
  16.     local x = self.x
  17.     local y = self.y
  18.     local z = self.z
  19.     local w = self.w
  20.     return Math.sqrt( x * x + y * y + z * z + w * w )
  21.   end
  22.  
  23.   function Vector4f:Max()
  24.     local x = self.x
  25.     local y = self.y
  26.     local z = self.z
  27.     local w = self.w
  28.     return Math.max(Math.max(x, y), Math.max(z, w))
  29.   end
  30.  
  31.   function Vector4f:Dot(r)
  32.     local x = self.x
  33.     local y = self.y
  34.     local z = self.z
  35.     local w = self.w
  36.     return x * r:GetX() + y * r:GetY() + z * r:GetZ() + w * r:GetW();
  37.   end
  38.  
  39.   function Vector4f:Cross(r)
  40.     local x = self.x
  41.     local y = self.y
  42.     local z = self.z
  43.     local w = self.w
  44.    
  45.     x_ = y * r:GetZ() - z * r:GetY();
  46.         y_ = z * r:GetX() - x * r:GetZ();
  47.         z_ = x * r:GetY() - y * r:GetX();
  48.  
  49.     return Vector4f(x_,y_,z_,0)
  50.   end
  51.  
  52.   function Vector4f:Normalized()
  53.     local x = self.x
  54.     local y = self.y
  55.     local z = self.z
  56.     local w = self.w
  57.    
  58.     local length = self:Length()
  59.    
  60.     return Vector4f(x / length, y / length, z / length, w / length)
  61.   end
  62.  
  63.   function Vector4f:Rotate(axis, angle)
  64.     local sinAngle = Math.sin(-angle)
  65.     local cosAngle = Math.cos(-angle)
  66.    
  67.     return self:Cross(axis:Mul(sinAngle)):Add(
  68.         (self:Mul(cosAngle)):Add(
  69.           axis:Mul(self:Dot(axis:Mul(1 - cosAngle)))))
  70.   end
  71.  
  72.   function Vector4f:Lerp(dest, lerpFactor)
  73.     return dest:Sub(self):Mul(lerpFactor):Add(self)
  74.   end
  75.  
  76.   function Vector4f:Add(r)
  77.     local x = self.x
  78.     local y = self.y
  79.     local z = self.z
  80.     local w = self.w
  81.    
  82.     if type(r)=="number" then
  83.       return Vector4f(x + r, y + r, z + r, w + r)
  84.     else
  85.       return Vector4f(x + r:GetX(), y + r:GetY(), z + r:GetZ(), w + r:GetW())
  86.     end
  87.   end
  88.  
  89.   function Vector4f:Sub(r)
  90.     local x = self.x
  91.     local y = self.y
  92.     local z = self.z
  93.     local w = self.w
  94.    
  95.     if type(r)=="number" then
  96.       return Vector4f(x - r, y - r, z - r, w - r)
  97.     else
  98.       return Vector4f(x - r:GetX(), y - r:GetY(), z - r:GetZ(), w - r:GetW())
  99.     end
  100.   end
  101.  
  102.   function Vector4f:Mul(r)
  103.     local x = self.x
  104.     local y = self.y
  105.     local z = self.z
  106.     local w = self.w
  107.    
  108.     if type(r)=="number" then
  109.       return Vector4f(x * r, y * r, z * r, w * r)
  110.     else
  111.       return Vector4f(x * r:GetX(), y * r:GetY(), z * r:GetZ(), w * r:GetW())
  112.     end
  113.   end
  114.  
  115.   function Vector4f:Div(r)
  116.     local x = self.x
  117.     local y = self.y
  118.     local z = self.z
  119.     local w = self.w
  120.    
  121.     if type(r)=="number" then
  122.       return Vector4f(x / r, y / r, z / r, w / r)
  123.     else
  124.       return Vector4f(x / r:GetX(), y / r:GetY(), z / r:GetZ(), w / r:GetW())
  125.     end
  126.   end
  127.  
  128.   function Vector4f:Abs()
  129.     local x = self.x
  130.     local y = self.y
  131.     local z = self.z
  132.     local w = self.w
  133.    
  134.     return Vector4f(Math.abs(x), Math.abs(y), Math.abs(z), Math.abs(w))
  135.   end
  136.  
  137.   function Vector4f:toString()
  138.     local x = self.x
  139.     local y = self.y
  140.     local z = self.z
  141.     local w = self.w
  142.  
  143.     local ct = {"(" , x , ", " , y , ", " , z , ", " , w , ")"}
  144.     return table.concat(ct)
  145.   end
  146.  
  147.   function Vector4f:GetX()
  148.     return self.x
  149.   end
  150.  
  151.   function Vector4f:GetY()
  152.     return self.y
  153.   end
  154.  
  155.   function Vector4f:GetZ()
  156.     return self.z
  157.   end
  158.  
  159.   function Vector4f:GetW()
  160.     return self.w
  161.   end
  162.  
  163.   function Vector4f:equals(r)
  164.     return (self.x == r:GetX() and self.y == r:GetY() and self.z == r:GetZ() and self.w == r:GetW())
  165.   end
  166.  
  167.   return Vector4f
  168. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top