CLASS v1.8.0

Rect

pykraken.Rect

Represents a rectangle with position and size.

More about Rect

A Rect is defined by its top-left corner position (x, y) and dimensions (w, h). Supports various geometric operations, collision detection, and positioning methods.

Reference

PYTHON API

Constructor

method6 overloads
Rect()
Rect(size: Vec2)
Rect(x: float, y: float, w: float, h: float)
Rect(x: float, y: float, size: Vec2)
Rect(pos: Vec2, w: float, h: float)
Rect(pos: Vec2, size: Vec2)

Overloaded function.

  1. Rect()

Create a Rect with default values (0, 0, 0, 0).

  1. Rect(size: Vec2)

Create a Rect at position (0, 0) with the given size.

Args

  • size (Vec2) — The size as a Vec2 (width, height).
  1. Rect(x: float, y: float, w: float, h: float)

Create a Rect with specified position and dimensions.

Args

  • x (float) — The x coordinate of the top-left corner.
  • y (float) — The y coordinate of the top-left corner.
  • w (float) — The width of the rectangle.
  • h (float) — The height of the rectangle.
  1. Rect(x: float, y: float, size: Vec2)

Create a Rect with specified position and size vector.

Args

  • x (float) — The x coordinate of the top-left corner.
  • y (float) — The y coordinate of the top-left corner.
  • size (Vec2) — The size as a Vec2 (width, height).
  1. Rect(pos: Vec2, w: float, h: float)

Create a Rect with specified position vector and dimensions.

Args

  • pos (Vec2) — The position as a Vec2 (x, y).
  • w (float) — The width of the rectangle.
  • h (float) — The height of the rectangle.
  1. Rect(pos: Vec2, size: Vec2)

Create a Rect with specified position and size vectors.

Args

  • pos (Vec2) — The position as a Vec2 (x, y).
  • size (Vec2) — The size as a Vec2 (width, height).

bottom

property
bottom: float

The y coordinate of the bottom edge.

bottom_left

property
bottom_left: Vec2

The position of the bottom-left corner as (x, y).

bottom_mid

property
bottom_mid: Vec2

The position of the bottom-middle point as (x, y).

bottom_right

property
bottom_right: Vec2

The position of the bottom-right corner as (x, y).

center

property
center: Vec2

The position of the center point as (x, y).

h

property
h: float

The height of the rectangle.

left

property
left: float

The x coordinate of the left edge.

mid_left

property
mid_left: Vec2

The position of the middle-left point as (x, y).

mid_right

property
mid_right: Vec2

The position of the middle-right point as (x, y).

pos

property
pos: Vec2

The position of the top-left corner as (x, y).

size

property
size: Vec2

The size of the rectangle as (width, height).

top

property
top: float

The y coordinate of the top edge.

top_left

property
top_left: Vec2

The position of the top-left corner as (x, y).

top_mid

property
top_mid: Vec2

The position of the top-middle point as (x, y).

top_right

property
top_right: Vec2

The position of the top-right corner as (x, y).

w

property
w: float

The width of the rectangle.

x

property
x: float

The x coordinate of the top-left corner.

y

property
y: float

The y coordinate of the top-left corner.

clamp

method2 overloads
clamp(self, other: Rect) -> None
clamp(self, min: Vec2, max: Vec2) -> None

Overloaded function.

  1. clamp(self, other: Rect) -> None

Clamp this rectangle to be within another rectangle.

Args

  • other (Rect) — The rectangle to clamp within.

Raises

  • ValueError — If this rectangle is larger than the clamp area.
  1. clamp(self, min: Vec2, max: Vec2) -> None

Clamp this rectangle to be within the specified bounds.

Args

  • min (Vec2) — The minimum bounds as (min_x, min_y).
  • max (Vec2) — The maximum bounds as (max_x, max_y).

Raises

  • ValueError — If min >= max or rectangle is larger than the clamp area.

clamped

method2 overloads
clamped(self, other: Rect) -> Rect
clamped(self, min: Vec2, max: Vec2) -> Rect

Overloaded function.

  1. clamped(self, other: Rect) -> Rect

Return a new Rect clamped within another rectangle.

Args

  • other (Rect) — The rectangle to clamp within.

Returns

  • Rect — A new clamped rectangle.
  1. clamped(self, min: Vec2, max: Vec2) -> Rect

Return a new Rect clamped within the specified bounds.

Args

  • min (Vec2) — The minimum bounds as (min_x, min_y).
  • max (Vec2) — The maximum bounds as (max_x, max_y).

Returns

  • Rect — A new clamped rectangle.

copy

method
copy(self) -> Rect

Create a copy of this rectangle.

Returns

  • Rect — A new Rect with the same position and size.

fit

method
fit(self, other: Rect) -> None

Scale this rectangle to fit inside another rectangle while maintaining aspect ratio.

Args

  • other (Rect) — The rectangle to fit inside.

Raises

  • ValueError — If other rectangle has non-positive dimensions.
get_corners(self) -> list[Vec2]

Get the corners of the rectangle.

Returns

List[Vec2]: A list of the four corners in the order: top-left, top-right, bottom-right, bottom-left.

get_edges

method
get_edges(self) -> list[Line]

Get the edges of the rectangle as Line segments.

Returns

List[Line]: A list of the four edges as Line objects in the order: top, right, bottom, left.

inflate

method
inflate(self, 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 (Vec2) — The amount to inflate by as (dw, dh).

move

method
move(self, offset: Vec2) -> None

Move the rectangle by the given offset.

Args

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

moved

method
moved(self, offset: Vec2) -> Rect

Return a new Rect moved by the given offset.

Args

  • offset (Vec2) — The offset to move by.

Returns

  • Rect — A new rectangle moved by the offset.

scale_by

method2 overloads
scale_by(self, factor: float) -> None
scale_by(self, factor: Vec2) -> None

Overloaded function.

  1. scale_by(self, factor: float) -> None

Scale the rectangle by a uniform factor.

Args

  • factor (float) — The scaling factor (must be > 0).

Raises

  • ValueError — If factor is <= 0.
  1. scale_by(self, factor: Vec2) -> None

Scale the rectangle by different factors for width and height.

Args

  • factor (Vec2) — The scaling factors as (scale_x, scale_y).

Raises

  • ValueError — If any factor is <= 0.

scale_to

method
scale_to(self, size: Vec2) -> None

Scale the rectangle to the specified size.

Args

  • size (Vec2) — The new size as (width, height).

Raises

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

scaled_by

method2 overloads
scaled_by(self, factor: float) -> Rect
scaled_by(self, factor: Vec2) -> Rect

Overloaded function.

  1. scaled_by(self, factor: float) -> Rect

Return a new Rect scaled by a uniform factor.

Args

  • factor (float) — The scaling factor (must be > 0).

Returns

  • Rect — A new scaled rectangle.
  1. scaled_by(self, factor: Vec2) -> Rect

Return a new Rect scaled by different factors for width and height.

Args

  • factor (Vec2) — The scaling factors as (scale_x, scale_y).

Returns

  • Rect — A new scaled rectangle.

scaled_to

method
scaled_to(self, size: Vec2) -> Rect

Return a new Rect scaled to the specified size.

Args

  • size (Vec2) — The new size as (width, height).

Returns

  • Rect — A new scaled rectangle.

Find your next step

Start typing to search the manual.