Update app.py
Browse files
app.py
CHANGED
@@ -3,8 +3,6 @@ import numpy as np
|
|
3 |
import matplotlib.pyplot as plt
|
4 |
from matplotlib.patches import Polygon, Circle
|
5 |
|
6 |
-
# Functions (including calculate_distance) are properly defined here
|
7 |
-
|
8 |
# Function to calculate the distance between two points
|
9 |
def calculate_distance(x1, y1, x2, y2):
|
10 |
return np.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)
|
@@ -12,13 +10,21 @@ def calculate_distance(x1, y1, x2, y2):
|
|
12 |
# Function to calculate angles using the Law of Cosines
|
13 |
def calculate_angle(a, b, c):
|
14 |
try:
|
15 |
-
angle = np.degrees(np.
|
16 |
except ValueError:
|
17 |
angle = 0 # Handle possible domain error in acos
|
18 |
return angle
|
19 |
|
20 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
|
|
22 |
def main():
|
23 |
st.set_page_config(
|
24 |
page_title="Advanced Triangle Solver",
|
@@ -26,7 +32,6 @@ def main():
|
|
26 |
initial_sidebar_state="expanded",
|
27 |
)
|
28 |
|
29 |
-
# Enhanced Aesthetic Style
|
30 |
st.markdown(
|
31 |
"""
|
32 |
<style>
|
@@ -75,18 +80,44 @@ def main():
|
|
75 |
y3 = st.sidebar.number_input("Y3", min_value=-100.0, max_value=100.0, step=0.1, format="%.2f")
|
76 |
|
77 |
if st.sidebar.button("Calculate 🔍"):
|
78 |
-
#
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
85 |
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
|
|
90 |
|
91 |
if __name__ == "__main__":
|
92 |
main()
|
|
|
3 |
import matplotlib.pyplot as plt
|
4 |
from matplotlib.patches import Polygon, Circle
|
5 |
|
|
|
|
|
6 |
# Function to calculate the distance between two points
|
7 |
def calculate_distance(x1, y1, x2, y2):
|
8 |
return np.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)
|
|
|
10 |
# Function to calculate angles using the Law of Cosines
|
11 |
def calculate_angle(a, b, c):
|
12 |
try:
|
13 |
+
angle = np.degrees(np.arccos((b ** 2 + c ** 2 - a ** 2) / (2 * b * c)))
|
14 |
except ValueError:
|
15 |
angle = 0 # Handle possible domain error in acos
|
16 |
return angle
|
17 |
|
18 |
+
# Function to calculate the area of a triangle using Heron's formula
|
19 |
+
def calculate_area(a, b, c):
|
20 |
+
s = (a + b + c) / 2
|
21 |
+
return np.sqrt(s * (s - a) * (s - b) * (s - c))
|
22 |
+
|
23 |
+
# Function to calculate the perimeter of the triangle
|
24 |
+
def calculate_perimeter(a, b, c):
|
25 |
+
return a + b + c
|
26 |
|
27 |
+
# Main function to run the app
|
28 |
def main():
|
29 |
st.set_page_config(
|
30 |
page_title="Advanced Triangle Solver",
|
|
|
32 |
initial_sidebar_state="expanded",
|
33 |
)
|
34 |
|
|
|
35 |
st.markdown(
|
36 |
"""
|
37 |
<style>
|
|
|
80 |
y3 = st.sidebar.number_input("Y3", min_value=-100.0, max_value=100.0, step=0.1, format="%.2f")
|
81 |
|
82 |
if st.sidebar.button("Calculate 🔍"):
|
83 |
+
# Calculate distances
|
84 |
+
a = calculate_distance(x2, y2, x3, y3)
|
85 |
+
b = calculate_distance(x1, y1, x3, y3)
|
86 |
+
c = calculate_distance(x1, y1, x2, y2)
|
87 |
+
|
88 |
+
# Calculate angles
|
89 |
+
angle_A = calculate_angle(b, a, c)
|
90 |
+
angle_B = calculate_angle(c, a, b)
|
91 |
+
angle_C = calculate_angle(a, b, c)
|
92 |
+
|
93 |
+
# Calculate area and perimeter
|
94 |
+
area = calculate_area(a, b, c)
|
95 |
+
perimeter = calculate_perimeter(a, b, c)
|
96 |
+
|
97 |
+
# Display results
|
98 |
+
st.subheader("📐 Triangle Properties")
|
99 |
+
st.write(f"**Side a (between points (x2, y2) and (x3, y3)): {a:.2f} units**")
|
100 |
+
st.write(f"**Side b (between points (x1, y1) and (x3, y3)): {b:.2f} units**")
|
101 |
+
st.write(f"**Side c (between points (x1, y1) and (x2, y2)): {c:.2f} units**")
|
102 |
+
st.write(f"**Angle A (at point (x1, y1)): {angle_A:.2f}°**")
|
103 |
+
st.write(f"**Angle B (at point (x2, y2)): {angle_B:.2f}°**")
|
104 |
+
st.write(f"**Angle C (at point (x3, y3)): {angle_C:.2f}°**")
|
105 |
+
st.write(f"**Area of the Triangle: {area:.2f} square units**")
|
106 |
+
st.write(f"**Perimeter of the Triangle: {perimeter:.2f} units**")
|
107 |
+
|
108 |
+
# Plot the triangle
|
109 |
+
fig, ax = plt.subplots()
|
110 |
+
triangle = Polygon([(x1, y1), (x2, y2), (x3, y3)], closed=True, fill=None, edgecolor='r')
|
111 |
+
ax.add_patch(triangle)
|
112 |
+
ax.text(x1, y1, f'({x1}, {y1})', fontsize=12, ha='right')
|
113 |
+
ax.text(x2, y2, f'({x2}, {y2})', fontsize=12, ha='right')
|
114 |
+
ax.text(x3, y3, f'({x3}, {y3})', fontsize=12, ha='right')
|
115 |
|
116 |
+
ax.set_xlim(min(x1, x2, x3) - 5, max(x1, x2, x3) + 5)
|
117 |
+
ax.set_ylim(min(y1, y2, y3) - 5, max(y1, y2, y3) + 5)
|
118 |
+
ax.set_aspect('equal', adjustable='box')
|
119 |
+
ax.set_title("Triangle Visualization")
|
120 |
+
st.pyplot(fig)
|
121 |
|
122 |
if __name__ == "__main__":
|
123 |
main()
|