Generate detailed documetation for this: [code]
- User
Generate detailed documetation for this:
# 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
Trig Class Documentation
Overview
The
Trigclass provides static utility methods for geometric calculations, particularly focused on point and shape transformations in 2D space. This class is designed to be used without instantiation, offering pure computational functionality.Installation
Ensure the
utils.pyfile containing this class is in your project's Python path.Usage
Import the
Trigclass from the utils module:from utils import TrigMethods
rotate_point(point_x, point_y, angle_radians, center_x=0, center_y=0)Rotates a single point around a specified center point.
Parameters:
point_x(float): X-coordinate of the point to rotatepoint_y(float): Y-coordinate of the point to rotateangle_radians(float): Rotation angle in radianscenter_x(float, optional): X-coordinate of rotation center. Defaults to 0.center_y(float, optional): Y-coordinate of rotation center. Defaults to 0.
Returns:
- tuple: (new_x, new_y) coordinates of the rotated point
Example:
# Rotate point (5,3) by 90 degrees (π/2 radians) around origin rotated_x, rotated_y = Trig.rotate_point(5, 3, math.pi/2)rotate_points(points_list, angle_radians, pivot=(0, 0))Rotates multiple points around a specified pivot point.
Parameters:
points_list(list): List of (x,y) tuples representing points to rotateangle_radians(float): Rotation angle in radianspivot(tuple, optional): (x,y) coordinates of rotation pivot. Defaults to (0,0).
Returns:
- list: List of (x,y) tuples representing the rotated points
Example:
# Rotate a square around its center square = [(0,0), (1,0), (1,1), (0,1)] center = (0.5, 0.5) rotated_square = Trig.rotate_points(square, math.pi/4, center)get_centroid(vertices)Calculates the centroid (geometric center) of a polygon.
Parameters:
vertices(list): List of (x,y) tuples representing polygon vertices
Returns:
- tuple: (x,y) coordinates of the centroid
Notes:
- Returns (0.0, 0.0) if input list has fewer than 3 vertices
- Works for any polygon, including triangles
Example:
# Find the center of a triangle triangle = [(0,0), (4,0), (2,3)] center = Trig.get_centroid(triangle)scale_point(point_x, point_y, scale_x, scale_y, center_x=0, center_y=0)Scales a single point relative to a center point.
Parameters:
point_x(float): X-coordinate of the point to scalepoint_y(float): Y-coordinate of the point to scalescale_x(float): Scaling factor for x-coordinatescale_y(float): Scaling factor for y-coordinatecenter_x(float, optional): X-coordinate of scaling center. Defaults to 0.center_y(float, optional): Y-coordinate of scaling center. Defaults to 0.
Returns:
- tuple: (new_x, new_y) coordinates of the scaled point
Example:
# Double the size of a point (3,4) relative to origin scaled_x, scaled_y = Trig.scale_point(3, 4, 2.0, 2.0)scale_points(points_list, scale_x, scale_y, center=(0,0))Scales multiple points relative to a center point.
Parameters:
points_list(list): List of (x,y) tuples representing points to scalescale_x(float): Scaling factor for x-coordinatesscale_y(float): Scaling factor for y-coordinatescenter(tuple, optional): (x,y) coordinates of scaling center. Defaults to (0,0).
Returns:
- list: List of (x,y) tuples representing the scaled points
Example:
# Scale a rectangle to twice its width and half its height rect = [(1,1), (5,1), (5,3), (1,3)] center = (3, 2) scaled_rect = Trig.scale_points(rect, 2.0, 0.5, center)Dependencies
math: Standard Python library for mathematical operations
Would you like me to explain or break down any part of this documentation?
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.