engrharis commited on
Commit
fb2e9b3
·
verified ·
1 Parent(s): 8d2aac4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -232
app.py CHANGED
@@ -1,202 +1,53 @@
1
  import streamlit as st
2
  import base64
 
 
 
3
 
4
- def set_background_video():
5
- video_path = 'numbers moving background.mp4' # Path to your video file in Hugging Face Space
6
- with open(video_path, 'rb') as video_file:
7
  video_bytes = video_file.read()
8
-
9
- # Encode the video to base64
10
- video_base64 = base64.b64encode(video_bytes).decode('utf-8')
11
-
12
- # HTML to embed the video as background
13
  video_html = f"""
14
- <video autoplay loop muted style="position: absolute; top: 0; left: 0; width: 100%; height: 100%; object-fit: cover; z-index: -1;">
15
- <source src="data:video/mp4;base64,{video_base64}" type="video/mp4">
16
- </video>
17
- """
 
 
 
18
  st.markdown(video_html, unsafe_allow_html=True)
19
 
20
- # Call the function to set the background video
21
- set_background_video()
22
-
23
 
24
-
25
- import numpy as np
26
- import matplotlib.pyplot as plt
27
- from matplotlib.patches import Polygon, Circle
28
-
29
- # Function to calculate the distance between two points
30
  def calculate_distance(x1, y1, x2, y2):
31
  return np.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)
32
 
33
- # Function to calculate angles using the Law of Cosines
34
  def calculate_angle(a, b, c):
35
  try:
36
- angle = np.degrees(np.acos((b ** 2 + c ** 2 - a ** 2) / (2 * b * c)))
37
  except ValueError:
38
- angle = 0 # Handle possible domain error in acos
39
  return angle
40
 
41
- # Function to calculate area using Heron's formula
42
- def calculate_area(a, b, c):
43
- s = (a + b + c) / 2
44
- area = np.sqrt(s * (s - a) * (s - b) * (s - c))
45
- return area
46
-
47
- # Function to calculate the perimeter
48
- def calculate_perimeter(a, b, c):
49
- return a + b + c
50
-
51
- # Function to calculate the radius of the inscribed circle
52
- def calculate_radius_inscribed_circle(a, b, c):
53
- try:
54
- s = (a + b + c) / 2
55
- area = calculate_area(a, b, c)
56
- radius = area / s
57
- except ZeroDivisionError:
58
- radius = 0 # Handle case where area or perimeter is zero
59
- return radius
60
-
61
- # Function to calculate the radius of the circumscribed circle
62
- def calculate_radius_circumscribed_circle(a, b, c):
63
- try:
64
- area = calculate_area(a, b, c)
65
- radius = (a * b * c) / (4 * area)
66
- except ZeroDivisionError:
67
- radius = 0 # Handle case where area is zero
68
- return radius
69
-
70
- # Function to calculate the centroid coordinates
71
- def calculate_centroid(x1, y1, x2, y2, x3, y3):
72
- G_x = (x1 + x2 + x3) / 3
73
- G_y = (y1 + y2 + y3) / 3
74
- return G_x, G_y
75
-
76
- # Function to calculate the incenter coordinates
77
- def calculate_incenter(x1, y1, x2, y2, x3, y3, a, b, c):
78
- try:
79
- I_x = (a * x1 + b * x2 + c * x3) / (a + b + c)
80
- I_y = (a * y1 + b * y2 + c * y3) / (a + b + c)
81
- except ZeroDivisionError:
82
- I_x, I_y = 0, 0 # Handle division by zero if sides sum to zero
83
- return I_x, I_y
84
-
85
- # Function to calculate the circumcenter coordinates
86
- def calculate_circumcenter(x1, y1, x2, y2, x3, y3, a, b, c):
87
- try:
88
- D = 2 * (x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2))
89
- U_x = ((x1**2 + y1**2) * (y2 - y3) + (x2**2 + y2**2) * (y3 - y1) + (x3**2 + y3**2) * (y1 - y2)) / D
90
- U_y = ((x1**2 + y1**2) * (x3 - x2) + (x2**2 + y2**2) * (x1 - x3) + (x3**2 + y3**2) * (x2 - x1)) / D
91
- except ZeroDivisionError:
92
- U_x, U_y = 0, 0 # Handle division by zero in circumcenter calculation
93
- return U_x, U_y
94
 
95
- # Function to calculate midpoints of sides
96
- def calculate_midpoints(x1, y1, x2, y2, x3, y3):
97
- # Midpoint of AB
98
- M1_x = (x1 + x2) / 2
99
- M1_y = (y1 + y2) / 2
100
- # Midpoint of BC
101
- M2_x = (x2 + x3) / 2
102
- M2_y = (y2 + y3) / 2
103
- # Midpoint of CA
104
- M3_x = (x3 + x1) / 2
105
- M3_y = (y3 + y1) / 2
106
- return (M1_x, M1_y), (M2_x, M2_y), (M3_x, M3_y)
107
-
108
- # Function to format values close to zero as 0
109
- def format_zero(val):
110
- if abs(val) < 1e-6:
111
- return 0.0
112
- return val
113
-
114
- # Function to plot the triangle with all points in different colors and a legend
115
  def plot_triangle(x1, y1, x2, y2, x3, y3, I_x, I_y, U_x, U_y, G_x, G_y, midpoints, a, b, c):
116
- fig, ax = plt.subplots(figsize=(8, 6))
117
- triangle = Polygon([(x1, y1), (x2, y2), (x3, y3)], closed=True, edgecolor='b', facecolor='lightblue')
118
- ax.add_patch(triangle)
119
-
120
- # Define colors for different points
121
- vertex_color = 'blue'
122
- midpoint_color = 'green'
123
- centroid_color = 'orange'
124
- incenter_color = 'red'
125
- circumcenter_color = 'purple'
126
-
127
- # Plot the triangle vertices
128
- vertices = [(x1, y1), (x2, y2), (x3, y3)]
129
- vertex_labels = [f"Vertex A ({x1:.3f}, {y1:.3f})", f"Vertex B ({x2:.3f}, {y2:.3f})", f"Vertex C ({x3:.3f}, {y3:.3f})"]
130
- for i, (vx, vy) in enumerate(vertices):
131
- ax.scatter(vx, vy, color=vertex_color, zorder=3)
132
-
133
- # Plot key points with their corresponding colors
134
- key_points = [
135
- (I_x, I_y, incenter_color),
136
- (U_x, U_y, circumcenter_color),
137
- (G_x, G_y, centroid_color)
138
- ]
139
- key_points_labels = [f"Incenter ({I_x:.3f}, {I_y:.3f})", f"Circumcenter ({U_x:.3f}, {U_y:.3f})", f"Centroid ({G_x:.3f}, {G_y:.3f})"]
140
-
141
- for x, y, color in key_points:
142
- ax.scatter(x, y, color=color, zorder=4)
143
-
144
- # Plot midpoints of sides
145
- for i, (mx, my) in enumerate(midpoints):
146
- ax.scatter(mx, my, color=midpoint_color, zorder=5)
147
- midpoints_labels = [f"Mid-Point M1 ({(x1 + x2) / 2:.3f}, {(y1 + y2) / 2:.3f})",
148
- f"Mid-Point M2 ({(x2 + x3) / 2:.3f}, {(y2 + y3) / 2:.3f})",
149
- f"Mid-Point M3 ({(x1 + x3) / 2:.3f}, {(y1 + y3) / 2:.3f})"]
150
-
151
- # Draw the inscribed circle (incircle)
152
- radius_in = calculate_radius_inscribed_circle(a, b, c)
153
- incircle = Circle((I_x, I_y), radius_in, color=incenter_color, fill=False, linestyle='--', linewidth=2, label="Inscribed Circle")
154
- ax.add_patch(incircle)
155
-
156
- # Draw the circumscribed circle (circumcircle)
157
- radius_circum = calculate_radius_circumscribed_circle(a, b, c)
158
- circumcircle = Circle((U_x, U_y), radius_circum, color=circumcenter_color, fill=False, linestyle='--', linewidth=2, label="Circumscribed Circle")
159
- ax.add_patch(circumcircle)
160
-
161
- # Add legend
162
- handles = [
163
- plt.Line2D([0], [0], marker='o', color='w', markerfacecolor=vertex_color, markersize=8, label=vertex_labels[0]),
164
- plt.Line2D([0], [0], marker='o', color='w', markerfacecolor=vertex_color, markersize=8, label=vertex_labels[1]),
165
- plt.Line2D([0], [0], marker='o', color='w', markerfacecolor=vertex_color, markersize=8, label=vertex_labels[2]),
166
- plt.Line2D([0], [0], marker='o', color='w', markerfacecolor=midpoint_color, markersize=8, label=midpoints_labels[0]),
167
- plt.Line2D([0], [0], marker='o', color='w', markerfacecolor=midpoint_color, markersize=8, label=midpoints_labels[1]),
168
- plt.Line2D([0], [0], marker='o', color='w', markerfacecolor=midpoint_color, markersize=8, label=midpoints_labels[2]),
169
- plt.Line2D([0], [0], marker='o', color='w', markerfacecolor=incenter_color, markersize=8, label=key_points_labels[0]),
170
- plt.Line2D([0], [0], marker='o', color='w', markerfacecolor=circumcenter_color, markersize=8, label=key_points_labels[1]),
171
- plt.Line2D([0], [0], marker='o', color='w', markerfacecolor=centroid_color, markersize=8, label=key_points_labels[2])
172
- ]
173
- ax.legend(handles=handles, loc='upper left', fontsize=12)
174
-
175
- # Adjust the plot limits and aspect ratio
176
- padding = 3
177
- ax.set_xlim([min(x1, x2, x3) - padding, max(x1, x2, x3) + padding])
178
- ax.set_ylim([min(y1, y2, y3) - padding, max(y1, y2, y3) + padding])
179
- ax.set_aspect('equal', adjustable='datalim')
180
-
181
- ax.set_title('Solved Triangle', fontsize=18)
182
- ax.set_xlabel('X-axis', fontsize=12)
183
- ax.set_ylabel('Y-axis', fontsize=12)
184
-
185
- plt.grid(True)
186
- st.pyplot(fig)
187
 
188
- # Function to check if the sides form a valid triangle
189
  def is_valid_triangle(a, b, c):
190
- # Check if the sum of two sides is greater than the third side (Triangle Inequality Theorem)
191
  return a + b > c and b + c > a and c + a > b
192
 
193
- # Main function to interact with the user
194
  def main():
195
  st.title("Advanced Triangle Solver")
196
 
197
  st.sidebar.header("Enter the coordinates of the three points:")
198
-
199
- # Coordinates input (X1, Y1), (X2, Y2), (X3, Y3)
200
  x1 = st.sidebar.number_input("X1", min_value=-100.0, max_value=100.0, step=0.1, format="%.3f")
201
  y1 = st.sidebar.number_input("Y1", min_value=-100.0, max_value=100.0, step=0.1, format="%.3f")
202
  x2 = st.sidebar.number_input("X2", min_value=-100.0, max_value=100.0, step=0.1, format="%.3f")
@@ -205,90 +56,36 @@ def main():
205
  y3 = st.sidebar.number_input("Y3", min_value=-100.0, max_value=100.0, step=0.1, format="%.3f")
206
 
207
  if st.sidebar.button("Calculate"):
208
- # Calculate the lengths of the sides of the triangle using Euclidean distance
209
  a = calculate_distance(x2, y2, x3, y3)
210
  b = calculate_distance(x1, y1, x3, y3)
211
  c = calculate_distance(x1, y1, x2, y2)
212
 
213
- # Validate if it's a valid triangle
214
  if not is_valid_triangle(a, b, c):
215
  st.error("The entered points do not form a valid triangle.")
216
  return
217
-
218
- # Calculate angles using the Law of Cosines
219
  A = calculate_angle(a, b, c)
220
  B = calculate_angle(b, a, c)
221
  C = calculate_angle(c, a, b)
222
 
223
- # Check if angles sum up to 180 degrees
224
  if abs(A + B + C - 180) > 1e-2:
225
  st.error("The sum of the angles is not 180 degrees.")
226
  return
227
 
228
- # Calculate area, perimeter, and radius of inscribed and circumscribed circles
229
  area = calculate_area(a, b, c)
230
  perimeter = calculate_perimeter(a, b, c)
231
  radius_in = calculate_radius_inscribed_circle(a, b, c)
232
  radius_circum = calculate_radius_circumscribed_circle(a, b, c)
233
-
234
- # Calculate centroid, incenter, and circumcenter coordinates
235
  G_x, G_y = calculate_centroid(x1, y1, x2, y2, x3, y3)
236
  I_x, I_y = calculate_incenter(x1, y1, x2, y2, x3, y3, a, b, c)
237
  U_x, U_y = calculate_circumcenter(x1, y1, x2, y2, x3, y3, a, b, c)
238
-
239
- # Calculate midpoints of the sides
240
  midpoints = calculate_midpoints(x1, y1, x2, y2, x3, y3)
241
 
242
- # Display results in columns
243
- col1, col2 = st.columns(2)
244
-
245
- with col1:
246
- st.subheader("Coordinates of Triangle:")
247
- st.markdown(f"Vertex A: **({x1:.3f}, {y1:.3f})**")
248
- st.markdown(f"Vertex B: **({x2:.3f}, {y2:.3f})**")
249
- st.markdown(f"Vertex C: **({x3:.3f}, {y3:.3f})**")
250
-
251
- with col2:
252
- st.subheader("Mid-Points of Triangle:")
253
- st.markdown(f"Midpoint of AB: ({midpoints[0][0]:.3f}, {midpoints[0][1]:.3f})")
254
- st.markdown(f"Midpoint of BC: ({midpoints[1][0]:.3f}, {midpoints[1][1]:.3f})")
255
- st.markdown(f"Midpoint of CA: ({midpoints[2][0]:.3f}, {midpoints[2][1]:.3f})")
256
-
257
-
258
- col1, col2 = st.columns(2)
259
-
260
- with col1:
261
- st.subheader("Angles of Triangle:")
262
- st.markdown(f"Angle A: **{format_zero(A):.3f}°**")
263
- st.markdown(f"Angle B: **{format_zero(B):.3f}°**")
264
- st.markdown(f"Angle C: **{format_zero(C):.3f}°**")
265
-
266
- with col2:
267
- st.subheader("Sides of Triangle:")
268
- st.markdown(f"Side a: **{format_zero(a):.3f}** units")
269
- st.markdown(f"Side b: **{format_zero(b):.3f}** units")
270
- st.markdown(f"Side c: **{format_zero(c):.3f}** units")
271
-
272
-
273
- col1, col2, col3 = st.columns(3)
274
-
275
- with col1:
276
- st.subheader("Incenter of Triangle:")
277
- st.markdown(f"Coordinates: **({format_zero(I_x):.3f}, {format_zero(I_y):.3f})**")
278
- st.markdown(f"Radius: **{radius_in:.3f}** units")
279
-
280
- with col2:
281
- st.subheader("Circumcenter of Triangle:")
282
- st.markdown(f"Coordinates: **({format_zero(U_x):.3f}, {format_zero(U_y):.3f})**")
283
- st.markdown(f"Radius: **{radius_circum:.3f}** units")
284
-
285
- with col3:
286
- st.subheader("Other Properties:")
287
- st.markdown(f"Area: **{format_zero(area):.3f}** square units")
288
- st.markdown(f"Perimeter: **{format_zero(perimeter):.3f}** units")
289
- st.markdown(f"Centroid: **({format_zero(G_x):.3f}, {format_zero(G_y):.3f})**")
290
 
291
- # Display triangle graph with midpoints and colored points
292
  plot_triangle(x1, y1, x2, y2, x3, y3, I_x, I_y, U_x, U_y, G_x, G_y, midpoints, a, b, c)
293
 
294
  if __name__ == "__main__":
 
1
  import streamlit as st
2
  import base64
3
+ import numpy as np
4
+ import matplotlib.pyplot as plt
5
+ from matplotlib.patches import Polygon, Circle
6
 
7
+ def set_background(video_path):
8
+ """Sets the background of the Streamlit app to a video."""
9
+ with open(video_path, "rb") as video_file:
10
  video_bytes = video_file.read()
11
+ video_base64 = base64.b64encode(video_bytes).decode("utf-8")
 
 
 
 
12
  video_html = f"""
13
+ <style>
14
+ .main {{
15
+ background-image: url("data:video/mp4;base64,{video_base64}");
16
+ background-size: cover;
17
+ }}
18
+ </style>
19
+ """
20
  st.markdown(video_html, unsafe_allow_html=True)
21
 
22
+ # Set the background *before* any other Streamlit elements
23
+ set_background("numbers moving background.mp4") # Replace with your video path
 
24
 
25
+ # --- Triangle Calculation and Plotting Functions ---
 
 
 
 
 
26
  def calculate_distance(x1, y1, x2, y2):
27
  return np.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)
28
 
 
29
  def calculate_angle(a, b, c):
30
  try:
31
+ angle = np.degrees(np.arccos((b ** 2 + c ** 2 - a ** 2) / (2 * b * c)))
32
  except ValueError:
33
+ angle = 0
34
  return angle
35
 
36
+ # ... (rest of your calculation functions: calculate_area, calculate_perimeter,
37
+ # calculate_radius_inscribed_circle, calculate_radius_circumscribed_circle,
38
+ # calculate_centroid, calculate_incenter, calculate_circumcenter, calculate_midpoints, format_zero)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
  def plot_triangle(x1, y1, x2, y2, x3, y3, I_x, I_y, U_x, U_y, G_x, G_y, midpoints, a, b, c):
41
+ # ... (your plot_triangle function - no changes needed here)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
 
 
43
  def is_valid_triangle(a, b, c):
 
44
  return a + b > c and b + c > a and c + a > b
45
 
46
+ # --- Main Streamlit App ---
47
  def main():
48
  st.title("Advanced Triangle Solver")
49
 
50
  st.sidebar.header("Enter the coordinates of the three points:")
 
 
51
  x1 = st.sidebar.number_input("X1", min_value=-100.0, max_value=100.0, step=0.1, format="%.3f")
52
  y1 = st.sidebar.number_input("Y1", min_value=-100.0, max_value=100.0, step=0.1, format="%.3f")
53
  x2 = st.sidebar.number_input("X2", min_value=-100.0, max_value=100.0, step=0.1, format="%.3f")
 
56
  y3 = st.sidebar.number_input("Y3", min_value=-100.0, max_value=100.0, step=0.1, format="%.3f")
57
 
58
  if st.sidebar.button("Calculate"):
 
59
  a = calculate_distance(x2, y2, x3, y3)
60
  b = calculate_distance(x1, y1, x3, y3)
61
  c = calculate_distance(x1, y1, x2, y2)
62
 
 
63
  if not is_valid_triangle(a, b, c):
64
  st.error("The entered points do not form a valid triangle.")
65
  return
66
+
 
67
  A = calculate_angle(a, b, c)
68
  B = calculate_angle(b, a, c)
69
  C = calculate_angle(c, a, b)
70
 
 
71
  if abs(A + B + C - 180) > 1e-2:
72
  st.error("The sum of the angles is not 180 degrees.")
73
  return
74
 
75
+ # ... (rest of your calculations for area, perimeter, radii, centroid, incenter, circumcenter)
76
  area = calculate_area(a, b, c)
77
  perimeter = calculate_perimeter(a, b, c)
78
  radius_in = calculate_radius_inscribed_circle(a, b, c)
79
  radius_circum = calculate_radius_circumscribed_circle(a, b, c)
 
 
80
  G_x, G_y = calculate_centroid(x1, y1, x2, y2, x3, y3)
81
  I_x, I_y = calculate_incenter(x1, y1, x2, y2, x3, y3, a, b, c)
82
  U_x, U_y = calculate_circumcenter(x1, y1, x2, y2, x3, y3, a, b, c)
 
 
83
  midpoints = calculate_midpoints(x1, y1, x2, y2, x3, y3)
84
 
85
+ # Display results (no changes needed here)
86
+ # ... (your existing display code using st.columns, st.subheader, st.markdown)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87
 
88
+ # Plot the triangle
89
  plot_triangle(x1, y1, x2, y2, x3, y3, I_x, I_y, U_x, U_y, G_x, G_y, midpoints, a, b, c)
90
 
91
  if __name__ == "__main__":