Skip to content

sonolus.script.vec

Vec2

Bases: Record

A 2D vector.

Usage
Vec2(x: float, y: float)

angle property

Calculate the angle of the vector in radians from the positive x-axis.

Returns:

Type Description
Num

The angle of the vector in radians.

magnitude property

Calculate the magnitude (length) of the vector.

Returns:

Type Description
Num

The magnitude of the vector.

tuple property

Return the vector as a tuple (x, y).

Returns:

Type Description
tuple[float, float]

A tuple representation of the vector.

__add__(other)

Add this vector to another vector and return a new vector.

Parameters:

Name Type Description Default
other Self

The vector to add.

required

Returns:

Type Description
Self

A new vector resulting from the addition.

__mul__(other)

Multiply this vector by another vector or a scalar and return a new vector.

Parameters:

Name Type Description Default
other Self | float

The vector or scalar to multiply by.

required

Returns:

Type Description
Self

A new vector resulting from the multiplication.

__neg__()

Negate the vector (invert the direction) and return a new vector.

Returns:

Type Description
Self

A new vector with inverted direction.

__sub__(other)

Subtract another vector from this vector and return a new vector.

Parameters:

Name Type Description Default
other Self

The vector to subtract.

required

Returns:

Type Description
Self

A new vector resulting from the subtraction.

__truediv__(other)

Divide this vector by another vector or a scalar and return a new vector.

Parameters:

Name Type Description Default
other Self | float

The vector or scalar to divide by.

required

Returns:

Type Description
Self

A new vector resulting from the division.

dot(other)

Calculate the dot product of this vector with another vector.

Parameters:

Name Type Description Default
other Self

The other vector to calculate the dot product with.

required

Returns:

Type Description
Num

The dot product of the two vectors.

down() classmethod

Return a vector pointing downwards (x=0, y=-1).

Returns:

Type Description
Self

A new vector pointing downwards.

left() classmethod

Return a vector pointing to the left (x=-1, y=0).

Returns:

Type Description
Self

A new vector pointing to the left.

normalize()

Normalize the vector (set the magnitude to 1) and return a new vector.

Returns:

Type Description
Self

A new vector with magnitude 1.

one() classmethod

Return a vector with x and y set to 1.

Returns:

Type Description
Self

A new vector with x=1 and y=1.

orthogonal()

Return a vector orthogonal to this vector.

The orthogonal vector is rotated 90 degrees counter-clockwise from this vector.

Returns:

Type Description
Self

A new vector orthogonal to this vector.

right() classmethod

Return a vector pointing to the right (x=1, y=0).

Returns:

Type Description
Self

A new vector pointing to the right.

rotate(angle)

Rotate the vector by a given angle in radians and return a new vector.

Parameters:

Name Type Description Default
angle Num

The angle to rotate the vector by, in radians.

required

Returns:

Type Description
Self

A new vector rotated by the given angle.

rotate_about(angle, pivot)

Rotate the vector about a pivot by a given angle in radians and return a new vector.

Parameters:

Name Type Description Default
angle Num

The angle to rotate the vector by, in radians.

required
pivot Self

The pivot point to rotate about.

required

Returns:

Type Description
Self

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

up() classmethod

Return a vector pointing upwards (x=0, y=1).

Returns:

Type Description
Self

A new vector pointing upwards.

zero() classmethod

Return a vector with x and y set to 0.

Returns:

Type Description
Self

A new vector with x=0 and y=0.

pnpoly(vertices, test)

Check if a point is inside a polygon.

No guaranteed behavior for points on the edges or very close to the edges.

Parameters:

Name Type Description Default
vertices ArrayLike[Vec2] | tuple[Vec2, ...]

The vertices of the polygon.

required
test Vec2

The point to test.

required

Returns:

Type Description
bool

Whether the point is inside the polygon.