Advertisement
Snusmumriken

Pure lua vec

Oct 6th, 2017
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 21.02 KB | None | 0 0
  1.  
  2.  
  3.  
  4. --[[
  5. --Reference:
  6.  
  7.     vec = require'vec'
  8.  
  9.     a = vec(3, 4)
  10.     b = vec(-30, -40)
  11.     c = vec(3, -2)
  12.  
  13. -- Call. It faster then defineing new vectors
  14.  
  15.     a(b)          --> a = {-30, -40}, a get new value, is not link to b
  16.     a(3, 4)       --> a = {3, 4}, set it back
  17.  
  18. -- Arithmetic:
  19.     d = a + b     --> d = {-27, -36}
  20.     d = b - a     --> d = {-33, -44}
  21.  
  22.     d = a * b     --> d = {-90, -160}
  23.     d = -b * 0.2  --> d = {6, 8}
  24.  
  25.     d = a / b     --> d = {0.1, 0.1}
  26.     d = a / 2     --> d = {1.5, 2}
  27.  
  28.     d = a ^ c     --> d = {27, 0.0625}
  29.     d = a ^ 3     --> d = {27, 64}
  30.  
  31.     Also here may be strong optimisation:
  32.         d:add(vec)                    average addition
  33.         d = a:nadd( (num) x, (num) y) adding vector from two numbers to d.x and d.y
  34.         d = d:vadd(v)                 faster addition of two vectors
  35.        
  36.         It works for every method with varying of args:
  37.         add, sub, mul, div, pow, eq, lt, le, dot, det, cross, angle, dist, dist2
  38.        
  39.         Every arithmetic/equality overloadings has method veriation
  40.        
  41.     If you don't need for new vectors:
  42.         d:sadd(vec or [num, num])     does not create new vector, only modify vector d
  43.        
  44.         It works for every method, returns new vector:
  45.         add, sub, mul, div, pow, unm, permul, trim, norm, rotate, magn (abs),
  46.         perp, project, mirror, clamp, lerp, alerp, floor, ceil, round
  47.        
  48.     Also it can be combined to fastest variation:
  49.         d:snadd(num, num)            add to d vector as two numbers, fastest variation
  50.         d:svadd(vec)
  51.    
  52.    
  53. -- Equality
  54.     a == 5        --> true, because a:len() = 5
  55.     a == d        --> false
  56.     b >  a        --> true
  57.     b <= 50       --> true, because #b = 50
  58.    
  59. -- Len
  60.     e = #a        --> e = 5
  61.     e = a:len()   --> e = 5
  62.     e = a:len2()  --> e = 25
  63.    
  64. -- Utility methods:
  65.     print(a:unpack()) --> 3     4
  66.  
  67.     str = tostring(a) or a:tostring()  --> str = '[3, 4]'
  68.     print('sum of vectors: '..(a+b))   --> sum of vectors: [33, 44]
  69.    
  70.     local n = vec:fromstring(str)      --> creates vector {33, 44} from string
  71.     len = n:tonumber()                 --> equal 'len = #n'
  72.    
  73.     t = {a:join(b, 10, 20, c, 30, 40)}
  74.     --> join return touple of all arguments, now t is {a.x, a.y, b.x, b.y, 10, 20, c.x, c.y, 30, 40}
  75.    
  76.     vec.isVec(b)    --> return true
  77.     d = a:clone()--> d = {3, 4}, work like call
  78.    
  79.     --Methods:
  80.         --a:area()                    - S of a-rectangle (a.x * a.y)
  81.         --a:permul(b)                 - like a*b, but only for vectors, little bit faster
  82.         --a:trim(len)                 - vector trimming
  83.         --a:norm()                    - normalize vector
  84.         --a:floor([n])                - flooring, n - digits after the decimal point, 0 as default
  85.         --a:ceil([n])                 - ceiling, n - digits after the decimal point, 0 as default
  86.         --a:round([n])                - ceiling, n - digits after the decimal point, 0 as default
  87.         --a:dist(b)                   - return distance between a and b
  88.         --a:dist2(b)                  - return distance^2 between a and b
  89.         --a:dot(b)                    - dot product (a.x * b.x + a.y * b.y)
  90.         --a:det(b)                    - det product (a.x * b.x - a.y * b.y)
  91.         --a:cross(b)                  - cross product   (a.x * b.y - a.y * b.x)
  92.         --a:angle([b|n,m])            - return vector angle (no args) or angle between two vectors (arg can be vec or two num)
  93.         --a:setAngle(n)               - set vector by angle n
  94.         --a:rotate(n)                 - return rotated vector
  95.         --a:magn()                    - vector magnitude (abs(a.x), abs(a.y)), also named 'abs'
  96.         --a:abs()                     - same as magn
  97.         --a:perp([b|n,m])             - set a perpendicular to b
  98.         --a:clamp(b, c)               - clamp vector between two other vectors
  99.         --a:project([b|n,m])          - set a to projection a to b
  100.         --a:mirror([b|n,m])           - mirror a relatively b
  101.         --a:lerp(b, n, dt)            - interpolate vector a to vector b, with n coef and dt as delta
  102.         --a:alerp(b, n, dt)           - interpolate angle of vector a to angle of vector b, with n coef and dt as delta
  103. ]]
  104.  
  105. local function isVec(v) return type(v) == 'table' and v.x and v.y end
  106. local function isNum(v) return type(v) == 'number' end
  107.  
  108. local oldtype = type
  109.  
  110. local function switch(a, b)
  111.     if isVec(a) then
  112.         return a, b
  113.     end
  114.     return b, a
  115. end
  116.  
  117. local lvl = 2
  118.  
  119. local vector, mt = {}, {}
  120. vector.mt = mt
  121.  
  122. local error = error
  123. local pi, tau, sqrt, cos, sin, acos, atan2 = math.pi, math.pi*2, math.sqrt, math.cos, math.sin, math.acos, math.atan2
  124.  
  125. function vector:new(x, y)
  126.     return setmetatable({x = x, y = y}, self.mt)
  127. end
  128.  
  129. setmetatable(vector, {__call = vector.new})
  130.  
  131. mt.__index = vector
  132. function mt.__call(self, x, y)
  133.     if x and y then
  134.         self.x = x
  135.         self.y = y
  136.         return self
  137.     end
  138.     return vector(self.x, self.y)
  139. end
  140.  
  141. -- OVERLOADING
  142. do
  143.     -- ADD
  144.     do
  145.         function vector:add(v)
  146.             return vector(self.x + v.x, self.y + v.y)
  147.         end
  148.         mt.__add = vector.add
  149.  
  150.         function vector:nadd(a, b)
  151.             return vector(self.x + a, self.y + (b or a))
  152.         end
  153.  
  154.         function vector:vadd(v)
  155.             return vector(self.x + v.x, self.y + v.y)
  156.         end
  157.  
  158.         function vector:sadd(v)
  159.             self.x, self.y = self.x + v.x, self.y + v.y
  160.             return self
  161.         end
  162.        
  163.         function vector:snadd(a, b)
  164.             self.x = self.x + a
  165.             self.y = self.y + (b or a)
  166.             return self
  167.         end
  168.  
  169.         function vector:svadd(v)
  170.             self.x = self.x + v.x
  171.             self.y = self.y + v.y
  172.             return self
  173.         end
  174.  
  175.     end
  176.  
  177.     -- SUB
  178.     do
  179.         function vector:sub(v)
  180.             return vector(self.x - v.x, self.y - v.y)
  181.         end
  182.         mt.__sub = vector.sub
  183.        
  184.         function vector:nsub(a, b)
  185.             return vector(self.x - a, self.y - (b or a))
  186.         end
  187.  
  188.         function vector:vsub(v)
  189.             return vector(self.x - v.x, self.y - v.y)
  190.         end
  191.  
  192.         function vector:ssub(v)
  193.             self.x, self.y = self.x - v.x, self.y - v.y
  194.             return self
  195.         end
  196.        
  197.         function vector:snsub(a, b)
  198.             self.x = self.x - a
  199.             self.y = self.y - (b or a)
  200.             return self
  201.         end
  202.  
  203.         function vector:svsub(v)
  204.             self.x = self.x - v.x
  205.             self.y = self.y - v.y
  206.             return self
  207.         end
  208.        
  209.     end
  210.  
  211.     -- MUL
  212.     do
  213.         function vector:mul(v)
  214.             self, v = switch(self, v)
  215.             return isVec(v) and vector(self.x * v.x, self.y * v.y)
  216.                     or isNum(v)    and vector(self.x * v,   self.y * v)
  217.                     or error('Vector mul: two vectors or vector and scalar expected, got vector and '..type(v), lvl)
  218.         end
  219.        
  220.         mt.__mul = vector.mul
  221.        
  222.         function vector:nmul(a, b)
  223.             return vector(self.x * a, self.y * (b or a))
  224.         end
  225.  
  226.         function vector:vmul(v)
  227.             return vector(self.x * v.x, self.y * v.y)
  228.         end
  229.        
  230.         function vector:smul(v)
  231.             self, v = switch(self, v)
  232.             if isNum(v) then
  233.                 self.x = self.x * v
  234.                 self.y = self.y * v
  235.             elseif isVec(self) then
  236.                 self.x = self.x * v.x
  237.                 self.y = self.y * v.y
  238.             else
  239.                 error('Vector mul: two vectors or vector and scalar expected, got vector and '..type(v), lvl)
  240.             end
  241.             return self
  242.         end
  243.        
  244.         function vector:snmul(a, b)
  245.             self.x = self.x * a
  246.             self.y = self.y * (b or a)
  247.             return self
  248.         end
  249.  
  250.         function vector:svmul(v)
  251.             self.x = self.x * v.x
  252.             self.y = self.y * v.y
  253.             return self
  254.         end
  255.        
  256.     end
  257.  
  258.     -- DIV
  259.     do
  260.         function vector:div(v)
  261.             self, v = switch(self, v)
  262.             return isVec(v) and vector(self.x / v.x, self.y / v.y)
  263.                     or isNum(v) and vector(self.x / v,   self.y / v)
  264.                     or error('Vector div: two vectors or vector and scalar expected, got vector and '..type(v), lvl)
  265.         end
  266.        
  267.         mt.__div = vector.div
  268.        
  269.         function vector:ndiv(a, b)
  270.             return vector(self.x / a, self.y / (b or a))
  271.         end
  272.  
  273.         function vector:vdiv(v)
  274.             return vector(self.x / v.x, self.y / v.y)
  275.         end
  276.  
  277.         function vector:sdiv(v)
  278.             self, v = switch(self, v)
  279.             if isNum(v) then
  280.                 self.x = self.x / v
  281.                 self.y = self.y / v
  282.             elseif isVec(self) then
  283.                 self.x = self.x / v.x
  284.                 self.y = self.y / v.y
  285.             else
  286.                 error('Vector sdiv: two vectors or vector and scalar expected, got vector and '..type(v), lvl)
  287.             end
  288.             return self
  289.         end
  290.        
  291.         function vector:sndiv(a, b)
  292.             self.x = self.x / a
  293.             self.y = self.y / (b or a)
  294.             return self
  295.         end
  296.  
  297.         function vector:svdiv(v)
  298.             self.x = self.x / v.x
  299.             self.y = self.y / v.y
  300.             return self
  301.         end
  302.        
  303.     end
  304.  
  305.     -- POW
  306.     do
  307.         function vector:pow(v)
  308.             self, v = switch(self, v)
  309.             return isVec(self) and vector(self.x ^ v.x, self.y ^ v.y)
  310.                     or isNum(v)    and vector(self.x ^ b,   self.y ^ b)
  311.                     or error('Vector pow: two vectors or vector and scalar expected, got vector and '..type(v), lvl)
  312.         end
  313.        
  314.         mt.__pow = vector.pow
  315.        
  316.         function vector:npow(a, b)
  317.             return vector(self.x ^ a, self.y ^ (b or a))
  318.         end
  319.  
  320.         function vector:vpow(v)
  321.             return vector(self.x ^ v.x, self.y ^ v.y)
  322.         end
  323.  
  324.         function vector:spow(v)
  325.             self, v = switch(self, v)
  326.             if isNum(v) then
  327.                 self.x = self.x ^ v
  328.                 self.y = self.y ^ v
  329.             elseif isVec(self) then
  330.                 self.x = self.x ^ v.x
  331.                 self.y = self.y ^ v.y
  332.             else
  333.                 error('Vector spow: two vectors or vector and scalar expected, got vector and '..type(v), lvl)
  334.             end
  335.             return self
  336.         end
  337.        
  338.         function vector:snpow(a, b)
  339.             self.x = self.x ^ a
  340.             self.y = self.y ^ (b or a)
  341.             return self
  342.         end
  343.  
  344.         function vector:svpow(v)
  345.             self.x = self.x ^ v.x
  346.             self.y = self.y ^ v.y
  347.             return self
  348.         end
  349.        
  350.     end
  351.  
  352.     -- EQ
  353.     do
  354.         function vector:eq(v)
  355.             self, v = switch(self, v)
  356.             return isVec(self) and self.x == v.x and self.y == v.y
  357.                     or isNum(v)    and #self == v
  358.                     or error('Vector eq: two vectors or vector and scalar expected, got vector and '..type(v), lvl)
  359.         end
  360.        
  361.         mt.__eq = vector.eq
  362.        
  363.         function vector:neq(a, b)
  364.             return b and (self.x == a and self.y == b) or #self == a
  365.         end
  366.  
  367.         function vector:veq(v)
  368.             return self.x == v.x and self.y == v.y
  369.         end
  370.     end
  371.  
  372.     -- LT
  373.     do
  374.         function vector:lt(v)
  375.             self, v = switch(self, v)
  376.             return isVec(self) and self.x < v.x and self.y < v.y
  377.                     or isNum(v)    and #self < v
  378.                     or error('Vector lt: two vectors or vector and scalar expected, got vector and '..type(v), lvl)
  379.         end
  380.        
  381.         mt.__lt = vector.lt
  382.        
  383.         function vector:nlt(a, b)
  384.             return b and (self.x < a and self.y < b) or #self < a
  385.         end
  386.  
  387.         function vector:vlt(v)
  388.             return self.x < v.x and self.y < v.y
  389.         end
  390.     end
  391.  
  392.     -- LE
  393.     do
  394.         function vector:le(v)
  395.             self, v = switch(self, v)
  396.             return isVec(self) and self.x <= v.x and self.y <= v.y
  397.                     or isNum(v)    and #self <= v
  398.                     or error('Vector le: two vectors or vector and scalar expected, got vector and '..type(b), lvl)
  399.         end
  400.        
  401.         mt.__le = vector.le
  402.        
  403.         function vector:nle(a, b)
  404.             return b and (self.x <= a and self.y <= b) or #self <= a
  405.         end
  406.  
  407.         function vector:vle(v)
  408.             return self.x <= v.x and self.y <= v.y
  409.         end
  410.     end
  411.  
  412.     -- UNM
  413.     do
  414.         function vector:unm()
  415.             return vector(-self.x, -self.y)
  416.         end
  417.        
  418.         mt.__unm = vector.unm
  419.        
  420.         function vector:sunm()
  421.             self.x, self.y = -self.x, -self.y
  422.             return self
  423.         end
  424.     end
  425.  
  426.     -- LEN
  427.     do
  428.         function vector:len()
  429.             return (self.x * self.x + self.y * self.y)^.5
  430.         end
  431.        
  432.         mt.__len = vector.len
  433.        
  434.         function vector:len2()
  435.             return (self.x * self.x + self.y * self.y)
  436.         end
  437.     end
  438.  
  439. end
  440. -- CONVERSIONS
  441. do
  442.     function vector:tostring()
  443.         return '['..self.x..', '..self.y..']'
  444.     end
  445.    
  446.     mt.__tostring = vector.tostring
  447.    
  448.     function vector:fromstring(s)
  449.         local a, b = s:match('(%-?%d%.?%d?).-(%-?%d%.?%d?)')
  450.         if not a or not b then
  451.             return nil, 'parse error, format \'[n1,n2]\''
  452.         end
  453.         return vector(tonumber(a), tonumber(b))
  454.     end
  455.    
  456.     function vector:tonumber()
  457.         return #self
  458.     end
  459.    
  460.     mt.__tonumber = vector.tonumber
  461.    
  462.     function vector:concat(v)
  463.         return tostring(self)..tostring(v)
  464.     end
  465.    
  466.     mt.__concat = vector.concat
  467.    
  468.     function vector:unpack(v)
  469.         if v then
  470.             return self.x*v, self.y*v
  471.         else
  472.             return self.x, self.y
  473.         end
  474.     end
  475. end
  476.  
  477. -- TOOLS
  478. do
  479.     function vector:isVec(v)
  480.         return v and isVec(v) or isVec(self)
  481.     end
  482.  
  483.     function vector:clone()
  484.         return vector(self.x, self.y)
  485.     end
  486.  
  487.     function vector:set(a, b)
  488.         if isNum(a) then
  489.             self.x, self.y = a, (b or a)
  490.         elseif isVec(a) then
  491.             self.x = a.x
  492.             self.y = a.y
  493.         end
  494.         return self
  495.     end
  496.    
  497.     local insert = table.insert
  498.     function vector:join(...)
  499.         local res = {}
  500.         for i, v in ipairs{self, ...} do
  501.             if isVec(v) then
  502.                 insert(res, v.x)
  503.                 insert(res, v.y)
  504.             else
  505.                 insert(res, v)
  506.             end
  507.         end
  508.         return unpack(res)
  509.     end
  510.    
  511. end
  512.  
  513. function vector:area()
  514.     return self.x * self.y
  515. end
  516.  
  517.  
  518. -- VEC TRANSFORM
  519. do
  520.     function vector:spermul(a, b)
  521.         if isVec(a) then
  522.             self.x, self.y = self.x * a.x, self.y * a.y
  523.             return self
  524.         elseif isNum(a) and isNum(b) then
  525.             self.x, self.y = self.x * a, self.y * b
  526.             return self
  527.         else
  528.             local str = type(a)
  529.             if b then str = str..', '..type(b) end
  530.             error('Vector:permul: vector or two numbers expected, got: '..str, lvl)
  531.         end
  532.     end
  533.  
  534.     function vector:permul(a, b)
  535.         return self:clone():spermul(a, b)
  536.     end
  537.  
  538.     local sqrt = math.sqrt
  539.     function vector:strim(l)
  540.         if not isNum(l) then error('Vector:trim: number expected, got: '..type(l), lvl) end
  541.         local s = l * l / self:len2()
  542.         s = (s > 1 and 1) or sqrt(s)
  543.         self.x, self.y = self.x * s, self.y * s
  544.         return self
  545.     end
  546.  
  547.     function vector:trim(l)
  548.         return self:clone():strim(l)
  549.     end
  550.    
  551.     function vector:snorm(v)
  552.         local d = isVec(v) and #v or isNum(v) and v or 1
  553.         local l = self:len()
  554.         if l > 0 then
  555.             self.x = self.x / l * d
  556.             self.y = self.y / l * d
  557.             return self
  558.         end
  559.         self.x = 0
  560.         self.y = 0
  561.         return self
  562.     end
  563.  
  564.     function vector:norm(v)
  565.         return self:clone():snorm(v)
  566.     end
  567.  
  568.     function vector:setAngle(phi)
  569.     --  phi = (phi % 2*pi)-pi
  570.         local a = #self
  571.         self.x, self.y = cos(phi) * a, sin(phi) * a
  572.         return self
  573.     end
  574.  
  575.     function vector:srotate(phi)
  576.         local c, s = cos(phi), sin(phi)
  577.         self.x, self.y = c * self.x - s * self.y, s * self.x + c * self.y
  578.         return self
  579.     end
  580.  
  581.     function vector:rotate(phi)
  582.         return self:clone():srotate(phi)
  583.     end
  584.  
  585.     local abs = math.abs
  586.     function vector:smagn()
  587.         self.x = abs(self.x)
  588.         self.y = abs(self.y)
  589.         return self
  590.     end
  591.  
  592.     function vector:magn()
  593.         return self:clone():smagn()
  594.     end
  595.     vector.abs  = vector.magn
  596.     vector.sabs = vector.smagn
  597.  
  598.     function vector:sperp(a, b)
  599.         if isVec(a) then
  600.             local d = #self/#a
  601.             self.x = a.y * d
  602.             self.y = -a.x * d
  603.             return self
  604.         elseif isNum(a) and isNum(b) then
  605.             local d = #self/((a ^ 2 + b ^ 2)^.5)
  606.             self.x, self.y = b*d, -a*d
  607.             return self
  608.         elseif not a and not b then
  609.             self.x, self.y = self.y, -self.x
  610.             return self
  611.         elseif type(a) == 'boolean' then
  612.             self.x, self.y = -self.y, self.x
  613.             return self
  614.         else
  615.             local str = type(a)
  616.             if b then str = str..', '..type(b) end
  617.             error('Vector:perpendiculared: vector or two numbers or boolean expected, got: '..str, lvl)
  618.         end
  619.     end
  620.  
  621.     function vector:perp(a, b)
  622.         return self:clone():sperp(a, b)
  623.     end
  624.  
  625.     function vector:sproject(a, b)
  626.         if isVec(a) then
  627.             local s = (self.x * a.x + self.y * a.y) / (a.x * a.x + a.y * a.y)
  628.             self.x, self.y = s * a.x, s * a.y
  629.             return self
  630.         elseif isNum(a) and isNum(b) then
  631.             local s = (self.x * a + self.y * b) / (a * a + b * b)
  632.             self.x, self.y = s * a, s * b
  633.             return self
  634.         else
  635.             local str = type(a)
  636.             str = b and str..', '..type(b) or str
  637.             error('Vector:project: vector or two numbers expected, got: '..str, lvl)
  638.         end
  639.     end
  640.  
  641.     function vector:project(a, b)
  642.         return self:clone():sproject(a, b)
  643.     end
  644.  
  645.     function vector:smirror(a, b)
  646.         if isVec(a) then
  647.             local s = 2 * (self.x * a.x + self.y * a.y) / (a.x * a.x + a.y * a.y)
  648.             -- s can be nan if #vectors is 0, 0
  649.             if s == s then
  650.                 self.x, self.y = s * a.x - self.x, s * a.y - self.y
  651.                 return self
  652.             end
  653.             return a:clone()
  654.         elseif isNum(a) and isNum(b) then
  655.             local s = 2 * (self.x * a + self.y * b) / (a * a + b * b)
  656.             if s == s then
  657.                 self.x, self.y = s * a - self.x, s * b - self.y
  658.                 return self
  659.             end
  660.             return vector(a, b)
  661.         else
  662.             local str = type(a)
  663.             if b then str = str..', '..type(b) end
  664.             error('Vector:mirror: vector or two numbers expected, got: '..str, lvl)
  665.         end
  666.     end
  667.  
  668.     function vector:mirror(a, b)
  669.         return self:clone():smirror()
  670.     end
  671.  
  672.     local function vmin(a, b) return a < b and b or a end
  673.     local function vmax(a, b) return a > b and b or a end
  674.    
  675.     function vector:sclamp(a, b)
  676.         self.x = vmin(vmax(self.x, a.x), b.x)
  677.         self.y = vmin(vmax(self.y, a.y), b.y)
  678.         return self
  679.     end
  680.  
  681.     function vector:clamp(a, b)
  682.         return self:clone():sclamp()
  683.     end
  684.    
  685.    
  686.     -- LERPING
  687.     do
  688.    
  689.         local pi = math.pi
  690.         function vector:salerp(a, b, dt)
  691.             local ca, ta = self:angle(), a:angle()
  692.             local theta = ta - ca
  693.             local ca = theta > pi and ca + pi*2 or theta < -pi and ca - pi*2 or ca
  694.             self:setAngle(ca + (ta - ca) * b)
  695.             return self
  696.         end
  697.        
  698.         function vector:alerp(a, b, dt)
  699.             return self:clone():salerp(a, b, dt)
  700.         end
  701.  
  702.         local function lerp(a, b, t) return a + (b - a) * t end
  703.  
  704.         function vector:slerp(a, b, c)
  705.             if isNum(a) and isNum(b) then
  706.                 a = vector(a, b)
  707.                 b = c
  708.             end
  709.             self.x = self.x + (a.x - self.x) * b
  710.             self.y = self.y + (a.y - self.y) * b
  711.             return self
  712.         end
  713.  
  714.         function vector:lerp(a, b, c)
  715.             return self:clone():slerp(a, b, c)
  716.         end
  717.     end
  718.    
  719.     -- ROUNDING
  720.     do
  721.         local floor = math.floor
  722.         function vector:sfloor(n)
  723.             n = n and n * 10
  724.             if n then
  725.                 self.x = floor(self.x * n) / n
  726.                 self.y = floor(self.y * n) / n
  727.             else
  728.                 self.x = floor(self.x)
  729.                 self.y = floor(self.y)
  730.             end
  731.             return self
  732.         end
  733.        
  734.         function vector:floor(n)
  735.             return self:clone():sfloor()
  736.         end
  737.  
  738.         local ceil = math.ceil
  739.         function vector:sceil(n)
  740.             n = n and n * 10
  741.             if n then
  742.                 self.x = ceil(self.x * n) / n
  743.                 self.y = ceil(self.y * n) / n
  744.             else
  745.                 self.x = ceil(self.x)
  746.                 self.y = ceil(self.y)
  747.             end
  748.             return self
  749.         end
  750.  
  751.         function vector:ceil(v)
  752.             return self:clone():sceil()
  753.         end
  754.        
  755.         local floor = math.floor
  756.         function vector:sround(n)
  757.             n = n and n * 10
  758.             if n then
  759.                 local fx, fy = floor(self.x * n)
  760.                 self.x = (floor(self.x * n + 0.5) - 1) / n
  761.                 self.x = (floor(self.y * n + 0.5) - 1) / n
  762.             else
  763.                 self.x = floor(self.x + 0.5) - 1
  764.                 self.y = floor(self.y + 0.5) - 1
  765.             end
  766.             return self
  767.         end
  768.        
  769.         function vector:round(v)
  770.             return self:clone():sround(v)
  771.         end
  772.     end
  773.    
  774. end
  775.  
  776.  
  777. -- VECTOR OPERATIONS
  778. do
  779.     function vector:dot(a, b)
  780.         if isVec(a) then
  781.             return self.x * a.x + self.y * a.y
  782.         elseif isNum(a) and isNum(b) then
  783.             return self.x * a + self.y * b
  784.         else
  785.             local str = type(a)
  786.             if b then str = str..', '..type(b) end
  787.             error('Vector:dot: vector or two numbers expected, got: '..str, lvl)
  788.         end
  789.     end
  790.  
  791.     function vector:vdot(v)
  792.         return self.x * v.x + self.y * v.y
  793.     end
  794.  
  795.     function vector:ndot(x, y)
  796.         return self.x * x + self.y * y
  797.     end
  798.  
  799.     function vector:det(a, b)
  800.         if isVec(a) then
  801.             return self.x * a.x - self.y * a.y
  802.         elseif isNum(a) and isNum(b) then
  803.             return self.x * a - self.y * b
  804.         else
  805.             local str = type(a)
  806.             if b then str = str..', '..type(b) end
  807.             error('Vector:det: vector or two numbers expected, got: '..str, lvl)
  808.         end
  809.     end
  810.  
  811.     function vector:vdet(v)
  812.         return self.x * v.x - self.y * v.y
  813.     end
  814.    
  815.     function vector:ndet(x, y)
  816.         return self.x * x - self.y * y
  817.     end
  818.    
  819.     function vector:cross(a, b)
  820.         if isVec(a) then
  821.             return self.x * a.y - self.y * a.x
  822.         elseif isNum(a) and isNum(b) then
  823.             return self.x * b - self.y * a
  824.         else
  825.             local str = type(a)
  826.             if b then str = str..', '..type(b) end
  827.             error('Vector:cross: vector or two numbers expected, got: '..str, lvl)
  828.         end
  829.     end
  830.    
  831.     function vector:vcross(v)
  832.         return self.x * v.y - self.y * v.x
  833.     end
  834.    
  835.     local atan2 = math.atan2
  836.     function vector:angle(a, b)
  837.         if isVec(a) then
  838.             return atan2(self.y, self.x) - atan2(a.y, a.x)
  839.         elseif isNum(a) and isNum(b) then
  840.             return atan2(self.y, self.x) - atan2(b, a)
  841.         elseif isNum(a) then
  842.             return atan2(self.y, self.x) - a
  843.         end
  844.         return atan2(self.y, self.x)
  845.     end
  846.    
  847.     function vector:vangle(v)
  848.         return atan2(self.y, self.x) - atan2(v.y, v.x)
  849.     end
  850.    
  851.     function vector:nangle()
  852.         return atan2(self.y, self.x)
  853.     end
  854.    
  855.     function vector:len2()
  856.         return self.x * self.x + self.y * self.y
  857.     end
  858.  
  859.     function vector:len()
  860.         return self:len2()^.5
  861.     end
  862.  
  863.     function vector:dist(a, b)
  864.         if isVec(a) then
  865.             local dx = self.x - a.x
  866.             local dy = self.y - a.y
  867.             return sqrt(dx * dx + dy * dy)
  868.         elseif isNum(a) and isNum(b) then
  869.             local dx = self.x - a
  870.             local dy = self.y - b
  871.             return sqrt(dx * dx + dy * dy)
  872.         else
  873.             local str = type(a)
  874.             str = b and str..', '..type(b) or str
  875.             error('Vector:dist: vector or two numbers expected, got: '..str, lvl)
  876.         end
  877.     end
  878.    
  879.     function vector:vdist(v)
  880.         local dx = self.x - v.x
  881.         local dy = self.y - v.y
  882.         return sqrt(dx * dx + dy * dy)
  883.     end
  884.  
  885.     function vector:dist2(a, b)
  886.         if isVec(a) then
  887.             local dx = self.x - a.x
  888.             local dy = self.y - a.y
  889.             return dx * dx + dy * dy
  890.         elseif isNum(a) and isNum(b) then
  891.             local dx = self.x - a
  892.             local dy = self.y - b
  893.             return dx * dx + dy * dy
  894.         else
  895.             local str = type(a)
  896.             str = b and str..', '..type(b) or str
  897.             error('Vector:dist: vector or two numbers expected, got: '..str, lvl)
  898.         end
  899.     end
  900.  
  901.     function vector:vdist2(v)
  902.         local dx = self.x - v.x
  903.         local dy = self.y - v.y
  904.         return dx * dx + dy * dy
  905.     end
  906.    
  907.     function vector:dir(other)
  908.         return self:dot(other)/(#self * #other)
  909.     end
  910.    
  911. end
  912.  
  913.  
  914. return vector
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement