Rect

Represents a rectangle with position and size.

Constructor

  • Rect() Rect
  • Rect(
        x: SupportsFloat,
        y: SupportsFloat,
        w: SupportsFloat,
        h: SupportsFloat
    ) Rect
  • Rect(
        x: SupportsFloat,
        y: SupportsFloat,
        size: Vec2
    ) Rect
  • Rect(
        pos: Vec2,
        w: SupportsFloat,
        h: SupportsFloat
    ) Rect
  • Rect(pos: Vec2, size: Vec2) Rect

Represents a rectangle with position and size.

Properties


NameDescriptionType
bottomThe y coordinate of the bottom edge.float
bottom_leftThe position of the bottom-left corner as (x, y).Vec2
bottom_midThe position of the bottom-middle point as (x, y).Vec2
bottom_rightThe position of the bottom-right corner as (x, y).Vec2
centerThe position of the center point as (x, y).Vec2
hThe height of the rectangle.float
leftThe x coordinate of the left edge.float
mid_leftThe position of the middle-left point as (x, y).Vec2
mid_rightThe position of the middle-right point as (x, y).Vec2
rightThe x coordinate of the right edge.float
sizeThe size of the rectangle as (width, height).Vec2
topThe y coordinate of the top edge.float
top_leftThe position of the top-left corner as (x, y).Vec2
top_midThe position of the top-middle point as (x, y).Vec2
top_rightThe position of the top-right corner as (x, y).Vec2
wThe width of the rectangle.float
xThe x coordinate of the top-left corner.float
yThe y coordinate of the top-left corner.float

Methods


Clamp

clamp(other: Rect) None

Clamp 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) None

Clamp 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() Rect

Create a copy of this rectangle.

Returns

Rect : A new Rect with the same position and size.

Fit

fit(other: Rect) None

Scale 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) None

Inflate 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) None

Move the rectangle by the given offset.

Args

  • offset : The offset to move by as (dx, dy).

Scale By

scale_by(factor: SupportsFloat) None

Scale the rectangle by a uniform factor.

Args

  • factor : The scaling factor (must be > 0).

Raises

  • ValueError : If factor is <= 0.

scale_by(factor: Vec2) None

Scale 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) None

Scale the rectangle to the specified size.

Args

  • size : The new size as (width, height).

Raises

  • ValueError : If width or height is <= 0.