math / app.py
engrharis's picture
Update app.py
14e92cc verified
raw
history blame
4.46 kB
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(
"""
<style>
body {
background-color: #f7f9fc;
color: #333333;
font-family: 'Open Sans', sans-serif;
}
.stTitle, .stHeader, .stSubheader {
color: #1a73e8;
font-weight: bold;
}
.stSidebar {
background-color: #f0f4f8;
color: #333333;
}
.stMarkdown {
font-size: 16px;
}
.stButton > button {
background-color: #1a73e8;
color: white;
border: none;
border-radius: 5px;
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
}
.stButton > button:hover {
background-color: #155ab3;
}
</style>
""",
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()