Skip to content

topdownengine.math Reference

lerp(start, end, t)

This function linearly interpolates two floats or Vectors. Accepts t values outside of the [0, 1] range. The start and end MUST be the same type.

Parameters:

Name Type Description Default
start float | Vector2

Start Vector2/float

required
end float | Vector2

End Vector2/float

required
t float

Interpolation weight

required

Returns:

Type Description
Vector2 | float

pygame.Vector2|float: The interpolated Vector2/float

Raises:

Type Description
TypeError

If start and end are not the same type.

Source code in topdownengine\math.py
def lerp(start: float|pg.Vector2, end: float|pg.Vector2, t: float) -> pg.Vector2|float:
    """This function linearly interpolates two floats or Vectors. Accepts t values
    outside of the [0, 1] range. The start and end MUST be the same type.

    Args:
        start (float|pygame.Vector2): Start Vector2/float
        end (float|pygame.Vector2): End Vector2/float
        t (float): Interpolation weight

    Returns: 
        pygame.Vector2|float: The interpolated Vector2/float

    Raises:
        TypeError: If `start` and `end` are not the same type.
    """
    if type(start) != type(end):
        raise TypeError(f"{type(start)} and {type(end)} are not the same; they must be equal.")
    return start + (end - start) * t

scale_rect(rect, scalar)

This function scales a Rect's position and size by a given scalar.

Parameters:

Name Type Description Default
rect Rect | FRect

The Rect object to scale

required
scalar int | float

The number to scale by

required

Returns:

Type Description
Rect | FRect

pygame.Rect|pygame.FRect: The scaled Rect object

Raises:

Type Description
ValueError

If scalar <= 0.

Source code in topdownengine\math.py
def scale_rect(rect: pg.Rect|pg.FRect, scalar: int|float) -> pg.Rect|pg.FRect:
    """This function scales a Rect's position and size by a given scalar.

    Args:
        rect (pygame.Rect|pygame.FRect): The Rect object to scale
        scalar (int|float): The number to scale by

    Returns:
        pygame.Rect|pygame.FRect: The scaled Rect object

    Raises:
        ValueError: If `scalar` <= 0.
    """
    if scalar <= 0:
        raise ValueError("Scalar must be greater than 0.")
    new_rect = rect.copy()
    new_rect.width *= scalar
    new_rect.height *= scalar
    new_rect.top *= scalar
    new_rect.left *= scalar

    return new_rect