|
import streamlit as st |
|
import numpy as np |
|
import matplotlib.pyplot as plt |
|
from matplotlib.patches import Polygon, Circle |
|
|
|
|
|
def calculate_distance(x1, y1, x2, y2): |
|
return np.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2) |
|
|
|
|
|
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 |
|
return angle |
|
|
|
|
|
def calculate_area(a, b, c): |
|
s = (a + b + c) / 2 |
|
return np.sqrt(s * (s - a) * (s - b) * (s - c)) |
|
|
|
|
|
def calculate_perimeter(a, b, c): |
|
return a + b + c |
|
|
|
|
|
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") |
|
|
|
|
|
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 π"): |
|
|
|
a = calculate_distance(x2, y2, x3, y3) |
|
b = calculate_distance(x1, y1, x3, y3) |
|
c = calculate_distance(x1, y1, x2, y2) |
|
|
|
|
|
angle_A = calculate_angle(b, a, c) |
|
angle_B = calculate_angle(c, a, b) |
|
angle_C = calculate_angle(a, b, c) |
|
|
|
|
|
area = calculate_area(a, b, c) |
|
perimeter = calculate_perimeter(a, b, c) |
|
|
|
|
|
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**") |
|
|
|
|
|
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() |
|
|