Advertisement
Guest User

bounds2.lua

a guest
Jan 22nd, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.48 KB | None | 0 0
  1. -- TODO: Don't use class system...
  2.  
  3. local TObject = require "class"
  4. local vec2 = require "vector"[2]
  5.  
  6. local bounds2 = TObject:inherit("bounds2")
  7.  
  8. function bounds2:create(a, b)
  9.   local f = self.fields
  10.   f._a = b and a:copy() or vec2(1)
  11.   f._b = b and b:copy() or a and a:copy() or vec2(1)
  12. end
  13.  
  14. bounds2.properties = {
  15.   a = {
  16.     get = function(self)
  17.       return self._a
  18.     end,
  19.     set = function(self, value)
  20.       self._a:assign(value)
  21.     end
  22.   },
  23.   b = {
  24.     get = function(self)
  25.       return self._b
  26.     end,
  27.     set = function(self, value)
  28.       self._b:assign(value)
  29.     end
  30.   },
  31.   pos = {
  32.     get = function(self)
  33.       return self._a
  34.     end,
  35.     set = function(self, value)
  36.       -- braces for speed
  37.       self:assign(self + (value - self._a))
  38.     end
  39.   },
  40.   size = {
  41.     get = function(self)
  42.       return self._b - self._a + 1
  43.     end,
  44.     set = function(self, value)
  45.       self._b = self._a + value - 1
  46.     end
  47.   },
  48.   width = {
  49.     get = function(self)
  50.       return self.size.x
  51.     end,
  52.     set = function(self, value)
  53.       self._b.x = self._a.x + value - 1
  54.     end
  55.   },
  56.   height = {
  57.     get = function(self)
  58.       return self.size.y
  59.     end,
  60.     set = function(self, value)
  61.       self._b.y = self._a.y + value - 1
  62.     end
  63.   },
  64.   left = {
  65.     get = function(self)
  66.       return self.a.x
  67.     end,
  68.     set = function(self, value)
  69.       self._a.x = value
  70.     end
  71.   },
  72.   top = {
  73.     get = function(self)
  74.       return self.a.y
  75.     end,
  76.     set = function(self, value)
  77.       self._a.y = value
  78.     end
  79.   },
  80.   right = {
  81.     get = function(self)
  82.       return self.b.x
  83.     end,
  84.     set = function(self, value)
  85.       self._b.x = value
  86.     end
  87.   },
  88.   bottom = {
  89.     get = function(self)
  90.       return self.b.y
  91.     end,
  92.     set = function(self, value)
  93.       self._b.y = value
  94.     end
  95.   }
  96. }
  97.  
  98. local function clamp(vec, bounds)
  99.   return vec2(
  100.     math.min(math.max(vec.x, bounds.a.x), bounds.b.x),
  101.     math.min(math.max(vec.y, bounds.a.y), bounds.b.y)
  102.   )
  103. end
  104.  
  105. function bounds2:clamp(bounds)
  106.   return bounds2(
  107.     clamp(self.a, bounds),
  108.     clamp(self.b, bounds)
  109.   )  
  110. end
  111.  
  112. function bounds2.mt:add(vec)
  113.   return bounds2(self.a + vec, self.b + vec)
  114. end
  115.  
  116. function bounds2.mt:sub(vec)
  117.   return bounds2(self.a - vec, self.b - vec)
  118. end
  119.  
  120. function bounds2.mt:equal(other)
  121.   return self.a == other.a and self.b == other.b
  122. end
  123.  
  124. function bounds2:assign(other)
  125.   self.a = other.a
  126.   self.b = other.b
  127. end
  128.  
  129. function bounds2:copy()
  130.   return bounds2(self.a, self.b)
  131. end
  132.  
  133. function bounds2:contains(vec)
  134.   return vec >= self.a and vec <= self.b
  135. end
  136.  
  137. return bounds2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement