Skip to content

sonolus.script.interval

Interval

Bases: Record

A closed interval.

Usage
Interval(start: float, end: float)

is_empty property

Whether the has a start greater than its end.

length property

The length of the interval.

May be negative if the end is less than the start.

mid property

The midpoint of the interval.

tuple property

The interval as a tuple.

__add__(other)

Add a value to both ends of the interval.

Parameters:

Name Type Description Default
other float | int

The value to add.

required

Returns:

Type Description
Self

A new interval with the value added to both ends.

__and__(other)

Get the intersection of two intervals.

The resulting interval will be empty and may have a negative length if the two intervals do not overlap.

Parameters:

Name Type Description Default
other Self

The other interval.

required

Returns:

Type Description
Self

A new interval representing the intersection of the two intervals.

__contains__(item)

Check if an item is within the interval.

Parameters:

Name Type Description Default
item Self | float | int

The item to check. If it is an interval, it must be fully contained within this interval.

required

Returns:

Type Description
bool

True if the item is within the interval, False otherwise.

__floordiv__(other)

Divide both ends of the interval by a value and floor the result.

Parameters:

Name Type Description Default
other float | int

The value to divide by.

required

Returns:

Type Description
Self

A new interval with both ends divided by the value and floored.

__mul__(other)

Multiply both ends of the interval by a value.

Parameters:

Name Type Description Default
other float | int

The value to multiply by.

required

Returns:

Type Description
Self

A new interval with both ends multiplied by the value.

__sub__(other)

Subtract a value from both ends of the interval.

Parameters:

Name Type Description Default
other float | int

The value to subtract.

required

Returns:

Type Description
Self

A new interval with the value subtracted from both ends.

__truediv__(other)

Divide both ends of the interval by a value.

Parameters:

Name Type Description Default
other float | int

The value to divide by.

required

Returns:

Type Description
Self

A new interval with both ends divided by the value.

clamp(x)

Clamp a value to the interval.

Parameters:

Name Type Description Default
x float

The value to clamp.

required

Returns:

Type Description
float

The clamped value.

expand(value)

Expand the interval by a value on both ends.

Parameters:

Name Type Description Default
value float | int

The value to expand by.

required

Returns:

Type Description
Self

A new interval with the value subtracted from the start and added to the end.

lerp(x)

Linearly interpolate a value within the interval.

Parameters:

Name Type Description Default
x float

The interpolation factor.

required

Returns:

Type Description
float

The interpolated value.

lerp_clamped(x)

Linearly interpolate a value within the interval, clamped to the interval.

Parameters:

Name Type Description Default
x float

The interpolation factor.

required

Returns:

Type Description
float

The interpolated value.

shrink(value)

Shrink the interval by a value on both ends.

Parameters:

Name Type Description Default
value float | int

The value to shrink by.

required

Returns:

Type Description
Self

A new interval with the value subtracted from the start and added to the end.

unlerp(x)

Inverse linear interpolation of a value within the interval.

Parameters:

Name Type Description Default
x float

The value to unlerp.

required

Returns:

Type Description
float

The unlerped value.

unlerp_clamped(x)

Inverse linear interpolation of a value within the interval, clamped to the interval.

Parameters:

Name Type Description Default
x float

The value to unlerp.

required

Returns:

Type Description
float

The unlerped value.

zero() classmethod

Get an empty interval.

clamp(x, a, b)

Clamp a value to an interval.

Parameters:

Name Type Description Default
x float

The value to clamp.

required
a float

The start of the interval.

required
b float

The end of the interval.

required

Returns:

Type Description
float

The clamped value.

lerp(a, b, x)

Linearly interpolate between two values.

Parameters:

Name Type Description Default
a T

The start value.

required
b T

The end value.

required
x float

The interpolation factor.

required

Returns:

Type Description
T

The interpolated value.

lerp_clamped(a, b, x)

Linearly interpolate between two values, clamped to the interval.

Parameters:

Name Type Description Default
a T

The start value.

required
b T

The end value.

required
x float

The interpolation factor.

required

Returns:

Type Description
T

The interpolated value.

remap(a, b, c, d, x)

Remap a value from one interval to another.

Parameters:

Name Type Description Default
a float

The start of the input interval.

required
b float

The end of the input interval.

required
c float

The start of the output interval.

required
d float

The end of the output interval.

required
x float

The value to remap.

required

Returns:

Type Description
float

The remapped value.

remap_clamped(a, b, c, d, x)

Remap a value from one interval to another, clamped to the output interval.

Parameters:

Name Type Description Default
a float

The start of the input interval.

required
b float

The end of the input interval.

required
c float

The start of the output interval.

required
d float

The end of the output interval.

required
x float

The value to remap.

required

Returns:

Type Description
float

The remapped value.

unlerp(a, b, x)

Inverse linear interpolation.

Parameters:

Name Type Description Default
a float

The start value.

required
b float

The end value.

required
x float

The value to unlerp.

required

Returns:

Type Description
float

The unlerped value.

unlerp_clamped(a, b, x)

Inverse linear interpolation, clamped to the interval.

Parameters:

Name Type Description Default
a float

The start value.

required
b float

The end value.

required
x float

The value to unlerp.

required

Returns:

Type Description
float

The unlerped value.