From d60c9ed0dd40d432021657a263c0d904dc732bbe Mon Sep 17 00:00:00 2001 From: Emmanuel Lepage Vallee Date: Sun, 9 Feb 2020 20:29:41 -0500 Subject: [PATCH] gears.geometry: Add a `rectangle.is_inside` function. --- lib/gears/geometry.lua | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/lib/gears/geometry.lua b/lib/gears/geometry.lua index d4944b66..d9a20303 100644 --- a/lib/gears/geometry.lua +++ b/lib/gears/geometry.lua @@ -108,9 +108,17 @@ end --- Calculate distance between two points. -- i.e: if we want to move to the right, we will take the right border -- of the currently focused screen and the left side of the checked screen. --- @param dir The direction. --- @param _gA The first rectangle. --- @param _gB The second rectangle. +-- +-- The valid `dir` are: +-- +-- * up +-- * down +-- * left +-- * right +-- +-- @tparam string dir The direction. +-- @tparam table _gA The first rectangle. +-- @tparam table _gB The second rectangle. -- @return The distance between the screens. local function calculate_distance(dir, _gA, _gB) local gAx = _gA.x @@ -174,6 +182,25 @@ function gears.geometry.rectangle.are_equal(a, b) return true end +--- Return if rectangle `a` is within rectangle `b`. +-- +-- This includes the edges. 100% of `a` area has to be within `b` for this +-- function to return true. If you wish to know if any part of `a` intersect +-- with `b`, use `gears.geometry.rectangle.get_intersection`. +-- +-- @tparam table a The smaller area. +-- @tparam table b The larger area. +-- @treturn boolean If the areas are identical. +-- @staticfct gears.geometry.rectangle.is_inside +-- @see gears.geometry.rectangle.get_intersection +function gears.geometry.rectangle.is_inside(a, b) + return (a.x >= b.x + and a.y >= b.y + and a.x+a.width <= b.x + b.width + and a.y+a.height <= b.y + b.height + ) +end + --- Check if an area intersect another area. -- @tparam table a The area. -- @tparam table b The other area. @@ -199,6 +226,7 @@ end -- @tparam number b.height The rectangle height -- @treturn table The intersect area. -- @staticfct gears.geometry.rectangle.get_intersection +-- @see gears.geometry.rectangle.is_inside function gears.geometry.rectangle.get_intersection(a, b) local g = {} g.x = math.max(a.x, b.x)