Skip to content

Rect

Rect describes a rectangle using position (x, y) and size (width, height). It is commonly used to place ImGui elements and other two-dimensional regions.

Rect only stores rectangular geometry. It does not place or clip anything by itself. The method receiving the value decides how to interpret its coordinates—for example, ImGui uses it as a screen-space drawing region.

Derived fields such as xMin/xMax/yMin/yMax/center recalculate the same bounds. Changing a local rectangle does not move something drawn earlier; pass it to the drawing API again.

local area = Rect.new(20, 30, 200, 40)
print(area.center) -- (120.00, 50.00)
Rect.new(x: number, y: number, width: number, height: number): Rect
Parameter Type Description
x number Position on the X axis.
y number Position on the Y axis.
width number Width.
height number Height.

Every Rect property is readable and writable.

Property Type Access Description
x number get/set X position.
y number get/set Y position.
width number get/set Width.
height number get/set Height.
xMin number get/set Minimum X coordinate.
yMin number get/set Minimum Y coordinate.
xMax number get/set Maximum X coordinate.
yMax number get/set Maximum Y coordinate.
center Vector2 get/set Rectangle center.

For Rect.new(10, 20, 30, 40), the values are:

Property Value
x, y 10, 20
width, height 30, 40
xMin, yMin 10, 20
xMax, yMax 40, 60
center (25, 40)

You can change the position and size, the boundaries, or the center:

local area = Rect.new(20, 20, 320, 180)
area.width = 400
area.center = Vector2.new(500, 300)

These related properties describe the same rectangle. After changing a boundary, center, position, or size, read any other values again before using them in later calculations.

local area = Rect.new(20, 20, 320, 180)
Scheduler:OnFrame(function()
ImGui:DrawTexture(texture, area)
end)

The API receiving a Rect defines its coordinate system. The type itself only stores a rectangular region. See ImGui for practical drawing methods.