Lua 数学,检测矩形是否在另一个矩形内部

Lua math, detect if rectangle is inside the other rectangle

我正在尝试制作一个类似于 GMOD 的 vgui 库 (https://wiki.facepunch.com/gmod/vgui) 的 UI 系统。在这个库中,您可以创建一个元素,然后是该元素的子元素。父元素的子元素将被父元素的大小裁剪。因此,如果您有一个宽度和高度为 100 的红框,则红框的子元素不能绘制在红框之外,依此类推。

local parent_x = Parent.INTERNAL_VARS.draw_start_x
local parent_y = Parent.INTERNAL_VARS.draw_start_y
local parent_width  = Parent._width
local parent_height = Parent._height

v.INTERNAL_VARS.draw_start_x = parent_x + v.x
v.INTERNAL_VARS.draw_start_y = parent_y + v.y

local child_x = v.INTERNAL_VARS.draw_start_x
local child_y = v.INTERNAL_VARS.draw_start_y
local child_width  = v._width
local child_height = v._height

local x = math.max(child_x, parent_x)
local y = math.max(child_y, parent_y)

draw.DrawOutlinedRect(
   x,
   y,
   math.min(child_x + child_width,  parent_x + parent_width ) - x,
   math.min(child_y + child_height, parent_y + parent_height) - y
)