Skip to content

sonolus.script.quad

QuadLike = _QuadLike | Quad

A type that can be used as a quad.

Quad

Bases: Record

A quad defined by its four corners.

Usage
Quad(bl: Vec2, tl: Vec2, tr: Vec2, br: Vec2)

bl: Vec2 instance-attribute

The bottom-left corner of the quad.

br: Vec2 instance-attribute

The bottom-right corner of the quad.

center: Vec2 property

The center of the quad.

mb: Vec2 property

The midpoint of the bottom edge of the quad.

ml: Vec2 property

The midpoint of the left edge of the quad.

mr: Vec2 property

The midpoint of the right edge of the quad.

mt: Vec2 property

The midpoint of the top edge of the quad.

tl: Vec2 instance-attribute

The top-left corner of the quad.

tr: Vec2 instance-attribute

The top-right corner of the quad.

contains_point(point: Vec2) -> bool

Check if the quad contains the given point.

It is not guaranteed whether points on the edges of the quad are considered inside or outside.

Parameters:

Name Type Description Default
point Vec2

The point to check.

required

Returns:

Type Description
bool

True if the point is inside the quad, False otherwise.

from_quad(value: QuadLike) -> Quad classmethod

Create a quad from a quad-like value.

permute(count: int = 1) -> Quad

Perform a cyclic permutation of the quad's vertices and return a new quad.

On a square, this operation is equivalent to rotating the square counterclockwise 90 degrees count times.

Negative values of count are allowed and will rotate the quad clockwise.

Parameters:

Name Type Description Default
count int

The number of vertices to shift. Defaults to 1.

1

Returns:

Type Description
Quad

The permuted quad.

rotate(angle: float) -> Quad

Rotate the quad by the given angle about the origin and return a new quad.

Parameters:

Name Type Description Default
angle float

The angle of rotation in radians. Positive angles rotate counterclockwise.

required

Returns:

Type Description
Quad

A new quad rotated by the given angle.

rotate_about(angle: float, /, pivot: Vec2) -> Quad

Rotate the quad by the given angle about the given pivot and return a new quad.

Parameters:

Name Type Description Default
angle float

The angle of rotation in radians. Positive angles rotate counterclockwise.

required
pivot Vec2

The pivot point for rotation.

required

Returns:

Type Description
Quad

A new quad rotated about the pivot by the given angle.

rotate_centered(angle: float) -> Quad

Rotate the quad by the given angle about its center and return a new quad.

Parameters:

Name Type Description Default
angle float

The angle of rotation in radians. Positive angles rotate counterclockwise.

required

Returns:

Type Description
Quad

A new quad rotated about its center by the given angle.

scale(factor: Vec2) -> Quad

Scale the quad by the given factor about the origin and return a new quad.

scale_about(factor: Vec2, /, pivot: Vec2) -> Quad

Scale the quad by the given factor about the given pivot and return a new quad.

scale_centered(factor: Vec2) -> Quad

Scale the quad by the given factor about its center and return a new quad.

translate(translation: Vec2) -> Quad

Translate the quad by the given translation and return a new quad.

zero() -> Quad classmethod

Return a quad with all corners set to (0, 0).

Returns:

Type Description
Quad

A new quad with all corners at the origin.

Rect

Bases: Record

A rectangle defined by its top, right, bottom, and left edges.

Usage
Rect(t: float, r: float, b: float, l: float)

b: float instance-attribute

The bottom edge of the rectangle.

bl: Vec2 property

The bottom-left corner of the rectangle.

br: Vec2 property

The bottom-right corner of the rectangle.

center: Vec2 property

The center of the rectangle.

h: float property

The height of the rectangle.

l: float instance-attribute

The left edge of the rectangle.

mb: Vec2 property

The middle-bottom point of the rectangle.

ml: Vec2 property

The middle-left point of the rectangle.

mr: Vec2 property

The middle-right point of the rectangle.

mt: Vec2 property

The middle-top point of the rectangle.

r: float instance-attribute

The right edge of the rectangle.

t: float instance-attribute

The top edge of the rectangle.

tl: Vec2 property

The top-left corner of the rectangle.

tr: Vec2 property

The top-right corner of the rectangle.

w: float property

The width of the rectangle.

as_quad() -> Quad

Convert the rectangle to a Quad.

contains_point(point: Vec2) -> bool

Check if the rectangle contains the given point.

Parameters:

Name Type Description Default
point Vec2

The point to check.

required

Returns:

Type Description
bool

True if the point is inside the rectangle, False otherwise.

expand(expansion: Vec2) -> Rect

Expand the rectangle by the given amount and return a new rectangle.

from_center(center: Vec2, dimensions: Vec2) -> Rect classmethod

Create a rectangle from its center and dimensions.

from_margin(a: float, b: float | None = None, c: float | None = None, d: float | None = None) -> Self classmethod

from_margin(trbl: float) -> Rect
from_margin(tb: float, lr: float) -> Rect
from_margin(t: float, lr: float, b: float) -> Rect
from_margin(t: float, r: float, b: float, l: float) -> Rect

Create a rectangle based on margins (edge distances) from the origin.

Compared to the regular Rect constructor, this method negates the bottom and left values, and supports shorthands when fewer than four arguments are provided.

The following signatures are supported:

  • from_margin(trbl): All margins set to trbl.
  • from_margin(tb, lr): Top and bottom margins set to tb, left and right margins set to lr.
  • from_margin(t, lr, b): Top margin set to t, left and right margins set to lr, bottom margin set to b.
  • from_margin(t, r, b, l): Top, right, bottom, and left margins set to t, r, b, and l respectively.
Usage
Rect.from_margin(1)  # Rect(t=1, r=1, b=-1, l=-1)
Rect.from_margin(1, 2)  # Rect(t=1, r=2, b=-1, l=-2)
Rect.from_margin(1, 2, 3)  # Rect(t=1, r=2, b=-3, l=-2)
Rect.from_margin(1, 2, 3, 4)  # Rect(t=1, r=2, b=-3, l=-4)

scale(factor: Vec2) -> Rect

Scale the rectangle by the given factor about the origin and return a new rectangle.

scale_about(factor: Vec2, /, pivot: Vec2) -> Rect

Scale the rectangle by the given factor about the given pivot and return a new rectangle.

scale_centered(factor: Vec2) -> Rect

Scale the rectangle by the given factor about its center and return a new rectangle.

shrink(shrinkage: Vec2) -> Rect

Shrink the rectangle by the given amount and return a new rectangle.

translate(translation: Vec2) -> Rect

Translate the rectangle by the given translation and return a new rectangle.