"""Helper functions and utilities."""
import math
import pygame
[docs]
def distance(x1, y1, x2, y2):
"""Calculate distance between two points."""
return math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)
[docs]
def normalize_vector(x, y):
"""Normalize a vector to unit length."""
length = math.sqrt(x * x + y * y)
if length == 0:
return 0, 0
return x / length, y / length
[docs]
def clamp(value, min_val, max_val):
"""Clamp a value between min and max."""
return max(min_val, min(value, max_val))
[docs]
def interpolate(start, end, t):
"""Linear interpolation between start and end."""
return start + (end - start) * t
[docs]
def point_in_rect(point, rect):
"""Check if a point is inside a rectangle."""
return rect.collidepoint(point)
[docs]
def create_gradient_surface(width, height, start_color, end_color):
"""Create a gradient surface."""
surface = pygame.Surface((width, height))
for y in range(height):
ratio = y / height
r = int(start_color[0] + (end_color[0] - start_color[0]) * ratio)
g = int(start_color[1] + (end_color[1] - start_color[1]) * ratio)
b = int(start_color[2] + (end_color[2] - start_color[2]) * ratio)
pygame.draw.line(surface, (r, g, b), (0, y), (width, y))
return surface