Rect
Represents a rectangle with position and size.
Constructor
-
Rect() → Rect -
Rect(
x: SupportsFloat,
y: SupportsFloat,
w: SupportsFloat,
h: SupportsFloat
) → Rect -
Rect(pos: Vec2, size: Vec2) → Rect
Represents a rectangle with position and size.
Properties
| Name | Description | Type |
|---|---|---|
bottom | The y coordinate of the bottom edge. | float |
bottom_left | The position of the bottom-left corner as (x, y). | Vec2 |
bottom_mid | The position of the bottom-middle point as (x, y). | Vec2 |
bottom_right | The position of the bottom-right corner as (x, y). | Vec2 |
center | The position of the center point as (x, y). | Vec2 |
h | The height of the rectangle. | float |
left | The x coordinate of the left edge. | float |
mid_left | The position of the middle-left point as (x, y). | Vec2 |
mid_right | The position of the middle-right point as (x, y). | Vec2 |
right | The x coordinate of the right edge. | float |
size | The size of the rectangle as (width, height). | Vec2 |
top | The y coordinate of the top edge. | float |
top_left | The position of the top-left corner as (x, y). | Vec2 |
top_mid | The position of the top-middle point as (x, y). | Vec2 |
top_right | The position of the top-right corner as (x, y). | Vec2 |
w | The width of the rectangle. | float |
x | The x coordinate of the top-left corner. | float |
y | The y coordinate of the top-left corner. | float |
Methods
Clamp
clamp(other: Rect) → NoneClamp this rectangle to be within another rectangle.
Args
other: The rectangle to clamp within.
Raises
ValueError: If this rectangle is larger than the clamp area.
clamp(min: Vec2, max: Vec2) → NoneClamp this rectangle to be within the specified bounds.
Args
min: The minimum bounds as (min_x, min_y).max: The maximum bounds as (max_x, max_y).
Raises
ValueError: If min >= max or rectangle is larger than the clamp area.
Copy
copy() → RectCreate a copy of this rectangle.
Returns
Rect : A new Rect with the same position and size.
Fit
fit(other: Rect) → NoneScale this rectangle to fit inside another rectangle while maintaining aspect ratio.
Args
other: The rectangle to fit inside.
Raises
ValueError: If other rectangle has non-positive dimensions.
Inflate
inflate(offset: Vec2) → NoneInflate the rectangle by the given offset.
The rectangle grows in all directions. The position is adjusted to keep the center in the same place.
Args
offset: The amount to inflate by as (dw, dh).
Move
move(offset: Vec2) → NoneMove the rectangle by the given offset.
Args
offset: The offset to move by as (dx, dy).
Scale By
scale_by(factor: SupportsFloat) → NoneScale the rectangle by a uniform factor.
Args
factor: The scaling factor (must be > 0).
Raises
ValueError: If factor is <= 0.
scale_by(factor: Vec2) → NoneScale the rectangle by different factors for width and height.
Args
factor: The scaling factors as (scale_x, scale_y).
Raises
ValueError: If any factor is <= 0.
Scale To
scale_to(size: Vec2) → NoneScale the rectangle to the specified size.
Args
size: The new size as (width, height).
Raises
ValueError: If width or height is <= 0.