Raxol.Core.Utils.Math (Raxol Core v2.4.0)

Copy Markdown View Source

Shared numeric utilities.

Summary

Functions

Clamps value to the range [lo, hi].

Computes the scroll offset needed to keep index visible in a viewport of visible_count items starting at scroll_offset.

Functions

clamp(value, lo, hi)

@spec clamp(number(), number(), number()) :: number()

Clamps value to the range [lo, hi].

Examples

iex> Raxol.Core.Utils.Math.clamp(5, 0, 10)
5

iex> Raxol.Core.Utils.Math.clamp(-1, 0, 10)
0

iex> Raxol.Core.Utils.Math.clamp(15, 0, 10)
10

scroll_into_view(index, scroll_offset, visible_count)

@spec scroll_into_view(integer(), integer(), pos_integer()) :: integer()

Computes the scroll offset needed to keep index visible in a viewport of visible_count items starting at scroll_offset.

Returns the (possibly adjusted) scroll offset.

Examples

iex> Raxol.Core.Utils.Math.scroll_into_view(5, 0, 10)
0

iex> Raxol.Core.Utils.Math.scroll_into_view(12, 0, 10)
3

iex> Raxol.Core.Utils.Math.scroll_into_view(2, 5, 10)
2