File size: 4,456 Bytes
01b44ac 0773e01 14e92cc 0773e01 14e92cc 01b44ac 14e92cc 01b44ac 0773e01 765e25a 0773e01 765e25a 0773e01 765e25a 0773e01 765e25a 0773e01 765e25a 0773e01 765e25a 0773e01 765e25a 0773e01 765e25a 0773e01 01b44ac 0773e01 01b44ac 0773e01 01b44ac 0773e01 14e92cc 01b44ac 14e92cc 01b44ac 765e25a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
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()
|