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.
At a glance
33 membersConstructormethodbottompropertybottom_leftpropertybottom_midpropertybottom_rightpropertycenterpropertyhpropertyleftpropertymid_leftpropertymid_rightpropertypospropertyrightpropertysizepropertytoppropertytop_leftpropertytop_midpropertytop_rightpropertywpropertyxpropertyypropertyclampmethodclampedmethodcopymethodfitmethodget_cornersmethodget_edgesmethodinflatemethodmovemethodmovedmethodscale_bymethodscale_tomethodscaled_bymethodscaled_tomethodReference
PYTHON APIOverloaded function.
Rect()
Create a Rect with default values (0, 0, 0, 0).
Rect(size: Vec2)
Create a Rect at position (0, 0) with the given size.
Args
- size (
Vec2) — The size as a Vec2 (width, height).
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.
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).
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.
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
propertybottom: floatThe y coordinate of the bottom edge.
bottom_left
propertybottom_left: Vec2The position of the bottom-left corner as (x, y).
bottom_mid
propertybottom_mid: Vec2The position of the bottom-middle point as (x, y).
bottom_right
propertybottom_right: Vec2The position of the bottom-right corner as (x, y).
center
propertycenter: Vec2The position of the center point as (x, y).
h
propertyh: floatThe height of the rectangle.
left
propertyleft: floatThe x coordinate of the left edge.
mid_left
propertymid_left: Vec2The position of the middle-left point as (x, y).
mid_right
propertymid_right: Vec2The position of the middle-right point as (x, y).
pos
propertypos: Vec2The position of the top-left corner as (x, y).
right
propertyright: floatThe x coordinate of the right edge.
size
propertysize: Vec2The size of the rectangle as (width, height).
top
propertytop: floatThe y coordinate of the top edge.
top_left
propertytop_left: Vec2The position of the top-left corner as (x, y).
top_mid
propertytop_mid: Vec2The position of the top-middle point as (x, y).
top_right
propertytop_right: Vec2The position of the top-right corner as (x, y).
w
propertyw: floatThe width of the rectangle.
x
propertyx: floatThe x coordinate of the top-left corner.
y
propertyy: floatThe y coordinate of the top-left corner.
Overloaded function.
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.
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.
Overloaded function.
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.
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
methodcopy(self) -> RectCreate a copy of this rectangle.
Returns
- Rect — A new Rect with the same position and size.
fit
methodfit(self, other: Rect) -> NoneScale 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
methodget_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
methodget_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
methodinflate(self, 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 (
Vec2) — The amount to inflate by as (dw, dh).
move
methodmove(self, offset: Vec2) -> NoneMove the rectangle by the given offset.
Args
- offset (
Vec2) — The offset to move by as (dx, dy).
moved
methodReturn 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(self, factor: float) -> Nonescale_by(self, factor: Vec2) -> NoneOverloaded function.
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.
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
methodscale_to(self, size: Vec2) -> NoneScale the rectangle to the specified size.
Args
- size (
Vec2) — The new size as (width, height).
Raises
- ValueError — If width or height is <= 0.
Overloaded function.
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.
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
methodReturn a new Rect scaled to the specified size.
Args
- size (
Vec2) — The new size as (width, height).
Returns
- Rect — A new scaled rectangle.