Coverage for topdownengine/math.py: 100%
14 statements
« prev ^ index » next coverage.py v7.15.2, created at 2026-07-24 02:27 +0000
« prev ^ index » next coverage.py v7.15.2, created at 2026-07-24 02:27 +0000
1# Copyright (c) 2026 Shaurya Sharma
2# SPDX-License-Identifier: MIT
4import pygame as pg
6def lerp(start: float|pg.Vector2, end: float|pg.Vector2, t: float) -> pg.Vector2|float:
7 """This function linearly interpolates two floats or Vectors. Accepts t values
8 outside of the [0, 1] range. The start and end MUST be the same type.
10 Args:
11 start (float|pygame.Vector2): Start Vector2/float
12 end (float|pygame.Vector2): End Vector2/float
13 t (float): Interpolation weight
15 Returns:
16 pygame.Vector2|float: The interpolated Vector2/float
18 Raises:
19 TypeError: If `start` and `end` are not the same type.
20 """
21 if type(start) != type(end):
22 raise TypeError(f"{type(start)} and {type(end)} are not the same; they must be equal.")
23 return start + (end - start) * t
25def scale_rect(rect: pg.Rect|pg.FRect, scalar: int|float) -> pg.Rect|pg.FRect:
26 """This function scales a Rect's position and size by a given scalar.
28 Args:
29 rect (pygame.Rect|pygame.FRect): The Rect object to scale
30 scalar (int|float): The number to scale by
32 Returns:
33 pygame.Rect|pygame.FRect: The scaled Rect object
35 Raises:
36 ValueError: If `scalar` <= 0.
37 """
38 if scalar <= 0:
39 raise ValueError("Scalar must be greater than 0.")
40 new_rect = rect.copy()
41 new_rect.width *= scalar
42 new_rect.height *= scalar
43 new_rect.top *= scalar
44 new_rect.left *= scalar
46 return new_rect