Sunday, January 07, 2024

Learning Python

 Here is python code for a program I am working on:

import matplotlib.pyplot as plt

import numpy as np


def draw_right_triangle_with_angles_and_side(angle_A, angle_B, angle_C, side_AC):

    # Check if the angles form a valid right-angled triangle

    if angle_A + angle_B + angle_C != 180 or angle_B != 90 or not (angle_A > 0 and angle_C > 0):

        print("Invalid input: The angles provided do not form a valid right-angled triangle.")

        return


    # Calculate the length of side BC using the Pythagorean theorem

    side_BC = np.sqrt(side_AC**2 - side_AC**2 / np.tan(np.radians(angle_A))**2)


    # Define the vertices of the triangle ABC

    vertices = np.array([[0, 0], [side_BC, 0], [0, side_AC]])


    # Plot the triangle ABC

    plt.figure(figsize=(6, 6))

    plt.plot(vertices[:, 0], vertices[:, 1], label='Triangle ABC', marker='o')


    # Label the vertices

    plt.text(vertices[0, 0], vertices[0, 1], 'A', ha='right')

    plt.text(vertices[1, 0], vertices[1, 1], 'B', ha='left')

    plt.text(vertices[2, 0], vertices[2, 1], 'C', ha='right')


    # Set axis limits and labels

    plt.xlim(-1, max(side_BC, side_AC) + 1)

    plt.ylim(-1, max(side_BC, side_AC) + 1)

    plt.xlabel('X-axis')

    plt.ylabel('Y-axis')


    # Add legend

    plt.legend()


    # Show the plot

    plt.grid(True)

    plt.title('Right-Angled Triangle ABC')

    plt.show()


# Example usage

angle_A = 45  # Angle A in degrees

angle_B = 90  # Angle B (right angle) in degrees

angle_C = 45  # Angle C in degrees

side_AC = 14   # Length of side AC


draw_right_triangle_with_angles_and_side(angle_A, angle_B, angle_C, side_AC)