Untitled

 avatar
unknown
plain_text
6 days ago
12 kB
3
Indexable
import math

class RightTriangle:
    def __init__(self, hypotenuse=None, angle_degrees=None, adjacent_side=None, opposite_side=None):
            """
                    Represents a right-angled triangle for the Canoe Movement problem.

                            Attributes:
                                        hypotenuse (float, optional): The length of the hypotenuse (canoe path). Defaults to None.
                                                    angle_degrees (float, optional): The angle in degrees between the adjacent side and hypotenuse (angle to river bank). Defaults to None.
                                                                adjacent_side (float, optional): The length of the adjacent side (distance along the river). Defaults to None.
                                                                            opposite_side (float, optional): The length of the opposite side (distance perpendicular to the river). Defaults to None.
                                                                                    """
                                                                                            self.hypotenuse = hypotenuse
                                                                                                    self.angle_degrees = angle_degrees
                                                                                                            self.adjacent_side = adjacent_side
                                                                                                                    self.opposite_side = opposite_side

                                                                                                                        def calculate_adjacent_side_from_hypotenuse_angle(self):
                                                                                                                                """
                                                                                                                                        Calculates the adjacent side using the hypotenuse and angle (in degrees).
                                                                                                                                                Uses the cosine formula: adjacent_side = hypotenuse * cos(angle)
                                                                                                                                                        """
                                                                                                                                                                if self.hypotenuse is not None and self.angle_degrees is not None:
                                                                                                                                                                            # Convert angle to radians for math.cos function
                                                                                                                                                                                        angle_radians = math.radians(self.angle_degrees)
                                                                                                                                                                                                    self.adjacent_side = self.hypotenuse * math.cos(angle_radians)
                                                                                                                                                                                                                return self.adjacent_side
                                                                                                                                                                                                                        else:
                                                                                                                                                                                                                                    return None # Cannot calculate if hypotenuse or angle is missing

                                                                                                                                                                                                                                        def __str__(self):
                                                                                                                                                                                                                                                """
                                                                                                                                                                                                                                                        Provides a string representation of the triangle's properties.
                                                                                                                                                                                                                                                                """
                                                                                                                                                                                                                                                                        return (f"RightTriangle:\n"
                                                                                                                                                                                                                                                                                        f"  Hypotenuse (Canoe Path): {self.hypotenuse} meters\n"
                                                                                                                                                                                                                                                                                                        f"  Angle to River Bank: {self.angle_degrees} degrees\n"
                                                                                                                                                                                                                                                                                                                        f"  Distance Along River (Adjacent Side): {self.adjacent_side:.2f} meters\n" # Format to 2 decimal places
                                                                                                                                                                                                                                                                                                                                        f"  Distance Perpendicular to River (Opposite Side): {self.opposite_side} meters") # Opposite side is not calculated in this problem, remains None


                                                                                                                                                                                                                                                                                                                                        # --- Example Usage for the Canoe Problem ---

                                                                                                                                                                                                                                                                                                                                        # Create a RightTriangle object representing the canoe movement
                                                                                                                                                                                                                                                                                                                                        canoe_triangle = RightTriangle()

                                                                                                                                                                                                                                                                                                                                        # Set the known values from the problem
                                                                                                                                                                                                                                                                                                                                        canoe_triangle.hypotenuse = 200  # meters
                                                                                                                                                                                                                                                                                                                                        canoe_triangle.angle_degrees = 45 # degrees

                                                                                                                                                                                                                                                                                                                                        # Calculate the distance along the river (adjacent side)
                                                                                                                                                                                                                                                                                                                                        distance_along_river = canoe_triangle.calculate_adjacent_side_from_hypotenuse_angle()

                                                                                                                                                                                                                                                                                                                                        # Print the triangle information
                                                                                                                                                                                                                                                                                                                                        print(canoe_triangle)

                                                                                                                                                                                                                                                                                                                                        # You can also access the result directly:
                                                                                                                                                                                                                                                                                                                                        # print(f"\nDistance along the river: {canoe_triangle.adjacent_side:.2f} meters")
                                                                                                                                                                                                                                                                                                                                        
Editor is loading...
Leave a Comment