import streamlit as st import numpy as np import matplotlib.pyplot as plt from matplotlib.patches import Polygon, Circle # Function to calculate the distance between two points def calculate_distance(x1, y1, x2, y2): return np.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2) # Function to calculate angles using the Law of Cosines def calculate_angle(a, b, c): try: angle = np.degrees(np.arccos((b ** 2 + c ** 2 - a ** 2) / (2 * b * c))) except ValueError: angle = 0 # Handle possible domain error in acos return angle # Function to calculate the area of a triangle using Heron's formula def calculate_area(a, b, c): s = (a + b + c) / 2 return np.sqrt(s * (s - a) * (s - b) * (s - c)) # Function to calculate the perimeter of the triangle def calculate_perimeter(a, b, c): return a + b + c # Main function to run the app def main(): st.set_page_config( page_title="Advanced Triangle Solver", layout="wide", initial_sidebar_state="expanded", ) st.markdown( """ """, unsafe_allow_html=True, ) st.title("🔺 Advanced Triangle Solver") st.sidebar.header("📌 Input Coordinates") # Collect user input x1 = st.sidebar.number_input("X1", min_value=-100.0, max_value=100.0, step=0.1, format="%.2f") y1 = st.sidebar.number_input("Y1", min_value=-100.0, max_value=100.0, step=0.1, format="%.2f") x2 = st.sidebar.number_input("X2", min_value=-100.0, max_value=100.0, step=0.1, format="%.2f") y2 = st.sidebar.number_input("Y2", min_value=-100.0, max_value=100.0, step=0.1, format="%.2f") x3 = st.sidebar.number_input("X3", min_value=-100.0, max_value=100.0, step=0.1, format="%.2f") y3 = st.sidebar.number_input("Y3", min_value=-100.0, max_value=100.0, step=0.1, format="%.2f") if st.sidebar.button("Calculate 🔍"): # Calculate distances a = calculate_distance(x2, y2, x3, y3) b = calculate_distance(x1, y1, x3, y3) c = calculate_distance(x1, y1, x2, y2) # Calculate angles angle_A = calculate_angle(b, a, c) angle_B = calculate_angle(c, a, b) angle_C = calculate_angle(a, b, c) # Calculate area and perimeter area = calculate_area(a, b, c) perimeter = calculate_perimeter(a, b, c) # Display results st.subheader("📐 Triangle Properties") st.write(f"**Side a (between points (x2, y2) and (x3, y3)): {a:.2f} units**") st.write(f"**Side b (between points (x1, y1) and (x3, y3)): {b:.2f} units**") st.write(f"**Side c (between points (x1, y1) and (x2, y2)): {c:.2f} units**") st.write(f"**Angle A (at point (x1, y1)): {angle_A:.2f}°**") st.write(f"**Angle B (at point (x2, y2)): {angle_B:.2f}°**") st.write(f"**Angle C (at point (x3, y3)): {angle_C:.2f}°**") st.write(f"**Area of the Triangle: {area:.2f} square units**") st.write(f"**Perimeter of the Triangle: {perimeter:.2f} units**") # Plot the triangle fig, ax = plt.subplots() triangle = Polygon([(x1, y1), (x2, y2), (x3, y3)], closed=True, fill=None, edgecolor='r') ax.add_patch(triangle) ax.text(x1, y1, f'({x1}, {y1})', fontsize=12, ha='right') ax.text(x2, y2, f'({x2}, {y2})', fontsize=12, ha='right') ax.text(x3, y3, f'({x3}, {y3})', fontsize=12, ha='right') ax.set_xlim(min(x1, x2, x3) - 5, max(x1, x2, x3) + 5) ax.set_ylim(min(y1, y2, y3) - 5, max(y1, y2, y3) + 5) ax.set_aspect('equal', adjustable='box') ax.set_title("Triangle Visualization") st.pyplot(fig) if __name__ == "__main__": main()