Skip to content

sonolus.script.interval

Interval

Bases: Record

A closed interval.

Usage
Interval(start: float, end: float)

is_empty: bool property

Whether the interval has a start greater than its end.

length: float property

The length of the interval.

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

mid: float property

The midpoint of the interval.

tuple property

The interval as a tuple.

__add__(other: float | int) -> Interval

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
Interval

A new interval with the value added to both ends.

__and__(other: Interval) -> Interval

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 Interval

The other interval.

required

Returns:

Type Description
Interval

A new interval representing the intersection of the two intervals.

__contains__(item: Interval | float | int) -> bool

Check if an item is within the interval.

Parameters:

Name Type Description Default
item Interval | 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: float | int) -> Interval

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
Interval

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

__mul__(other: float | int) -> Interval

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
Interval

A new interval with both ends multiplied by the value.

__sub__(other: float | int) -> Interval

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
Interval

A new interval with the value subtracted from both ends.

__truediv__(other: float | int) -> Interval

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
Interval

A new interval with both ends divided by the value.

clamp(x: float) -> float

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: float | int) -> Interval

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
Interval

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

lerp(x: float) -> float

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: float) -> float

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: float | int) -> Interval

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
Interval

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

unlerp(x: float) -> float

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: float) -> float

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() -> Interval classmethod

Get an empty interval.

clamp(x: float, a: float, b: float) -> float

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.

interp(xp: ArrayLike[float] | tuple[float, ...], fp: ArrayLike[float] | tuple[float, ...], x: float) -> float

Linearly interpolate a value within a sequence of points.

The sequence must have at least 2 elements and be sorted in increasing order of x-coordinates. For values of x outside the range of xp, the slope of the first or last segment is used to extrapolate.

Parameters:

Name Type Description Default
xp ArrayLike[float] | tuple[float, ...]

The x-coordinates of the points in increasing order.

required
fp ArrayLike[float] | tuple[float, ...]

The y-coordinates of the points.

required
x float

The x-coordinate to interpolate.

required

Returns:

Type Description
float

The interpolated value.

interp_clamped(xp: ArrayLike[float] | tuple[float, ...], fp: ArrayLike[float] | tuple[float, ...], x: float) -> float

Linearly interpolate a value within a sequence of points.

The sequence must have at least 2 elements and be sorted in increasing order of x-coordinates. For x-coordinates outside the range of the sequence, the respective endpoint of fp is returned.

Parameters:

Name Type Description Default
xp ArrayLike[float] | tuple[float, ...]

The x-coordinates of the points in increasing order.

required
fp ArrayLike[float] | tuple[float, ...]

The y-coordinates of the points.

required
x float

The x-coordinate to interpolate.

required

Returns:

Type Description
float

The interpolated value.

lerp(a: T, b: T, x: float) -> T

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: T, b: T, x: float) -> T

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: float, b: float, c: float, d: float, x: float) -> float

Linearly 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: float, b: float, c: float, d: float, x: float) -> float

Linearly 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: float, b: float, x: float) -> float

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: float, b: float, x: float) -> float

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.