Kraken Logo

Kraken Engine

DocumentationGuidesShowcaseCommunity
Ctrl
K
Navigating the Docs
Building Docs
Overview
Anchor
AnimationController
Audio
Batcher
Body
Camera
Capsule
CastHit
CharacterBody
Circle
Collision
Color
DistanceJoint
Effect
Event
FilterJoint
Flip (Texture)
Font
ImageLayer
InputAction
Joint
Layer
Line
Map
MapObject
Mask
MotorJoint
MouseJoint
ObjectGroup
Orchestrator
PixelArray
PolarCoordinate
Polygon
PrismaticJoint
Rect
RevoluteJoint
RigidBody
Sample
Sampler
Shader
SheetStrip
StaticBody
Stream
Style
Terrain (TileSet)
Text
TextProperties
Texture
Tile (TileLayer)
Tile (TileSet)
TileLayer
TileResult (TileLayer)
TileSet
Timer
Transform
Tween
Vec2
Vertex
WeldJoint
WheelJoint
World

Built bydurkisneer1.Kraken Engine is open source and available onGitHub.

  1. Docs
  2. Classes
  3. Rect

Rect

Represents a rectangle with position and size.

Constructor

  • Rect() → RectRect() → Rect
  • Rect(size: Vec2) → RectRect(size: Vec2) → Rect
  • Rect(x: float, y: float, w: float, h: float) → RectRect(x: float, y: float, w: float, h: float) → Rect
  • Rect(x: float, y: float, size: Vec2) → RectRect(x: float, y: float, size: Vec2) → Rect
  • Rect(pos: Vec2, w: float, h: float) → RectRect(pos: Vec2, w: float, h: float) → Rect
  • Rect(pos: Vec2, size: Vec2) → RectRect(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
posThe position of the top-left corner 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) → Noneclamp(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) → Noneclamp(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.

Clamped

clamped(other: Rect) → Rectclamped(other: Rect) → Rect

Return a new Rect clamped within another rectangle.

Args

  • other : The rectangle to clamp within.

Returns

Rect : A new clamped rectangle.


clamped(min: Vec2, max: Vec2) → Rectclamped(min: Vec2, max: Vec2) → Rect

Return a new Rect clamped within the specified bounds.

Args

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

Returns

Rect : A new clamped rectangle.

Copy

copy() → Rectcopy() → Rect

Create a copy of this rectangle.

Returns

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

Fit

fit(other: Rect) → Nonefit(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.

Get Corners

get_corners() → list[Vec2]get_corners() → 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

get_edges() → list[Line]get_edges() → 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

inflate(offset: Vec2) → Noneinflate(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) → Nonemove(offset: Vec2) → None

Move the rectangle by the given offset.

Args

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

Moved

moved(offset: Vec2) → Rectmoved(offset: Vec2) → Rect

Return a new Rect moved by the given offset.

Args

  • offset : The offset to move by.

Returns

Rect : A new rectangle moved by the offset.

Scale By

scale_by(factor: float) → Nonescale_by(factor: float) → 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) → Nonescale_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) → Nonescale_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.

Scaled By

scaled_by(factor: float) → Rectscaled_by(factor: float) → Rect

Return a new Rect scaled by a uniform factor.

Args

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

Returns

Rect : A new scaled rectangle.


scaled_by(factor: Vec2) → Rectscaled_by(factor: Vec2) → Rect

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

Args

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

Returns

Rect : A new scaled rectangle.

Scaled To

scaled_to(size: Vec2) → Rectscaled_to(size: Vec2) → Rect

Return a new Rect scaled to the specified size.

Args

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

Returns

Rect : A new scaled rectangle.

PreviousPrismaticJoint
NextRevoluteJoint

On this page

ConstructorPropertiesMethodsClampClampedCopyFitGet CornersGet EdgesInflateMoveMovedScale ByScale ToScaled ByScaled To