Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- TODO: Don't use class system...
- local TObject = require "class"
- local vec2 = require "vector"[2]
- local bounds2 = TObject:inherit("bounds2")
- function bounds2:create(a, b)
- local f = self.fields
- f._a = b and a:copy() or vec2(1)
- f._b = b and b:copy() or a and a:copy() or vec2(1)
- end
- bounds2.properties = {
- a = {
- get = function(self)
- return self._a
- end,
- set = function(self, value)
- self._a:assign(value)
- end
- },
- b = {
- get = function(self)
- return self._b
- end,
- set = function(self, value)
- self._b:assign(value)
- end
- },
- pos = {
- get = function(self)
- return self._a
- end,
- set = function(self, value)
- -- braces for speed
- self:assign(self + (value - self._a))
- end
- },
- size = {
- get = function(self)
- return self._b - self._a + 1
- end,
- set = function(self, value)
- self._b = self._a + value - 1
- end
- },
- width = {
- get = function(self)
- return self.size.x
- end,
- set = function(self, value)
- self._b.x = self._a.x + value - 1
- end
- },
- height = {
- get = function(self)
- return self.size.y
- end,
- set = function(self, value)
- self._b.y = self._a.y + value - 1
- end
- },
- left = {
- get = function(self)
- return self.a.x
- end,
- set = function(self, value)
- self._a.x = value
- end
- },
- top = {
- get = function(self)
- return self.a.y
- end,
- set = function(self, value)
- self._a.y = value
- end
- },
- right = {
- get = function(self)
- return self.b.x
- end,
- set = function(self, value)
- self._b.x = value
- end
- },
- bottom = {
- get = function(self)
- return self.b.y
- end,
- set = function(self, value)
- self._b.y = value
- end
- }
- }
- local function clamp(vec, bounds)
- return vec2(
- math.min(math.max(vec.x, bounds.a.x), bounds.b.x),
- math.min(math.max(vec.y, bounds.a.y), bounds.b.y)
- )
- end
- function bounds2:clamp(bounds)
- return bounds2(
- clamp(self.a, bounds),
- clamp(self.b, bounds)
- )
- end
- function bounds2.mt:add(vec)
- return bounds2(self.a + vec, self.b + vec)
- end
- function bounds2.mt:sub(vec)
- return bounds2(self.a - vec, self.b - vec)
- end
- function bounds2.mt:equal(other)
- return self.a == other.a and self.b == other.b
- end
- function bounds2:assign(other)
- self.a = other.a
- self.b = other.b
- end
- function bounds2:copy()
- return bounds2(self.a, self.b)
- end
- function bounds2:contains(vec)
- return vec >= self.a and vec <= self.b
- end
- return bounds2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement