math_utils.py

Math Utilities - Essential Mathematical Functions for Game Development

LOCATION: lunaengine/utils/math_utils.py

DESCRIPTION:
Collection of fundamental mathematical functions commonly used in game
development. Provides optimized implementations for interpolation,
clamping, distance calculations, and vector operations.

KEY FUNCTIONS:
- lerp: Linear interpolation for smooth transitions
- clamp: Value constraint within specified ranges
- distance: Euclidean distance between points
- normalize_vector: Vector normalization for movement calculations
- angle_between_points: Angle calculation for directional systems

LIBRARIES USED:
- math: Core mathematical operations and trigonometric functions
- numpy: High-performance numerical operations (optional)
- typing: Type annotations for coordinates and return values

USAGE:
>>> smoothed_value = lerp(start, end, 0.5)
>>> constrained_value = clamp(value, 0, 100)
>>> dist = distance((x1, y1), (x2, y2))
>>> direction = normalize_vector(dx, dy)
>>> angle = angle_between_points(point_a, point_b)

Global Functions

def lerp(a: float, b: float, t: float) -> float

Linear interpolation between a and b

def clamp(value: float, min_val: float, max_val: float) -> float

Clamp value between min and max

def distance(point1: Tuple[float, float], point2: Tuple[float, float]) -> float

Calculate distance between two points

def normalize_vector(x: float, y: float) -> Tuple[float, float]

Normalize a 2D vector

def angle_between_points(point1: Tuple[float, float], point2: Tuple[float, float]) -> float

Calculate angle between two points in radians

def rgba_brightness(rgba: Tuple[int, int, int, float]) -> float

This function is used to calculate the overall brightness of an RGBA color

Parameters:
   rgba (Tuple[int, int, int, float]): A tuple representing the RGBA color
Returns:
   float: A float representing the brightness of the color

def individual_rgba_brightness(rgba: Tuple[int, int, int, float]) -> Tuple[float, float, float, float]

This function is used to calculate the individual brightness of each color channel (R, G, B, A)

Parameters:
   rgba (Tuple[int, int, int, float]): A tuple representing the RGBA color
Returns:
   Tuple[float, float, float, float]: A tuple representing the normalized RGBA values

def get_rgba_common(color1: Tuple[int, int, int, float] | Tuple[int, int, int], color2: Tuple[int, int, int, float] | Tuple[int, int, int]) -> Tuple[int, int, int, float]

This function is used to get the common RGBA values between two colors

Parameters:
   color1 (Tuple[int, int, int, float]): A tuple representing the first RGBA color
   color2 (Tuple[int, int, int, float]): A tuple representing the second RGBA color
Returns:
   Tuple[int, int, int, float]: A tuple representing the common RGBA values

def humanize_number(number: float, decimal_places: int = 2) -> str

This function is used to format a number into a human-readable string
Like: 1K, 1M, 1B, etc.
Also: 1.5K, 2.34M, etc.
Parameters:
   number (float): A float representing the number to be formatted
   decimal_places (int): Number of decimal places to include
Returns:
   str: A string representing the formatted number

def humanize_time(seconds: float) -> str

This function is used to format a time duration in seconds into a human-readable string
Like: 1h 30m, 2d 5h, etc.

Parameters:
   seconds (float): A float representing the time duration in seconds
Returns:
   str: A string representing the formatted time duration

def humanize_size(size: float) -> str

This function will convert sizes in bytes to a human-readable format
Parameters:
   size (float): A float representing the size in bytes
Returns:
   str: A string representing the human-readable size

def generate_matrix(rows: Any, cols: Any, dtype: Any = np.float32) -> np.ndarray

Generate matrix of zeros
Parameters:
   rows (int): Number of rows in the matrix
   cols (int): Number of columns in the matrix
   dtype (np.dtype): Data type of the matrix
Returns:
   np.ndarray: Matrix of zeros

def get_radius_by_diameter(diameter: float) -> float

No documentation

def get_diameter_by_radius(radius: float) -> float

No documentation

def get_circle_area(radius: float) -> float

No documentation

def get_circle_circumference(radius: float) -> float

No documentation

def get_diameter_by_area(area: float) -> float

No documentation

def get_diameter_by_circumference(circumference: float) -> float

No documentation

def get_area_by_diameter(diameter: float) -> float

No documentation

def get_area_by_radius(radius: float) -> float

No documentation

def get_mid_colors(color1: Tuple[int, int, int, float] | Tuple[int, int, int], color2: Tuple[int, int, int, float] | Tuple[int, int, int]) -> Tuple[int, int, int] | Tuple[int, int, int, float]

Get the middle color between two colors

Parameters:
   color1 (Tuple[int,int,int,float]|Tuple[int,int,int]): First color
   color2 (Tuple[int,int,int,float]|Tuple[int,int,int]): Second color
Returns:
   Tuple[int,int,int]|Tuple[int,int,int,float]: Middle color

Back to Utils Module