brown.utils.math_helpers

General math helper tools.

methods

interpolate (line_start, line_end, x) permalink view source

Calculate the value of a line defined by two points at a given x pos

arguments:

line_start (Point or tuple): The start of the line

line_end (Point or tuple): The end of the line

x (Unit): The position to interpolate from

returns:

clamp_value (value, minimum, maximum) permalink view source

Clamp a value to fit within a range.

arguments:

value (number or Unit): The value to clamp

minimum (number or Unit): The lower bound

maximum (number or Unit): The upper bound

returns:

int or float: The clamped value

  • If value is less than minimum, return minimum.
  • If value is greater than maximum, return maximum
  • Otherwise, return value

min_and_max (iterable) permalink view source

Efficiently get the min and max of an iterable

arguments:

iterable (Iterable): An iterable whose elements can all be compared with each other

returns:

tuple(min, max)

sign (value) permalink view source

Return the sign of a value as 1 or -1.

arguments:

value (int): The value to check

returns:

int: -1 if value is negative, and 1 if value is positive

float_to_rounded_fraction_tuple (value, target_denominator=None, limit_denominator=1024) permalink view source

Make a rounded fraction tuple from a float.

arguments:

value (float): The value to convert into a fraction tuple.

target_denominator (int): A denominator to use in the resulting tuple. If None, the denominator is calculated with a maximum value of limit_denominator.

limit_denominator (int): The maximum denominator value. If round_to is specified, this does nothing.

returns:

tuple(numerator, denominator)