# utils.py (ensure this file exists with this content or adjust import)
import math
class Trig:
"""
Handles geometric calculations.
All methods are static and perform calculations only.
"""
@staticmethod
def rotate_point(point_x, point_y, angle_radians, center_x=0, center_y=0):
"""Rotates a point around a center point."""
s = math.sin(angle_radians)
c = math.cos(angle_radians)
x = point_x - center_x
y = point_y - center_y
new_x = x * c - y * s
new_y = x * s + y * c
return new_x + center_x, new_y + center_y
@staticmethod
def rotate_points(points_list, angle_radians, pivot=(0, 0)):
"""Rotates a list of (x,y) points around a given pivot."""
px, py = pivot
cos_theta = math.cos(angle_radians)
sin_theta = math.sin(angle_radians)
rotated_points = []
for x, y in points_list:
x_shifted = x - px
y_shifted = y - py
x_rotated = x_shifted * cos_theta - y_shifted * sin_theta
y_rotated = x_shifted * sin_theta + y_shifted * cos_theta
rotated_points.append((x_rotated + px, y_rotated + py))
return rotated_points
@staticmethod
def get_centroid(vertices):
"""Calculates the centroid of a polygon (works for triangles too)."""
if not vertices or len(vertices) < 3:
# Or raise error, or handle as appropriate for context
return (0.0, 0.0)
x_sum = sum(v[0] for v in vertices)
y_sum = sum(v[1] for v in vertices)
num_vertices = len(vertices)
return (x_sum / num_vertices, y_sum / num_vertices)
@staticmethod
def scale_point(point_x, point_y, scale_x, scale_y, center_x=0, center_y=0):
"""Scales a point relative to a center point."""
x = (point_x - center_x) * scale_x + center_x
y = (point_y - center_y) * scale_y + center_y
return x, y
@staticmethod
def scale_points(points_list, scale_x, scale_y, center=(0,0)):
"""Scales a list of (x,y) points relative to a center point."""
scaled_points = []
cx, cy = center
for x, y in points_list:
new_x = (x - cx) * scale_x + cx
new_y = (y - cy) * scale_y + cy
scaled_points.append((new_x, new_y))
return scaled_points
Assistant
Documentation for utils.py
This document provides detailed documentation for the Trig class in the utils.py module. The Trig class is designed to handle geometric calculations, such as rotating and scaling points or lists of points, as well as calculating the centroid of a polygon. All methods in this class are static and perform calculations without maintaining any internal state.
Class: Trig
Description
The Trig class encapsulates utility functions for geometric transformations. It includes methods for rotating and scaling points, as well as calculating the centroid of a polygon. These methods are implemented as static methods, meaning they can be called directly on the class without instantiating an object.
Description:
Calculates the centroid (geometric center) of a polygon defined by a list of vertices.
Parameters:
vertices (list of tuples): A list of (x, y) tuples representing the vertices of the polygon. Must contain at least three vertices.
Returns:
A tuple (centroid_x, centroid_y) representing the coordinates of the centroid. If the input list is empty or contains fewer than three vertices, the method returns (0.0, 0.0).
Static Methods: All methods in the Trig class are static, meaning they can be called directly on the class without creating an instance. For example:
python
result = Trig.rotate_point(1, 0, math.pi / 2)
Coordinate System: The methods assume a Cartesian coordinate system where positive x-values extend to the right and positive y-values extend upwards.
Error Handling: The get_centroid method returns (0.0, 0.0) if the input list of vertices is empty or contains fewer than three points. Depending on the use case, you may want to raise an exception instead.
Performance: The methods are optimized for simplicity and readability. For large datasets, consider vectorized implementations using libraries like NumPy.
This documentation provides a comprehensive overview of the Trig class and its methods. Use it as a reference when integrating these utilities into your projects.